Skip to content

Commit

Permalink
переделала класс и тесты к нему
Browse files Browse the repository at this point in the history
  • Loading branch information
Rey1147 committed Oct 11, 2023
1 parent 3e71293 commit 3273173
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 41 deletions.
3 changes: 1 addition & 2 deletions rpgsaga/saga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node --script-mode src/gun.ts",
"index": "ts-node --script-mode src/index.ts",
"dev": "ts-node --script-mode src/index.ts",
"test": "jest",
"lint": "eslint src --ext .js --ext .jsx --ext .ts --ext .tsx",
"lint:fix": "npm run lint -- --fix"
Expand Down
39 changes: 23 additions & 16 deletions rpgsaga/saga/src/gun.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
export class Gun {
readonly serialNumber: string = 's52001';
caliber: number;
readonly serialNumber: string = 's00001';
private aMagazine: number;
model: string;

magazin = 20;
shotCount = 0;

constructor(modelName: string, public bullets: number, gunCaliber?: number) {
constructor(modelName: string, magazine: number, public bullets?: number) {
this.model = modelName;
this.bullets = bullets;
this.caliber = gunCaliber;
this.magazine = magazine;
this.bullets = this.bullets < this.magazine && this.bullets >= 0 ? this.bullets : this.magazine;
}
// инфо :)
info() {
return `You're create gun model: "${this.model}". He has a magazine for ${this.magazine} bullets and ${this.bullets} bullets.`;
}

// выстрел
shot(): string {
return `The Model ${this.model} pistol fired`;
if (this.bullets > 0) {
this.shotCount += 1;
this.bullets -= 1;
return `The gun ${this.model} fired ${this.shotCount} times`;
} else {
return 'You are out of ammo. Recharge';
}
}

// перезарядка
recharge(): string {
this.bullets = this.magazine;
return 'The magazine is being recharged';
}

// проверка магазина
fullMagazin(): string {
this.bullets = this.bullets === undefined ? 0 : this.bullets;
if (this.magazin === this.bullets) {
return 'magazine full';
} else if (this.bullets > this.magazin / 2) {
return `in magazine ${this.bullets} bullets`;
} else {
return 'go recharge';
}
set magazine(magazine: number) {
this.aMagazine = magazine >= 0 && magazine < 21 ? magazine : this.aMagazine ?? 20;
}
get magazine() {
return this.aMagazine;
}
}
5 changes: 3 additions & 2 deletions rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Gun } from './gun';

const gun: Gun = new Gun('p-250', 9, 8);
const gun: Gun = new Gun('p-250', 21, 5);

console.log(gun.fullMagazin());
console.log(gun);
console.log(gun.shot());
69 changes: 48 additions & 21 deletions rpgsaga/saga/tests/gunTest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,66 @@
import { Gun } from '../src/gun';

describe('Testing gun constructor', () => {
it('gun must be create', () => {
const first = new Gun('Пистолет 1', 15, 5.45);
it('Gun must be create', () => {
const first = new Gun('Пистолет 1', 15, 5);
expect(first.model).toEqual('Пистолет 1');
expect(first.bullets).toEqual(15);
expect(first.caliber).toEqual(5.45);
expect(first.magazine).toEqual(15);
expect(first.bullets).toEqual(5);
});
it('Gun with empty caliber', () => {

it('Gun with empty string of bullets', () => {
const first = new Gun('Пистолет 2', 10);
expect(first.model).toEqual('Пистолет 2');
expect(first.magazine).toEqual(10);
expect(first.bullets).toEqual(10);
});
it('Number of bullets lower than bound', () => {
const first = new Gun('Пистолет 3', 10, -5);
expect(first.model).toEqual('Пистолет 3');
expect(first.magazine).toEqual(10);
expect(first.bullets).toEqual(10);
});
it('Number of bullets higher than bound', () => {
const first = new Gun('Пистолет 4', 10, 15);
expect(first.model).toEqual('Пистолет 4');
expect(first.magazine).toEqual(10);
expect(first.bullets).toEqual(10);
expect(first.caliber).toBeUndefined();
});

it('Magazine lower than bound', () => {
const first = new Gun('Пистолет 5', -5, 15);
expect(first.model).toEqual('Пистолет 5');
expect(first.magazine).toEqual(20);
expect(first.bullets).toEqual(15);
});
it('Magazine highed than bound', () => {
const first = new Gun('Пистолет 6', 35, 15);
expect(first.model).toEqual('Пистолет 6');
expect(first.magazine).toEqual(20);
expect(first.bullets).toEqual(15);
});
});

describe('Testing gun methods', () => {
it('Gun set fullMagazine valid value', () => {
const first = new Gun('Пистолет 3', 20, 3.62);
expect(first.fullMagazin()).toEqual('magazine full');
});
it('Gun set medium value', () => {
const first = new Gun('Пистолет 4', 13, 4.5);
expect(first.fullMagazin()).toEqual('in magazine 13 bullets');
it('The gun is firing', () => {
const first = new Gun('Пистолет 7', 20, 20);
expect(first.shot()).toEqual('The gun Пистолет 7 fired 1 times');
});
it('Gun set lower than valid value', () => {
const first = new Gun('Пистолет 5', -1, 2.5);
expect(first.fullMagazin()).toEqual('go recharge');
it('The gun fires several times', () => {
const first = new Gun('Пистолет 8', 10, 10);
expect(first.shot()).toEqual('The gun Пистолет 8 fired 1 times');
expect(first.shot()).toEqual('The gun Пистолет 8 fired 2 times');
expect(first.shot()).toEqual('The gun Пистолет 8 fired 3 times');
expect(first.shot()).toEqual('The gun Пистолет 8 fired 4 times');
});
it('Gun must shot', () => {
const first = new Gun('Пистолет 6', 10, 3.62);
expect(first.shot()).toEqual('The Model Пистолет 6 pistol fired');
it('The gun ran out of ammo', () => {
const first = new Gun('Пистолет 9', 20, 1);
expect(first.shot()).toEqual('The gun Пистолет 9 fired 1 times');
expect(first.shot()).toEqual('You are out of ammo. Recharge');
});
it('Gun must recharge', () => {
const first = new Gun('Пистолет 7', 2, 3.62);
it('Reloading the gun', () => {
const first = new Gun('Пистолет 10', 20, 1);
expect(first.recharge()).toEqual('The magazine is being recharged');
expect(first.bullets).toEqual(20);
});
});

0 comments on commit 3273173

Please sign in to comment.