Skip to content
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

Дополнительное задание к первому (абстрактные классы и методы, интерфейсы, наследование) #18

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
82 changes: 76 additions & 6 deletions rpgsaga/saga/src/pig.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,61 @@
export class Pig
// Список растений, овощей и фруктов, которые ест PlantEater
export enum plantNames
{
private _mass: number;
public identificator: string; // Код на бирке у свиньи

constructor(mass: number, identificator: string)
carrot = "carrot",
grass = "grass",
apple = "apple",
pear = "pear",
cucumber = "cucumber",
cabbage = "cabbage",
}

// Список хищников, которые могут охотиться на Herbivore
export enum carnivoreNames
{
wolf = "wolf",
bear = "bear",
}

abstract class Animal
{
protected _mass: number;
public identificator: string; // код животного на бирке, прикрепленной на ухо

/*
В Typescript (и в JS) нет возможности сделать перегрузку конструкторов,
поэтому сделала лишь один с опциональными параметрами.
*/
constructor(animalName ?: string)
{
if (animalName)
{
this.identificator = animalName;
}
else
{
this.identificator = "Unknown identificator";
}
}

abstract makeSound(): string;
}

interface PlantEater
{
eatPlant(plantName: plantNames): string;
}

interface Herbivore
{
runFromCarnivore(carnivoreName: carnivoreNames): string;
}

export class Pig extends Animal implements PlantEater, Herbivore
{
constructor(mass: number, identificator ?: string)
{
super(identificator);
this.mass = mass;
this.identificator = identificator;
}

get mass(): number
Expand All @@ -31,4 +80,25 @@ export class Pig
// Допускается, что количество получаемого со свиньи сала равно 20% от её массы тела
return this._mass * 0.2;
}

// Текстовое представление класса
public toString(): string
{
return `The pig id is ${this.identificator} and it's mass is ${this.mass}`;
}

makeSound(): string
{
return "Oink-oink";
}

eatPlant(plantName: plantNames): string
{
return `Pig "${this.identificator}" is peacefully eating a ${plantName}.`
}

runFromCarnivore(carnivoreName: carnivoreNames): string
{
return `Pig "${this.identificator}" successfully ran away from a ${carnivoreName}`;
}
}
45 changes: 43 additions & 2 deletions rpgsaga/saga/tests/pig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import { Pig } from '../src/pig';
import { Pig, plantNames, carnivoreNames } 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 empty identificator should be created', () => {
const pig = new Pig(500);
expect(pig.mass).toEqual(500);
expect(pig.identificator).toEqual("Unknown identificator");
});
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', () => {
describe ('Testing pig setters and getters', () => {
it('Normal mass', () => {
const pig = new Pig(50, "ID823");
expect(pig.mass).toEqual(50);
})
it('Normal mass (float)', () => {
const pig = new Pig(50.283, "ID823");
expect(pig.mass).toEqual(50.283);
})
it('Less than normal mass', () => {
expect(() => new Pig(25, "ID823")).toThrow(Error("Pig's mass should be 50 kg or bigger."));
})
it('Negative mass', () => {
expect(() => new Pig(-5, "ID823")).toThrow(Error("Pig's mass should be 50 kg or bigger."));
})
});

describe('Testing getSalo method', () => {
it('Should give right amount of сало', () => {
const pig = new Pig(100, "ADB69");
expect(pig.getSalo()).toEqual(20);
Expand All @@ -24,4 +46,23 @@ describe('Testing pig methods', () => {
const pig = new Pig(100.5, "ADB69");
expect(pig.getSalo()).toBeCloseTo(20.1);
});
});

describe('Testing Pig methods', () => {
it('Should return corrent text representation of Pig class', () => {
const pig = new Pig(100, "ADB69");
expect(pig.toString()).toEqual(`The pig id is ${pig.identificator} and it's mass is ${pig.mass}`);
});
it('Should return corrent sound', () => {
const pig = new Pig(100, "ADG283");
expect(pig.makeSound()).toEqual("Oink-oink");
});
it('Should return corrent message about eating plant', () => {
const pig = new Pig(100, "ADJ820");
expect(pig.eatPlant(plantNames.cabbage)).toEqual(`Pig "${pig.identificator}" is peacefully eating a cabbage.`);
});
it('Should successfully run away from carnivore', () => {
const pig = new Pig(100, "ADJ820");
expect(pig.runFromCarnivore(carnivoreNames.wolf)).toEqual(`Pig "${pig.identificator}" successfully ran away from a wolf`);
});
});