Skip to content

Commit

Permalink
Pig class finished
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneWWolf committed Sep 27, 2023
1 parent 23a7c8c commit 5100704
Show file tree
Hide file tree
Showing 3 changed files with 1,740 additions and 1,915 deletions.
34 changes: 34 additions & 0 deletions rpgsaga/saga/src/pig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class Pig
{
private _mass: number;
public identificator: string; // Код на бирке у свиньи

constructor(mass: number, identificator: string)
{
this.mass = mass;
this.identificator = identificator;
}

get mass(): number
{
return this._mass;
}

set mass(value: number)
{
if (value >= 50)
{
this._mass = value;
}
else
{
throw new Error("Pig's mass should be 50 kg or bigger.");
}
}

getSalo(): number
{
// Допускается, что количество получаемого со свиньи сала равно 20% от её массы тела
return this._mass * 0.2;
}
}
29 changes: 29 additions & 0 deletions rpgsaga/saga/tests/pig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Pig } from '../src/pig';

describe('Testing pig constructor', () => {
it('Pig should be created', () => {
const first = new Pig(500, "DIV69");
});
it('Pig with invalid mass', () => {
expect(() => new Pig(10, "RIP38")).toThrow(Error("Pig's mass should be 50 kg or bigger."));
});
it('Pig identificator should be assigned', () => {
const pig = new Pig(100, "REZ27");
expect(pig.identificator).toEqual("REZ27");
});
});

describe('Testing pig methods', () => {
it('Should give right amount of сало', () => {
const pig = new Pig(100, "ADB69");
expect(pig.getSalo()).toEqual(20);
});
it('Should be able to return float values', () => {
const pig = new Pig(101, "ADB69");
expect(pig.getSalo()).toBeCloseTo(20.2);
});
it('Should be able to work with float values', () => {
const pig = new Pig(100.5, "ADB69");
expect(pig.getSalo()).toBeCloseTo(20.1);
});
});
Loading

0 comments on commit 5100704

Please sign in to comment.