57963194cf6a8fa7982598f3275b2b4eebfb79e2.svn-base
12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* prompt-test.js: Tests for prompt.
*
* (C) 2010, Nodejitsu Inc.
*
*/
var assert = require('assert'),
vows = require('vows'),
prompt = require('../lib/prompt'),
helpers = require('./helpers');
vows.describe('prompt').addBatch({
"When using prompt": {
topic: function () {
//
// Reset the prompt for mock testing
//
prompt.started = false;
prompt.start({
stdin: helpers.stdin,
stdout: helpers.stdout
});
return null;
},
"the readLine() method": {
topic: function () {
prompt.readLine(this.callback);
helpers.stdin.write('testing\n');
},
"should respond with data from the stdin stream": function (err, input) {
assert.isNull(err);
assert.equal(input, 'testing');
}
},
"the readLineHidden() method": {
"when given backspaces": {
topic: function () {
prompt.readLineHidden(this.callback);
helpers.stdin.write('no-\x08backspace.\x7f');
helpers.stdin.write('\n');
},
"should remove the proper characters": function (err,input) {
assert.isNull(err);
assert.equal(input, 'nobackspace');
}
},
topic: function () {
prompt.readLineHidden(this.callback);
helpers.stdin.write('testing');
helpers.stdin.write('\r\n');
},
"should respond with data from the stdin stream": function (err, input) {
assert.isNull(err);
assert.equal(input, 'testing');
}
},
"the getInput() method": {
"with a simple string prompt": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
})
prompt.getInput('test input', this.callback);
helpers.stdin.write('test value\n');
},
"should prompt to stdout and respond with data": function (err, input) {
assert.isNull(err);
assert.equal(input, 'test value');
assert.isTrue(this.msg.indexOf('test input') !== -1);
}
},
"with any field that is not supposed to be empty": {
"and we don't provide any input": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
});
helpers.stderr.once('data', function (msg) {
that.errmsg = msg;
});
prompt.getInput(helpers.properties.notblank, function () {});
prompt.once('invalid', this.callback.bind(null, null))
helpers.stdin.write('\n');
},
"should prompt with an error": function (ign, prop, input) {
assert.isObject(prop);
assert.equal(input, '');
assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1);
assert.isTrue(this.msg.indexOf('notblank') !== -1);
}
}
},
"with a hidden field that is not supposed to be empty": {
"and we provide valid input": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
});
prompt.getInput('password', this.callback);
helpers.stdin.write('trustno1\n');
},
"should prompt to stdout and respond with data": function (err, input) {
assert.isNull(err);
assert.equal(input, 'trustno1');
assert.isTrue(this.msg.indexOf('password') !== -1);
}
},
"and we don't provide an input": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
});
helpers.stderr.once('data', function (msg) {
that.errmsg = msg;
});
prompt.getInput(helpers.properties.password, function () {});
prompt.once('invalid', this.callback.bind(null, null))
helpers.stdin.write('\n');
},
"should prompt with an error": function (ign, prop, input) {
assert.isObject(prop);
assert.equal(input, '');
assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1);
assert.isTrue(this.msg.indexOf('password') !== -1);
}
}
},
"with a complex property prompt": {
"and a valid input": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
});
prompt.getInput(helpers.properties.username, this.callback);
helpers.stdin.write('some-user\n');
},
"should prompt to stdout and respond with data": function (err, input) {
assert.isNull(err);
assert.equal(input, 'some-user');
assert.isTrue(this.msg.indexOf('username') !== -1);
}
},
"and an invalid input": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
});
helpers.stderr.once('data', function (msg) {
that.errmsg = msg;
})
prompt.getInput(helpers.properties.username, this.callback);
prompt.once('invalid', function () {
prompt.once('prompt', function () {
process.nextTick(function () {
helpers.stdin.write('some-user\n');
})
})
});
helpers.stdin.write('some -user\n');
},
"should prompt with an error before completing the operation": function (err, input) {
assert.isNull(err);
assert.equal(input, 'some-user');
assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1);
assert.isTrue(this.msg.indexOf('username') !== -1);
}
},
"with an invalid validator (array)": {
topic: function () {
prompt.getInput(helpers.properties.badValidator, this.callback);
},
"should respond with an error": function (err, ign) {
assert.isTrue(!!err);
}
}
}
},
"the get() method": {
"with a simple string prompt": {
"that is not a property in prompt.properties": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
})
prompt.get('test input', this.callback);
helpers.stdin.write('test value\n');
},
"should prompt to stdout and respond with the value": function (err, result) {
assert.isNull(err);
assert.include(result, 'test input');
assert.equal(result['test input'], 'test value');
assert.isTrue(this.msg.indexOf('test input') !== -1);
}
},
"that is a property name in prompt.properties": {
"with a default value": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
});
prompt.properties['riffwabbles'] = helpers.properties['riffwabbles'];
prompt.get('riffwabbles', this.callback);
helpers.stdin.write('\n');
},
"should prompt to stdout and respond with the default value": function (err, result) {
assert.isNull(err);
assert.isTrue(this.msg.indexOf('riffwabbles') !== -1);
assert.isTrue(this.msg.indexOf('(foobizzles)') !== -1);
assert.include(result, 'riffwabbles');
assert.equal(result['riffwabbles'], helpers.properties['riffwabbles'].default);
}
},
"with a sync function validator": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
});
prompt.get(helpers.properties.fnvalidator, this.callback);
helpers.stdin.write('fn123\n');
},
"should accept a value that is checked": function (err, result) {
assert.isNull(err);
assert.equal(result['fnvalidator'],'fn123');
}
},
"with a callback validator": {
topic: function () {
var that = this;
helpers.stdout.once('data', function (msg) {
that.msg = msg;
});
prompt.get(helpers.properties.cbvalidator, this.callback);
helpers.stdin.write('cb123\n');
},
"should not accept a value that is correct": function (err, result) {
assert.isNull(err);
assert.equal(result['cbvalidator'],'cb123');
}
}
}
},
"skip prompt with prompt.overide": {
topic: function () {
prompt.override = { coconihet: 'whatever' }
prompt.get('coconihet', this.callback);
},
"skips prompt and uses overide": function (err, results) {
assert.equal(results.coconihet, 'whatever')
}
}
},
"the addProperties() method": {
topic: function () {
prompt.addProperties({}, ['foo', 'bar'], this.callback);
helpers.stdin.write('foo\n');
helpers.stdin.write('bar\n');
},
"should add the properties to the object": function (err, obj) {
assert.isNull(err);
assert.isObject(obj);
assert.equal(obj.foo, 'foo');
assert.equal(obj.bar, 'bar');
}
}
}
}).addBatch({
"When using prompt": {
"the history() method": {
"when used inside of a complex property": {
"with correct value(s)": {
topic: function () {
prompt.get([helpers.properties.animal, helpers.properties.sound], this.callback);
helpers.stdin.write('dog\n');
helpers.stdin.write('woof\n');
},
"should respond with the values entered": function (err, result) {
assert.isTrue(!err);
assert.equal(result.animal, 'dog');
assert.equal(result.sound, 'woof');
}
},
"with an incorrect value": {
topic: function () {
prompt.get([helpers.properties.animal, helpers.properties.sound], function () {});
prompt.once('invalid', this.callback.bind(null, null));
helpers.stdin.write('dog\n');
helpers.stdin.write('meow\n');
},
"should prompt for the error": function (ign, property, line) {
assert.equal(property.name, 'sound');
assert.equal(line, 'meow');
}
}
}
}
}
}).addBatch({
"when using prompt": {
topic: function () {
//
// Reset the prompt for mock testing
//
prompt.started = false;
prompt.start({
stdin: helpers.stdin,
stdout: helpers.stdout
});
return null;
},
"the get() method": {
topic: function () {
prompt.override = { xyz: 468, abc: 123 }
prompt.get(['xyz', 'abc'], this.callback);
},
"should respond with overrides": function (err, results) {
assert.isNull(err);
assert.deepEqual(results, { xyz: 468, abc: 123 });
}
}
}
}).addBatch({
"when using prompt": {
topic: function () {
//
// Reset the prompt for mock testing
//
prompt.started = false;
prompt.start({
stdin: helpers.stdin,
stdout: helpers.stdout
});
return null;
},
"with fancy properties": {
"the get() method": {
topic: function () {
prompt.override = { UVW: 5423, DEF: 64235 }
prompt.get([{
name:'UVW',
message: 'a custom message',
default: 6
},{
name:'DEF',
message: 'a custom message',
default: 6
}], this.callback);
},
"should respond with overrides": function (err, results) {
assert.isNull(err);
assert.deepEqual(results, { UVW: 5423, DEF: 64235 });
}
}
}
}
}).export(module);