diff --git a/.travis.yml b/.travis.yml index 76fd222..7bec32f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ node_js: before_install: - "npm install -g mocha" - "npm install chai" + - "npm install selenium-webdriver" notifications: email: false diff --git a/calculator.js b/calculator.js index caf34db..cc8e71f 100644 --- a/calculator.js +++ b/calculator.js @@ -1,5 +1,5 @@ function isNumber(numStr){ - var re = /^[0-9\.]+$/; + var re=/^[+-]?[0-9]+\.{0,1}[0-9]*$/ return re.test(numStr); } @@ -10,6 +10,14 @@ function add(num1, num2){ function substract(num1, num2){ return num1 - num2; } - +function mul(num1,num2){ + return num1*num2; +} +function div(num1,num2){ + return num1/num2; +} +module.exports.substract=substract; +module.exports.div=div; +module.exports.mul=mul; module.exports.add = add; module.exports.isNumber = isNumber; diff --git a/index.html b/index.html index cc6a9d9..5bcf6a3 100644 --- a/index.html +++ b/index.html @@ -20,8 +20,10 @@

Silly Calculator

=
diff --git a/main.js b/main.js index 957a234..7523089 100644 --- a/main.js +++ b/main.js @@ -1,23 +1,34 @@ window.onload = function(){ - document.getElementById('go_btn').onclick = function(){ - var num1Str = document.getElementById('num1').value; - var num2Str = document.getElementById('num2').value; - if (!isNumber(num1Str) || !isNumber(num2Str)){ - alert("Some of the input is not a number!") - document.getElementById('ans').value = "ERROR" - return + document.getElementById('go_btn').onclick = function(){ + var num1Str = document.getElementById('num1').value; + var num2Str = document.getElementById('num2').value; + if (!isNumber(num1Str) || !isNumber(num2Str)){ + alert("Some of the input is not a number!") + document.getElementById('ans').value = "ERROR" + return + } + var num1 = parseFloat(num1Str) + var num2 = parseFloat(num2Str) + var operator = document.getElementById('operator').value; + if (operator == "add"){ + document.getElementById('ans').value = add(num1, num2); + } + else if (operator == "substract"){ + document.getElementById('ans').value = substract(num1, num2); + } + else if (operator == "mul"){ + document.getElementById('ans').value = mul(num1,num2); + } + else if (operator == "div"){ + if(num2===0){ + document.getElementById('ans').value = "ERROR" + } + else{ + document.getElementById('ans').value = div(num1,num2); + } + } + else { + alert("Bad operator!") + } } - var num1 = parseFloat(num1Str) - var num2 = parseFloat(num2Str) - var operator = document.getElementById('operator').value; - if (operator == "add"){ - document.getElementById('ans').value = add(num1, num2); - } - else if (operator == "substract"){ - document.getElementById('ans').value = substract(num1, num2); - } - else { - alert("Bad operator!") - } - } } diff --git a/package.json b/package.json index d727f6d..b77db86 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,9 @@ "author": "Askeing Yen & Shing Lyu", "license": "MPLv2", "dependencies": { - "mocha": "~2.3.2" + "mocha": "~2.3.2", + "sinon": "~1.17.2" }, "repository":{} + } diff --git a/selenium_test.js b/selenium_test.js new file mode 100644 index 0000000..c00ec4c --- /dev/null +++ b/selenium_test.js @@ -0,0 +1,17 @@ +var webdriver = require('selenium-webdriver'); +var assert = require('assert'); + +var driver = new webdriver.Builder(). + forBrowser('chrome').build(); + driver.get("http://127.0.0.1:8000/") + var searchBox=driver.findElement(webdriver.By.id('num1')); + searchBox.sendKeys('1'); + var searchBox2=driver.findElement(webdriver.By.id('num2')); + searchBox2.sendKeys('1'); + driver.findElement(webdriver.By.id('go_btn')).click(); + var searchBoxResult = driver.findElement(webdriver.By.id('ans')); + searchBoxResult.getAttribute('value').then(function(value){ + console.log(value); + assert.equal(value,'22'); + }); + diff --git a/test/test_calculator.js b/test/test_calculator.js index 13f6205..3d7a5a8 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -1,10 +1,95 @@ var assert = require('assert'); var cal = require('../calculator.js') +var sinon=require('sinon') + +//var webdriver = require('selenium-webdriver'); +//webdriver = require('selenium-webdriver'); +//var driver =undefined; describe('Calculator', function(){ - describe('add', function(){ - it('add', function(){ - assert.equal('2', cal.add(1, 1)); + describe('add', function(){ + it('add', function(){ + assert.equal('2', cal.add(1, 1)); + }) + it('add_decimal', function(){ + assert.equal('2.2', cal.add(1.1, 1.1)); + }) + it('add_neg', function(){ + assert.equal('-2', cal.add(-1,-1)); + }) + it('add_neg', function(){ + assert.equal('-2', cal.add(-1,-1)); + }) + + }) + describe('sub', function(){ + it('sub', function(){ + assert.equal('0', cal.substract(1, 1)); + }) + it('sub_decimal',function(){ + assert.equal('0',cal.substract(1.1,1.1)); + }) + it('sub_neg',function(){ + assert.equal('0',cal.substract(-13.15,-13.15)); + }) + }) + describe('mul', function(){ + it('mul', function(){ + assert.equal('1', cal.mul(1, 1)); + }) + + it('mul_decimal', function(){ + assert.equal('65', cal.mul(0.65,100)); + }) + it('mul_neg', function(){ + assert.equal('-11', cal.mul(-11, 1)); + }) + }) + describe('div', function(){ + it('div', function(){ + assert.equal('1', cal.div(1, 1)); + }) + it('div_decimal', function(){ + assert.equal('1', cal.div(1.1, 1.1)); + }) + it('div_neg', function(){ + assert.equal('-1', cal.div(-1, 1)); + }) + }) + + describe('negative_test', function(){ + it('shold be false', function(){ + assert.equal(false, cal.isNumber('a')); + assert.equal(true, cal.isNumber(1)); + }) + }) + describe('sinon_test',function(){ + it('Sinon test',function(){ + var stub = sinon.stub(Math,"random").returns(39); + + assert.equal(39,cal.mul(Math.random(),1)); + }) }) - }) + + + /* describe('selenium-webdriver',function(){ + it('selenium-test',function(done){ + var driver = new webdriver.Builder(). + forBrowser('chrome').build(); + driver.get("http://127.0.0.1:8000/") + var searchBox=driver.findElement(webdriver.By.id('num1')); + searchBox.sendKeys('1'); + var searchBox2=driver.findElement(webdriver.By.id('num2')); + searchBox2.sendKeys('1'); + driver.findElement(webdriver.By.id('go_btn')).click(); + var searchBoxResult = driver.findElement(webdriver.By.id('ans')); + searchBoxResult.getAttribute('value').then(function(value){ + console.log(value); + assert.equal(value,'3'); + }); + //driver.quit(); + }) + })*/ + + })