forked from ISUCT/TProgramming_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |