Skip to content
Myr

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

2 files+131 added−8 removed

modified:lib/commander.js+10 added-8 removed
710 unmodified lines
711 * @api private712 */713714Command.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 lines
729 */730731Command.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 lines
765 * @api private766 */767768Command.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 lines
added:test/test.prompt.js+121 added
1/**2 * Module dependencies.3 */45var events = require('events')6 , program = require('../')7 , should = require('should');89//mock stdin on process10var stdin = new events.EventEmitter();11stdin.setEncoding = stdin.resume = function() {};12stdin.write = function(data) { stdin.emit('data', data); };13process.__defineGetter__('stdin', function() { return stdin });1415//mock stdout on process16var stdout = new events.EventEmitter();17stdout.write = function(data) { this.emit('data', data) };18stdout.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;25process.__defineGetter__('stdout', function() { return stdout });2627var count = 0;28var 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}4243process.on('exit', function(){44 count.should.equal(0);45})4647/* Single Line String Prompt Tests */48var prompt = 'hello '49 , expected = 'world';5051// simple single line string 52stdout.expect(prompt, function(){53 program.prompt(prompt, test(expected));54 stdin.write(expected); 55})56// with trim57stdout.expect(prompt, function(){58 program.prompt(prompt, test(expected));59 stdin.write(' ' + expected + ' '); 60})61// newline62stdout.expect(prompt, function(){63 program.prompt(prompt, test(''));64 stdin.write('\n'); 65})6667/* Multiline Tests*/68var prompt = 'A multiline';69stdout.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})7677/* Date Prompt Tests */78var prompt = 'A date '79 , expected = new Date();80 81// simple date82stdout.expect(prompt, function(){83 program.prompt(prompt, Date, test(expected));84 stdin.write(expected.toString()); 85})86// not a date twice87stdout.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})9899/* Number Prompt Tests*/100var prompt = 'A number '101 , expected = 12;102 103//simple number104stdout.expect(prompt, function(){105 program.prompt(prompt, Number, test(expected));106 stdin.write(expected.toString()); 107})108109// not a number twice110stdout.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