Skip to content

Commit

Permalink
added cli prime checker
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeKittens committed Jun 30, 2014
1 parent 8cb1de1 commit f5944f1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
30 changes: 30 additions & 0 deletions bin/prime
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

'use strict';

var program = require('commander')
, version = require('../package.json').version;

var isPrime = require('../lib/prime');

program
.version(version)
.parse(process.argv);

if (!program.args || !program.args.length) {
console.error('No number provided');
process.exit(1);
}

var number = Number(program.args[0]);

if (isNaN(number)) {
console.error('Expected number, got ' + program.args[0]);
process.exit(1);
}

if (isPrime(number)) {
console.log(number + ' is prime');
} else {
console.log(number + ' is not prime');
}
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"name": "interview",
"version": "0.1.1",
"version": "0.1.2",
"description": "A collection of command line tools for snarky answers to generic interview questions",
"main": "bin/fizzbuzz",
"bin": {
"itv-fizz": "bin/fizzbuzz",
"itv-anagrams": "bin/anagrams",
"itv-fib": "bin/fib",
"itv-fizzbuzz": "bin/fizzbuzz",
"itv-strrev": "bin/strrev",
"itv-prime": "bin/prime",
"itv-fib": "bin/fib",
"itv-anagrams": "bin/anagrams"
"itv-strrev": "bin/strrev"
},
"preferGlobal": true,
"scripts": {
Expand Down
51 changes: 51 additions & 0 deletions test/prime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

require('should');

var exec = require('child_process').exec;

describe('Prime', function() {
it('should correctly identify a prime number', function(done) {
exec('./bin/prime 13',
function (err, stdout, stderr) {
if (err) { return done(err); }

stdout.should.eql('13 is prime\n');
stderr.should.eql('');
done();
});
});

it('should correctly identify a non-prime number', function(done) {
exec('./bin/prime 12',
function (err, stdout, stderr) {
if (err) { return done(err); }

stdout.should.eql('12 is not prime\n');
stderr.should.eql('');
done();
});
});

it('should log an error when called with no arguments', function(done) {
exec('./bin/prime',
function (err, stdout, stderr) {
stderr.should.eql('No number provided\n');
err.code.should.eql(1);
stdout.should.eql('');

done();
});
});

it('should log an error when called with incorrect arguments', function(done) {
exec('./bin/prime eleventybillion',
function (err, stdout, stderr) {
stderr.should.eql('Expected number, got eleventybillion\n');
err.code.should.eql(1);
stdout.should.eql('');

done();
});
});
});

0 comments on commit f5944f1

Please sign in to comment.