f03f353158c7bd31dda0509b0eb56aee53a5e8b0.svn-base
2.33 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
/*
* helpers.js: Test helpers for the prompt tests.
*
* (C) 2010, Nodejitsu Inc.
*
*/
var events = require('events'),
stream = require('stream'),
util = require('util'),
prompt = require('../lib/prompt');
var helpers = exports;
var MockReadWriteStream = helpers.MockReadWriteStream = function () {
//
// No need to do anything here, it's just a mock.
//
};
util.inherits(MockReadWriteStream, events.EventEmitter);
['resume', 'pause', 'setEncoding', 'flush'].forEach(function (method) {
MockReadWriteStream.prototype[method] = function () { /* Mock */ };
});
MockReadWriteStream.prototype.write = function (msg) {
this.emit('data', msg);
};
//
// Create some mock streams for asserting against
// in our prompt tests.
//
helpers.stdin = new MockReadWriteStream();
helpers.stdout = new MockReadWriteStream();
helpers.stderr = new MockReadWriteStream();
//
// Monkey punch `util.error` to silence console output
// and redirect to helpers.stderr for testing.
//
util.error = function () {
helpers.stderr.write.apply(helpers.stderr, arguments);
}
helpers.properties = {
riffwabbles: {
name: 'riffwabbles',
validator: /^[\w|\-]+$/,
warning: 'riffwabbles can only be letters, numbers, and dashes',
default: 'foobizzles'
},
username: {
name: 'username',
validator: /^[\w|\-]+$/,
warning: 'Username can only be letters, numbers, and dashes'
},
notblank: {
name: 'notblank',
empty: false
},
password: {
name: 'password',
hidden: true,
empty: false
},
badValidator: {
name: 'bad-validator',
validator: ['cant', 'use', 'array']
},
animal: {
name: 'animal',
description: 'Enter an animal',
default: 'dog',
validator: /dog|cat/
},
sound: {
name: 'sound',
description: 'What sound does this animal make?',
validator: function (value) {
var animal = prompt.history(0).value;
return animal === 'dog' && value === 'woof'
|| animal === 'cat' && value === 'meow';
}
},
fnvalidator: {
name: 'fnvalidator',
validator: function (line) {
return line.slice(0,2) == 'fn';
},
warning: 'fnvalidator must start with "fn"'
},
cbvalidator: {
name: 'cbvalidator',
validator: function (line, next) {
next(line.slice(0,2) == 'cb');
},
warning: 'cbvalidator must start with "cb"'
}
};