Skip to content

For labs #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: Gorjunov_Aleksandr_Alekseevich
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rpgsaga.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
run: |
cd ./rpgsaga/saga
yarn install --immutable --immutable-cache --check-cache
yarn run lint
yarn run lint --fix
- name: Test Saga
run: |
cd ./rpgsaga/saga
Expand Down
13 changes: 5 additions & 8 deletions rpgsaga/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/saga/src/index.ts",
"program": "${workspaceFolder}\\src\\index.ts",
"outFiles": [
"${workspaceFolder}/**/*.js"
]
Expand All @@ -20,17 +21,13 @@
"type": "node",
"request": "launch",
"name": "Tests debug",
"program": "${workspaceRoot}/saga/node_modules/jest/bin/jest.js",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
"--config",
"${workspaceFolder}/saga/package.json",
"-i"
],
"skipFiles": [
"<node_internals>/**/*.js", "node_modules",
"${workspaceFolder}/out/**/*.js"
],
"console": "integratedTerminal"
]
}

]
}
130 changes: 130 additions & 0 deletions rpgsaga/saga/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:import/typescript',
'plugin:prettier/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'prettier', 'import'],
ignorePatterns: ['.eslintrc.js', 'react-app-env.d.ts'],
rules: {
'no-param-reassign': ['error'],
'prettier/prettier': ['error', { endOfLine: 'auto' }],
'linebreak-style': 0,
'arrow-body-style': [
'error',
'as-needed',
{
requireReturnForObjectLiteral: false,
},
],
curly: ['error', 'all'],
'no-implicit-coercion': ['error'],
'spaced-comment': ['error', 'always'],
eqeqeq: ['error', 'always'],
'prefer-template': 'error',
'no-useless-concat': 'error',
'prefer-destructuring': [
'error',
{
VariableDeclarator: {
array: false,
object: true,
},
AssignmentExpression: {
array: false,
object: true,
},
},
{
enforceForRenamedProperties: false,
},
],
'import/extensions': [
'error',
'ignorePackages',
{
ts: 'never',
tsx: 'never',
},
],
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
'import/order': [
'error',
{
'newlines-between': 'always',
groups: ['builtin', 'external', 'parent', 'sibling', 'index'],
pathGroups: [
{
pattern: 'src/**',
group: 'parent',
position: 'after',
},
],
},
],
'import/newline-after-import': 'error',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/ban-types': ['error'],
'@typescript-eslint/no-shadow': ['error'],
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'default',
format: ['camelCase'],
},
{
selector: ['enumMember', 'variable'],
format: ['camelCase', 'PascalCase', 'UPPER_CASE'],
},
{
selector: 'parameter',
format: ['camelCase'],
leadingUnderscore: 'allow',
modifiers: ['unused'],
},
{
selector: 'objectLiteralProperty',
filter: {
// Regular expression for BEM classnames
// Source: https://medium.com/takeaway-tech/the-search-for-a-regex-to-match-hyphenated-bem-css-class-names-5f8b66cc2bd9
regex: '^[a-z]([a-z0-9-]+)?(__([a-z0-9]+-?)+)?(--([a-z0-9]+-?)+){0,2}$',
match: true,
},
format: null,
},
{
selector: 'typeLike',
format: ['PascalCase'],
},
{
selector: 'typeProperty',
format: ['snake_case', 'camelCase'],
},
],
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
moduleDirectory: ['node_modules', '.'],
},
},
},
};
24 changes: 24 additions & 0 deletions rpgsaga/saga/computer/src/computer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Gadget } from "./gadget";

