Skip to content

Commit

Permalink
//
Browse files Browse the repository at this point in the history
  • Loading branch information
HaQmUser committed Nov 5, 2024
1 parent 3ed5824 commit 9602201
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
8 changes: 4 additions & 4 deletions rpgsaga/saga/src/Rabbit.ts
Original file line number Diff line number Diff line change
@@ -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)
const rabbitOne = new Rabbit('Vitalik', 3, 'Brown', 'Male');
console.log(rabbitOne);
const rabbitTwo = new Rabbit('LuiVitonovich', 4, 'Black', 'Female');
console.log(rabbitTwo);
86 changes: 43 additions & 43 deletions rpgsaga/saga/src/RabbitClass.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
public get age() {
return this._age;
}
public get name() {
return this._name;
}
public get gender() {
return this._gender;
}
public get color() {
return this._color;
}
}
27 changes: 27 additions & 0 deletions rpgsaga/saga/tests/RabbitClass.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit 9602201

Please sign in to comment.