diff --git a/rpgsaga/saga/src/Rabbit.ts b/rpgsaga/saga/src/Rabbit.ts index cb91641d..1594e66f 100644 --- a/rpgsaga/saga/src/Rabbit.ts +++ b/rpgsaga/saga/src/Rabbit.ts @@ -1,6 +1,6 @@ import { Rabbit } from './RabbitClass'; -const rabbitOne = new Rabbit('Vitalik',3,'Brown','Male') -console.log(rabbitOne) -const rabbitTwo = new Rabbit('LuiVitonovich',4,'Black','Female') -console.log(rabbitTwo) \ No newline at end of file +const rabbitOne = new Rabbit('Vitalik', 3, 'Brown', 'Male'); +console.log(rabbitOne); +const rabbitTwo = new Rabbit('LuiVitonovich', 4, 'Black', 'Female'); +console.log(rabbitTwo); diff --git a/rpgsaga/saga/src/RabbitClass.ts b/rpgsaga/saga/src/RabbitClass.ts index fdfa997b..35c247ad 100644 --- a/rpgsaga/saga/src/RabbitClass.ts +++ b/rpgsaga/saga/src/RabbitClass.ts @@ -1,46 +1,46 @@ export class Rabbit { - private _name: string; - private _age: number; - private _color: string; - private _gender: string; - constructor(name: string, age: number, gender: string, color: string) { - this._name = name; - this._age = age; - this._color = color; - this._gender = gender; + private _name: string; + private _age: number; + private _color: string; + private _gender: string; + constructor(name: string, age: number, color: string, gender: string) { + this._name = name; + this._age = age; + this._color = color; + this._gender = gender; + } + + public set name(name: string) { + this._name = name; + } + public set age(age: number) { + if (age < 0 || age > 9) { + throw new Error('Invalid age'); } - - public set age(age: number) { - if (age < 0) { - throw new Error('Invalid age'); - } - this._age = age; + this._age = age; + } + public set color(color: string) { + this.color = color; + } + public set gender(gender: string) { + if (gender.toLowerCase() === 'male') { + this._gender = 'Male'; + } else if (gender.toLowerCase() === 'female') { + this._gender = 'Female'; + } else { + throw new Error('Invalid gender'); } - public set name(name: string) { - this._name = name; - } - public set color(color: string) { - this.color = color; - } - public set gender(gender: string) { - if (gender.toLowerCase() == 'male'){ - this._gender = 'Male'; - } else if (gender.toLowerCase() == 'female'){ - this._gender = 'Female' - } else { - throw new Error('Invalid gender') - } - } - public get age() { - return this._age; - } - public get name() { - return this._name; - } - public get gender() { - return this._gender; - } - public get color() { - return this._color; - } -} \ No newline at end of file + } + public get age() { + return this._age; + } + public get name() { + return this._name; + } + public get gender() { + return this._gender; + } + public get color() { + return this._color; + } +} diff --git a/rpgsaga/saga/tests/RabbitClass.spec.ts b/rpgsaga/saga/tests/RabbitClass.spec.ts new file mode 100644 index 00000000..eb767e27 --- /dev/null +++ b/rpgsaga/saga/tests/RabbitClass.spec.ts @@ -0,0 +1,27 @@ +import { Rabbit } from "../src/RabbitClass"; + +describe('Rabbit',()=> { + let rabbit: Rabbit; + beforeEach(()=>{ + rabbit = new Rabbit('Sergey',4,'Black','Male'); + }) + it('Should return right info',()=>{ + expect(rabbit.name).toBe('Sergey') + expect(rabbit.age).toBe(4) + expect(rabbit.color).toBe('Black') + expect(rabbit.gender).toBe('Male') + }) + it('Should return Error when age is invalid',()=>{ + expect(()=>{ + rabbit.age = -1 + }).toThrow('Invalid age') + expect(()=>{ + rabbit.age = 100 + }).toThrow('Invalid age') + }) + it('Should return Error when gender is invalid',()=>{ + expect(()=>{ + rabbit.gender='Mechanic' + }).toThrow('Invalid gender') + }) +}) \ No newline at end of file