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

102502034 #31

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_js:
before_install:
- "npm install -g mocha"
- "npm install chai"
- "npm install selenium-webdriver"

notifications:
email: false
12 changes: 10 additions & 2 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function isNumber(numStr){
var re = /^[0-9\.]+$/;
var re=/^[+-]?[0-9]+\.{0,1}[0-9]*$/
return re.test(numStr);
}

Expand All @@ -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;
6 changes: 4 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
<h1>Silly Calculator</h1>
<input type="text" name="" id="num1" value="1" />
<select id="operator" size="1">
<option value="add">+</option>
<option value="substract">-</option>
<option value="add">+</option>
<option value="substract">-</option>
<option value="mul">×</option>
<option value="div">÷</option>
</select>
<input type="text" id="num2" value="1" />
<div>=</div>
Expand Down
51 changes: 31 additions & 20 deletions main.js
Original file line number Diff line number Diff line change
@@ -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!")
}
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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":{}

}
17 changes: 17 additions & 0 deletions selenium_test.js
Original file line number Diff line number Diff line change
@@ -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');
});

93 changes: 89 additions & 4 deletions test/test_calculator.js
Original file line number Diff line number Diff line change
@@ -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();
})
})*/


})