Created
April 3, 2014 08:43
-
-
Save imjacobclark/9950721 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function Calculator(){ | |
this.add = function(num1, num2){ | |
if(this.isNumber([num1,num2]) == false){ | |
throw new Error('Invalid argument specified to add method.'); | |
}else{ | |
return num1 + num2; | |
} | |
} | |
this.subtract = function(num1, num2){ | |
if(this.isNumber([num1,num2]) == false){ | |
throw new Error('Invalid argument specified to subtract method.'); | |
}else{ | |
return num1 - num2; | |
} | |
} | |
this.isNumber = function(arr){ | |
for(var i=0; i < arr.length; i++){ | |
if(typeof(arr[i]) !== 'number'){ | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
var calc = new Calculator(); | |
console.log(calc.add(1,"2")); | |
console.log(calc.subtract(5,4)); |
This file contains hidden or 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
describe('Calculator', function() { | |
var calc = new Calculator(); | |
describe('The add method', function(){ | |
it('should add two numbers together', function(){ | |
expect(calc.add(1,1)).toEqual(2); | |
expect(calc.add(20,33)).toEqual(53); | |
}); | |
it('should throw an expection when a non number is passed', function(){ | |
expect( function(){ calc.add(1,"1") } ).toThrow(new Error("Invalid argument specified to add method.")); | |
}); | |
}); | |
describe('The subtract method', function(){ | |
it('should subtract two numbers together', function(){ | |
expect(calc.subtract(5,4)).toEqual(1); | |
expect(calc.subtract(33,20)).toEqual(13); | |
}); | |
it('should throw an expection when a non number is passed', function(){ | |
expect( function(){ calc.subtract(5,"2") } ).toThrow(new Error("Invalid argument specified to subtract method.")); | |
}); | |
}); | |
describe('The validator method', function(){ | |
it('return true when all numbers are passed in', function(){ | |
expect(calc.isNumber([5,7,3,5,6])).toBeTruthy(); | |
expect(calc.isNumber([5,66,7777,88,5])).toBeTruthy(); | |
}); | |
it('return false when a non number is passed in', function(){ | |
expect(calc.isNumber(["5","7","3","5","6"])).toBeFalsy(); | |
expect(calc.isNumber([5,66,"7777",88,5])).toBeFalsy(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment