Permalink
Browse files

A simple calculator command line node application that accepts two po…

…sitive numerical arguments and sums them together.
  • Loading branch information...
allenevans committed Dec 24, 2015
1 parent 60da6d2 commit 2f2df9a4deefad6846c3f544c6cad6a48a84c7dc
Showing with 94 additions and 3 deletions.
  1. +24 −0 calc.js
  2. +12 −3 package.json
  3. +38 −0 spec/calc.spec.js
  4. +9 −0 spec/helpers/setProcessArgs.js
  5. +11 −0 spec/support/jasmine.json
View
24 calc.js
@@ -0,0 +1,24 @@
/*
* File : calc.js
* Description : A calculator node command line interface app.
* ------------------------------------------------------------------------------------------------ */
var argv = require('minimist')(process.argv.slice(2));
/**
* Sum two numbers together
* @param {number} num1
* @param {number} num2
* @returns {number}
*/
function sum(num1, num2) {
return num1 + num2;
}
var num1 = argv._[0];
var num2 = argv._[1];
if (isNaN(num1) || isNaN(num2)) {
throw 'Arguments must be valid numbers';
}
console.log(sum(num1, num2));
View
@@ -2,9 +2,9 @@
"name": "node-githooks-precommit-blog",
"version": "0.0.1",
"description": "Git: Node Pre-commit Hooks Supporting Blog Application",
"main": "app.js",
"main": "calc.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha --colors spec/**/*.spec.js"
},
"keywords": [
"git",
@@ -15,5 +15,14 @@
"hooks"
],
"author": "Allen Evans",
"license": "ISC"
"license": "ISC",
"dependencies": {
"chai": "^3.4.1",
"minimist": "^1.2.0",
"mocha": "^2.3.4",
"sinon": "^1.17.2"
},
"devDependencies": {
"sinon-chai": "^2.8.0"
}
}
View
@@ -0,0 +1,38 @@
/*
* File : calc.spec.js
* Description : Calculator tests.
* ------------------------------------------------------------------------------------------------ */
var sinon = require('sinon');
var chai = require('chai');
var sinonChai = require("sinon-chai");
var expect = chai.expect;
chai.use(sinonChai);
var setProcessArgs = require('./helpers/setProcessArgs');
describe('calc app', function () {
beforeEach(function () {
// reset module cache.
var resolvedName = require.resolve('../calc');
delete require.cache[resolvedName];
sinon.spy(console, 'log');
});
afterEach(function () {
console.log.restore();
});
it('should add two numbers together', function () {
setProcessArgs(3, 4);
require('../calc');
expect(console.log).to.have.been.calledWith(7);
});
it('should add not accept non-numerical strings', function () {
setProcessArgs('+', 4);
expect(function () { require('../calc'); }).to.throw();
});
});
@@ -0,0 +1,9 @@
/*
* File : setProcessArgs.js
* Description : Adds / resets arguments passed on the command line.
* ------------------------------------------------------------------------------------------------ */
module.exports = function setProcessArgs() {
// first two arguments are 'node' and the script name, we need to preserve these.
if (process.argv.length > 2) { process.argv.splice(2 - process.argv.length); }
for (var i = 0; i < arguments.length; i += 1) { process.argv.push(arguments[i]); }
};
View
@@ -0,0 +1,11 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*.[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}

0 comments on commit 2f2df9a

Please sign in to comment.