export class Computer extends Gadget {
hard_disk_size: number;

constructor(brand: string, model: string, hard_disk_size: number = 500) {
super(brand, model);
this.hard_disk_size = hard_disk_size;
}

setHardDiskSize(size: number): void {
this.hard_disk_size = size;
console.log(`Hard disk size set to ${this.hard_disk_size} GB.`);
}

getHardDiskSize(): number {
return this.hard_disk_size;
}

getStatus(): string {
const status = this.isOn ? "ON" : "OFF";
return `${this.brand} ${this.model} is ${status} with ${this.hard_disk_size} GB hard disk.`;
}
}
34 changes: 34 additions & 0 deletions rpgsaga/saga/computer/src/gadget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class Gadget {
brand: string;
model: string;
isOn: boolean;

constructor(brand: string, model: string) {
this.brand = brand;
this.model = model;
this.isOn = false;
}

turnOn(): void {
if (!this.isOn) {
this.isOn = true;
console.log(`${this.brand} ${this.model} is now ON.`);
} else {
console.log(`${this.brand} ${this.model} is already ON.`);
}
}

turnOff(): void {
if (this.isOn) {
this.isOn = false;
console.log(`${this.brand} ${this.model} is now OFF.`);
} else {
console.log(`${this.brand} ${this.model} is already OFF.`);
}
}

getStatus(): string {
const status = this.isOn ? "ON" : "OFF";
return `${this.brand} ${this.model} is ${status}.`;
}
}
27 changes: 27 additions & 0 deletions rpgsaga/saga/computer/src/phone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Gadget } from "./gadget";

export class Phone extends Gadget {
battery_level: number;

constructor(brand: string, model: string, battery_level: number = 100) {
super(brand, model);
this.battery_level = battery_level;
}

charge(percentage: number): void {
this.battery_level += percentage;
if (this.battery_level > 100) {
this.battery_level = 100;
}
console.log(`Battery level is now ${this.battery_level}%.`);
}

getBatteryLevel(): number {
return this.battery_level;
}

getStatus(): string {
const status = this.isOn ? "ON" : "OFF";
return `${this.brand} ${this.model} is ${status} with ${this.battery_level}% battery.`;
}
}
63 changes: 63 additions & 0 deletions rpgsaga/saga/computer/tests/computer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Computer } from "../src/computer";

describe('Computer', () => {
let computer: Computer;

beforeEach(() => {
computer = new Computer('Dell', 'XPS 13', 1000);
});

test('should initialize with correct brand and model', () => {
expect(computer.brand).toBe('Dell');
expect(computer.model).toBe('XPS 13');
});

test('should initialize with correct hard disk size', () => {
expect(computer.getHardDiskSize()).toBe(1000);
});

test('should turn on the computer', () => {
computer.turnOn();
expect(computer.isOn).toBe(true);
});

test('should not turn on the computer if it is already on', () => {
computer.turnOn();
const consoleSpy = jest.spyOn(console, 'log');
computer.turnOn();
expect(consoleSpy).toHaveBeenCalledWith('Dell XPS 13 is already ON.');
expect(computer.isOn).toBe(true);
});

test('should turn off the computer', () => {
computer.turnOn();
computer.turnOff();
expect(computer.isOn).toBe(false);
});

test('should not turn off the computer if it is already off', () => {
const consoleSpy = jest.spyOn(console, 'log');
computer.turnOff();
expect(consoleSpy).toHaveBeenCalledWith('Dell XPS 13 is already OFF.');
expect(computer.isOn).toBe(false);
});

test('should set hard disk size', () => {
computer.setHardDiskSize(2000);
expect(computer.getHardDiskSize()).toBe(2000);
});

test('should get correct status when computer is on', () => {
computer.turnOn();
expect(computer.getStatus()).toBe('Dell XPS 13 is ON with 1000 GB hard disk.');
});

test('should get correct status when computer is off', () => {
expect(computer.getStatus()).toBe('Dell XPS 13 is OFF with 1000 GB hard disk.');
});

test('should initialize with default hard disk size if not provided', () => {
const defaultComputer = new Computer('Apple', 'MacBook Pro');
expect(defaultComputer.getHardDiskSize()).toBe(500);
});
});
Loading
Loading