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