Skip to content

Commit

Permalink
Merge pull request #6 from EugeneWWolf/Ermakova_Ksenija_Dmitrievna
Browse files Browse the repository at this point in the history
Pig class finished
  • Loading branch information
jskonst authored Oct 11, 2023
2 parents f53ad7a + e768aeb commit 4dc7129
Show file tree
Hide file tree
Showing 3 changed files with 1,738 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;
}
}
27 changes: 27 additions & 0 deletions rpgsaga/saga/tests/pig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Pig } from '../src/pig';

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

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 4dc7129

Please sign in to comment.