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

feature/prototypes #408

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
73 changes: 73 additions & 0 deletions staff/pere-hernandez/prototype/animal/Animal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function Animal (birthday, weight, height, legs){
if (birthday instanceof Date === false)
throw new TypeError(birthday + ' is not a Date')
if (typeof weight !== 'number')
throw new TypeError(weight + ' is not a number')
if (weight < 0)
throw new RangeError('weight needs to be positive')
if (typeof height !== 'number')
throw new TypeError(height + ' is not a number')
if (height < 0)
throw new RangeError('height needs to be positive')
if (!Number.isInteger(legs))
throw new TypeError(legs + ' is not an Integer')
if (legs < 0)
throw new RangeError('legs needs to be positive')

this.birthday = birthday
this.weight = weight
this.height = height
this.legs = legs
this.eats = ''
this.awake = true
this.speed = 0
this.alive = true
this.silent = true
this.message = ''
}

Animal.prototype.eat = function(food){
if (typeof food !== 'string')
throw new TypeError(food + ' is not a String')
if (this.alive === false)
throw new Error ('tries to eat on alive false')
if (this.awake === false)
throw new Error('tries to eat on awake false')


this.eats = food
}

Animal.prototype.sleep = function(){
if (this.alive === false)
throw new Error('tries to sleep on alive false')
this.awake = false
this.silent = true
this.message = ''
}

Animal.prototype.wakeUp = function(){
if (this.alive === false)
throw new Error('tries to wake up on alive false')
this.awake = true
}

Animal.prototype.move = function(speed){
if (typeof speed !== 'number')
throw new TypeError(speed + ' is not a number')
if (speed < 0)
throw new RangeError('speed must be positive')
if (this.alive === false)
throw new Error('tries to move on alive false')
if (this.awake === false)
throw new Error('try to move on awake false')


this.speed = speed
}

Animal.prototype.die = function(){
this.alive = false
}

module.exports = Animal
232 changes: 232 additions & 0 deletions staff/pere-hernandez/prototype/animal/Animal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
var Animal = require ('./Animal')
var assert = require ('./assert')


console.log('CASE Animal constructor happy')

var animal = new Animal(new Date(2020, 1, 8), 2.5, 0.3, 2)

assert.instanceOf(animal, Animal)

assert.assert(animal.birthday.getFullYear(), 2020)
assert.assert(animal.birthday.getMonth(), 1)
assert.assert(animal.birthday.getDate(), 8)
assert.assert(animal.weight, 2.5)
assert.assert(animal.height, 0.3)
assert.assert(animal.legs, 2)
assert.assert(animal.eats, '')
assert.assert(animal.awake, true)
assert.assert(animal.speed, 0)
assert.assert(animal.alive, true)
assert.assert(animal.silent, true)
assert.assert(animal.message, '')


console.log('CASE Animal constructor birthday unhappy')

var errorThrown
try {
animal = new Animal('12Oct21', 2.5, 0.3, 2)
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'TypeError', '12Oct21 is not a Date')



console.log('CASE Animal constructor weight unhappy')

try {
animal = new Animal(new Date(2020, 1, 8), '2.5', 0.3, 2)
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'TypeError', '2.5 is not a number')

try {
animal = new Animal(new Date(2020, 1, 8), -7, 0.3, 2)
} catch (error){
errorThrown = error
}
assert.error(errorThrown, 'RangeError', 'weight needs to be positive')



console.log('CASE Animal constructor height unhappy')

try {
animal = new Animal(new Date(2020, 1, 8), 2.5, '0.3', 2)
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'TypeError', '0.3 is not a number')

try {
animal = new Animal(new Date(2020, 1, 8), 2.5, -0.3, 2)
} catch (error){
errorThrown = error
}
assert.error(errorThrown, 'RangeError', 'height needs to be positive')



console.log('CASE Animal constructor legs unhappy')

try {
animal = new Animal(new Date(2020, 1, 8), 2.5, 0.3, '2')
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'TypeError', '2 is not an Integer')

try {
animal = new Animal(new Date(2020, 1, 8), 2.5, 0.3, -2)
} catch (error){
errorThrown = error
}
assert.error(errorThrown, 'RangeError', 'legs needs to be positive')



console.log('CASE eat() happy')

animal.eat('espaguetis')
assert.assert(animal.eats, 'espaguetis')



console.log('CASE eat() unhappy not a String')

try{
animal.eat(4)
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'TypeError', '4 is not a String')



console.log('CASE sleep() happy')

animal.sleep()
assert.assert(animal.awake, false)
assert.assert(animal.silent, true)
assert.assert(animal.message, '')



console.log('CASE try to eat on awake false unhappy')

try{
animal.eat('lechuga')
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'Error', 'tries to eat on awake false')



console.log('CASE wakeUp() happy')

animal.wakeUp()
assert.assert(animal.awake, true)



console.log('CASE moves() happy happy')

animal.move(5)
assert.assert(animal.speed, 5)



console.log('CASE moves() unhappy not a String')

try{
animal.move('A toda hostia')
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'TypeError', 'A toda hostia is not a number')



console.log('CASE moves() unhappy out of range')

try{
animal.move(-7)
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'RangeError', 'speed must be positive')



console.log('CASE moves() unhappy try to use move on awake false')

animal.sleep()
try{
animal.move(3)
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'Error', 'try to move on awake false')




console.log('CASE die() happy')

animal.wakeUp()
animal.die()
assert.assert(animal.alive, false)



console.log('CASE eat() on alive false unhappy')

try{
animal.eat('Una buena hostia')
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'Error', 'tries to eat on alive false')



console.log('CASE sleep() on alive false unhappy')

try{
animal.sleep()
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'Error', 'tries to sleep on alive false')



console.log('CASE wakeUp() on alive false unhappy')

animal.alive = true
animal.sleep()
animal.die()
try{
animal.wakeUp()
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'Error', 'tries to wake up on alive false')



console.log('CASE move() on alive false unhappy')

animal.alive = true
animal.wakeUp()
animal.die()
try{
animal.move(12)
} catch (error) {
errorThrown = error
}
assert.error(errorThrown, 'Error', 'tries to move on alive false')
43 changes: 43 additions & 0 deletions staff/pere-hernandez/prototype/animal/Cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var Pet = require ('./Pet')

function Cat (birthday, weight, height, legs, name, owner, breed){
Pet.call(this, birthday, weight, height, legs, name, owner)

if (typeof breed !== 'string')
throw new TypeError(breed + ' is not a string')

this.breed = breed
}

Cat.prototype = Object.create(Pet.prototype)
Cat.prototype.constructor = Cat

Cat.prototype.meow = function(){
if (this.alive === false)
throw new Error('tries to meow on alive false')
if (this.awake === false)
throw new Error('tries to meow on awake false')

this.silent = false
this.message = 'Meow, meow'
}


Cat.prototype.eat = function(food){
if (typeof food !== 'string')
throw new TypeError(food + ' is not a string')
if (this.alive === false)
throw new Error ('tries to eat on alive false')
if (this.awake === false)
throw new Error('tries to eat on awake false')


if (food === 'lasagna'){
this.silent = true
this.message = ''
}
this.eats = food
}


module.exports = Cat
Loading