2 * prompt-test.js: Tests for prompt.
4 * (C) 2010, Nodejitsu Inc.
8 var assert = require('assert'),
9 vows = require('vows'),
10 prompt = require('../lib/prompt'),
11 helpers = require('./helpers');
13 vows.describe('prompt').addBatch({
14 "When using prompt": {
17 // Reset the prompt for mock testing
19 prompt.started = false;
22 stdout: helpers.stdout
27 "the readLine() method": {
29 prompt.readLine(this.callback);
30 helpers.stdin.write('testing\n');
32 "should respond with data from the stdin stream": function (err, input) {
34 assert.equal(input, 'testing');
37 "the readLineHidden() method": {
38 "when given backspaces": {
40 prompt.readLineHidden(this.callback);
41 helpers.stdin.write('no-\x08backspace.\x7f');
42 helpers.stdin.write('\n');
44 "should remove the proper characters": function (err,input) {
46 assert.equal(input, 'nobackspace');
50 prompt.readLineHidden(this.callback);
51 helpers.stdin.write('testing');
52 helpers.stdin.write('\r\n');
54 "should respond with data from the stdin stream": function (err, input) {
56 assert.equal(input, 'testing');
59 "the getInput() method": {
60 "with a simple string prompt": {
63 helpers.stdout.once('data', function (msg) {
67 prompt.getInput('test input', this.callback);
68 helpers.stdin.write('test value\n');
70 "should prompt to stdout and respond with data": function (err, input) {
72 assert.equal(input, 'test value');
73 assert.isTrue(this.msg.indexOf('test input') !== -1);
76 "with any field that is not supposed to be empty": {
77 "and we don't provide any input": {
80 helpers.stdout.once('data', function (msg) {
84 helpers.stderr.once('data', function (msg) {
88 prompt.getInput(helpers.properties.notblank, function () {});
89 prompt.once('invalid', this.callback.bind(null, null))
90 helpers.stdin.write('\n');
93 "should prompt with an error": function (ign, prop, input) {
94 assert.isObject(prop);
95 assert.equal(input, '');
96 assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1);
97 assert.isTrue(this.msg.indexOf('notblank') !== -1);
101 "with a hidden field that is not supposed to be empty": {
102 "and we provide valid input": {
105 helpers.stdout.once('data', function (msg) {
109 prompt.getInput('password', this.callback);
110 helpers.stdin.write('trustno1\n');
113 "should prompt to stdout and respond with data": function (err, input) {
115 assert.equal(input, 'trustno1');
116 assert.isTrue(this.msg.indexOf('password') !== -1);
119 "and we don't provide an input": {
122 helpers.stdout.once('data', function (msg) {
126 helpers.stderr.once('data', function (msg) {
130 prompt.getInput(helpers.properties.password, function () {});
131 prompt.once('invalid', this.callback.bind(null, null))
132 helpers.stdin.write('\n');
134 "should prompt with an error": function (ign, prop, input) {
135 assert.isObject(prop);
136 assert.equal(input, '');
137 assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1);
138 assert.isTrue(this.msg.indexOf('password') !== -1);
142 "with a complex property prompt": {
143 "and a valid input": {
146 helpers.stdout.once('data', function (msg) {
150 prompt.getInput(helpers.properties.username, this.callback);
151 helpers.stdin.write('some-user\n');
153 "should prompt to stdout and respond with data": function (err, input) {
155 assert.equal(input, 'some-user');
156 assert.isTrue(this.msg.indexOf('username') !== -1);
159 "and an invalid input": {
162 helpers.stdout.once('data', function (msg) {
166 helpers.stderr.once('data', function (msg) {
170 prompt.getInput(helpers.properties.username, this.callback);
172 prompt.once('invalid', function () {
173 prompt.once('prompt', function () {
174 process.nextTick(function () {
175 helpers.stdin.write('some-user\n');
180 helpers.stdin.write('some -user\n');
182 "should prompt with an error before completing the operation": function (err, input) {
184 assert.equal(input, 'some-user');
185 assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1);
186 assert.isTrue(this.msg.indexOf('username') !== -1);
189 "with an invalid validator (array)": {
191 prompt.getInput(helpers.properties.badValidator, this.callback);
193 "should respond with an error": function (err, ign) {
194 assert.isTrue(!!err);
199 "the get() method": {
200 "with a simple string prompt": {
201 "that is not a property in prompt.properties": {
204 helpers.stdout.once('data', function (msg) {
208 prompt.get('test input', this.callback);
209 helpers.stdin.write('test value\n');
211 "should prompt to stdout and respond with the value": function (err, result) {
213 assert.include(result, 'test input');
214 assert.equal(result['test input'], 'test value');
215 assert.isTrue(this.msg.indexOf('test input') !== -1);
218 "that is a property name in prompt.properties": {
219 "with a default value": {
223 helpers.stdout.once('data', function (msg) {
227 prompt.properties['riffwabbles'] = helpers.properties['riffwabbles'];
228 prompt.get('riffwabbles', this.callback);
229 helpers.stdin.write('\n');
231 "should prompt to stdout and respond with the default value": function (err, result) {
233 assert.isTrue(this.msg.indexOf('riffwabbles') !== -1);
234 assert.isTrue(this.msg.indexOf('(foobizzles)') !== -1);
235 assert.include(result, 'riffwabbles');
236 assert.equal(result['riffwabbles'], helpers.properties['riffwabbles'].default);
239 "with a sync function validator": {
243 helpers.stdout.once('data', function (msg) {
247 prompt.get(helpers.properties.fnvalidator, this.callback);
248 helpers.stdin.write('fn123\n');
250 "should accept a value that is checked": function (err, result) {
252 assert.equal(result['fnvalidator'],'fn123');
255 "with a callback validator": {
259 helpers.stdout.once('data', function (msg) {
263 prompt.get(helpers.properties.cbvalidator, this.callback);
264 helpers.stdin.write('cb123\n');
266 "should not accept a value that is correct": function (err, result) {
268 assert.equal(result['cbvalidator'],'cb123');
273 "skip prompt with prompt.overide": {
275 prompt.override = { coconihet: 'whatever' }
276 prompt.get('coconihet', this.callback);
278 "skips prompt and uses overide": function (err, results) {
279 assert.equal(results.coconihet, 'whatever')
283 "the addProperties() method": {
285 prompt.addProperties({}, ['foo', 'bar'], this.callback);
286 helpers.stdin.write('foo\n');
287 helpers.stdin.write('bar\n');
289 "should add the properties to the object": function (err, obj) {
291 assert.isObject(obj);
292 assert.equal(obj.foo, 'foo');
293 assert.equal(obj.bar, 'bar');
298 "When using prompt": {
299 "the history() method": {
300 "when used inside of a complex property": {
301 "with correct value(s)": {
303 prompt.get([helpers.properties.animal, helpers.properties.sound], this.callback);
304 helpers.stdin.write('dog\n');
305 helpers.stdin.write('woof\n');
307 "should respond with the values entered": function (err, result) {
309 assert.equal(result.animal, 'dog');
310 assert.equal(result.sound, 'woof');
313 "with an incorrect value": {
315 prompt.get([helpers.properties.animal, helpers.properties.sound], function () {});
316 prompt.once('invalid', this.callback.bind(null, null));
317 helpers.stdin.write('dog\n');
318 helpers.stdin.write('meow\n');
320 "should prompt for the error": function (ign, property, line) {
321 assert.equal(property.name, 'sound');
322 assert.equal(line, 'meow');
329 "when using prompt": {
332 // Reset the prompt for mock testing
334 prompt.started = false;
336 stdin: helpers.stdin,
337 stdout: helpers.stdout
342 "the get() method": {
344 prompt.override = { xyz: 468, abc: 123 }
345 prompt.get(['xyz', 'abc'], this.callback);
347 "should respond with overrides": function (err, results) {
349 assert.deepEqual(results, { xyz: 468, abc: 123 });
354 "when using prompt": {
357 // Reset the prompt for mock testing
359 prompt.started = false;
361 stdin: helpers.stdin,
362 stdout: helpers.stdout
367 "with fancy properties": {
368 "the get() method": {
370 prompt.override = { UVW: 5423, DEF: 64235 }
373 message: 'a custom message',
377 message: 'a custom message',
381 "should respond with overrides": function (err, results) {
383 assert.deepEqual(results, { UVW: 5423, DEF: 64235 });