-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cb1de1
commit f5944f1
Showing
3 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
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'); | ||
} |
This file contains 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
This file contains 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
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(); | ||
}); | ||
}); | ||
}); |