added prompt test and fixed four issues
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Marty Nelson · committed by Marty Nelson · parent f91f203 · Browse files · 2 files·+131 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+131 added−8 removed
- tests:+121 added−0 removed
- other:+10 added−8 removed
Provenance
- Commit
eea372f- Tree
4bd7f5e- Author
Marty Nelson <marty.nelson@yahoo.com>- Committer
Marty Nelson <marty.nelson@yahoo.com>- Parent
f91f203
2 files changed+131 added−8 removed
modified:lib/commander.js+10 added-8 removed
710 unmodified lines711 * @api private712 */713 714 Command.prototype.promptForNumber = function(str, fn){715+ var self = this;715- this.promptSingleLine(str, function(val){716+ this.promptSingleLine(str, function parseNumber(val){717 val = Number(val);717- if (isNaN(val)) return program.promptForNumber(str + '(must be a number) ', fn);718+ if (isNaN(val)) return self.promptSingleLine(str + '(must be a number) ', parseNumber);719 fn(val);720 });721 };722 6 unmodified lines729 */730 731 Command.prototype.promptForDate = function(str, fn){732 var self = this;732- this.promptSingleLine(str, function(val){733+ this.promptSingleLine(str, function parseDate(val){734 val = new Date(val);734- if (isNaN(val.getTime())) return self.promptForDate(str + '(must be a date) ', fn);735+ if (isNaN(val.getTime())) return self.promptSingleLine(str + '(must be a date) ', parseDate);736 fn(val);737 });738 };739 25 unmodified lines765 * @api private766 */767 768 Command.prototype.promptMultiLine = function(str, fn){768- var buf = '';769+ var buf = [];770 console.log(str);771 process.stdin.setEncoding('utf8');772 process.stdin.on('data', function(val){773+ //node on windows returns '\r\n'772- if ('\n' == val) {774+ if ('\n' == val || '\r\n' == val) {775 process.stdin.removeAllListeners('data');774- fn(buf);776+ fn(buf.join('\n'));777 } else {776- buf += val;778+ buf.push(val.replace(/\s+$/, ''));779 }780 }).resume();781 };782 210 unmodified linesadded:test/test.prompt.js+121 added
1+/**2+ * Module dependencies.3+ */4+5+var events = require('events')6+ , program = require('../')7+ , should = require('should');8+9+//mock stdin on process10+var stdin = new events.EventEmitter();11+stdin.setEncoding = stdin.resume = function() {};12+stdin.write = function(data) { stdin.emit('data', data); };13+process.__defineGetter__('stdin', function() { return stdin });14+15+//mock stdout on process16+var stdout = new events.EventEmitter();17+stdout.write = function(data) { this.emit('data', data) };18+stdout.expect = function(expected, fn) {19+ var actual = '';20+ this.once('data', function(data){ actual = data; });21+ fn();22+ actual.should.equal(expected);23+}24+//var realOut = process.stdout;25+process.__defineGetter__('stdout', function() { return stdout });26+27+var count = 0;28+var test = function(expected) {29+ count++;30+ return function(actual) {31+ count--;32+ if(expected instanceof Date) {33+ //need something better :(34+ //using `new Date(date.toString())` round trip35+ //loosing some precision for .getTime()36+ actual.should.be.an.instanceof(Date);37+ } else {38+ actual.should.equal(expected);39+ }40+ }41+}42+43+process.on('exit', function(){44+ count.should.equal(0);45+})46+47+/* Single Line String Prompt Tests */48+var prompt = 'hello '49+ , expected = 'world';50+51+// simple single line string 52+stdout.expect(prompt, function(){53+ program.prompt(prompt, test(expected));54+ stdin.write(expected); 55+})56+// with trim57+stdout.expect(prompt, function(){58+ program.prompt(prompt, test(expected));59+ stdin.write(' ' + expected + ' '); 60+})61+// newline62+stdout.expect(prompt, function(){63+ program.prompt(prompt, test(''));64+ stdin.write('\n'); 65+})66+67+/* Multiline Tests*/68+var prompt = 'A multiline';69+stdout.expect(prompt + '\n', function() {70+ program.prompt(prompt, test('hello world\n and what a world\nit could be'));71+ stdin.write('hello world\n');72+ stdin.write(' and what a world\n');73+ stdin.write('it could be\n');74+ stdin.write('\n');75+})76+77+/* Date Prompt Tests */78+var prompt = 'A date '79+ , expected = new Date();80+ 81+// simple date82+stdout.expect(prompt, function(){83+ program.prompt(prompt, Date, test(expected));84+ stdin.write(expected.toString()); 85+})86+// not a date twice87+stdout.expect(prompt, function() {88+ program.prompt(prompt, Date, test(expected));89+ 90+ stdout.expect(prompt + '(must be a date) ', function() {91+ stdin.write('blue'); 92+ })93+ stdout.expect(prompt + '(must be a date) ', function() {94+ stdin.write('blue'); 95+ })96+ stdin.write(expected.toString()); 97+})98+99+/* Number Prompt Tests*/100+var prompt = 'A number '101+ , expected = 12;102+ 103+//simple number104+stdout.expect(prompt, function(){105+ program.prompt(prompt, Number, test(expected));106+ stdin.write(expected.toString()); 107+})108+109+// not a number twice110+stdout.expect(prompt, function() {111+ program.prompt(prompt, Number, test(expected));112+ 113+ stdout.expect(prompt + '(must be a number) ', function() {114+ stdin.write('blue'); 115+ })116+ stdout.expect(prompt + '(must be a number) ', function() {117+ stdin.write('blue'); 118+ })119+ stdin.write(expected.toString()); 120+})121+