diff options
author | Minteck <contact@minteck.org> | 2021-12-21 16:50:49 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2021-12-21 16:50:49 +0100 |
commit | 20204baf1807825af4798ad03bfb329e4da05bc5 (patch) | |
tree | 1568515fa1e4592206ed5d2327b39e6b443cbd03 /node_modules/prompt/test/helpers.js | |
download | bingoloto-remote-20204baf1807825af4798ad03bfb329e4da05bc5.tar.gz bingoloto-remote-20204baf1807825af4798ad03bfb329e4da05bc5.tar.bz2 bingoloto-remote-20204baf1807825af4798ad03bfb329e4da05bc5.zip |
Commit
Diffstat (limited to 'node_modules/prompt/test/helpers.js')
-rw-r--r-- | node_modules/prompt/test/helpers.js | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/node_modules/prompt/test/helpers.js b/node_modules/prompt/test/helpers.js new file mode 100644 index 0000000..b21021f --- /dev/null +++ b/node_modules/prompt/test/helpers.js @@ -0,0 +1,161 @@ +/* + * helpers.js: test helpers for the prompt tests. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var 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. + // + var self = this; + this.on('pipe', function (src) { + var _emit = src.emit; + src.emit = function () { + //console.dir(arguments); + _emit.apply(src, arguments); + }; + + src.on('data', function (d) { + self.emit('data', d + ''); + }) + }) +}; + +util.inherits(MockReadWriteStream, stream.Stream); + +['resume', 'pause', 'setEncoding', 'flush', 'end'].forEach(function (method) { + MockReadWriteStream.prototype[method] = function () { /* Mock */ }; +}); + +MockReadWriteStream.prototype.write = function (msg) { + this.emit('data', msg); + return true; +}; + +MockReadWriteStream.prototype.writeNextTick = function (msg) { + var self = this + process.nextTick(function () { + self.write(msg); + }); +}; + +// +// Create some mock streams for asserting against +// in our prompt teSts. +// +helpers.stdin = new MockReadWriteStream(); +helpers.stdout = new MockReadWriteStream(); +helpers.stderr = new MockReadWriteStream(); + +// +// Because `read` uses a `process.nextTick` for reading from +// stdin, it is necessary to write sequences of input with extra +// `process.nextTick` calls +// +helpers.stdin.writeSequence = function (lines) { + if (!lines || !lines.length) { + return; + } + + helpers.stdin.writeNextTick(lines.shift()); + prompt.once('prompt', function () { + process.nextTick(function () { + helpers.stdin.writeSequence(lines); + }); + }); +} + +// +// Monkey punch `util.error` to silence console output +// and redirect to helpers.stderr for testing. +// +process.stderr.write = function () { + helpers.stderr.write.apply(helpers.stderr, arguments); +} + +// 1) .properties +// 2) warning --> message +// 3) Name --> description || key +// 4) validator --> conform (fxns), pattern (regexp), format (built-in) +// 5) empty --> required +helpers.schema = { + properties: { + oldschema: { + message: 'Enter your username', + validator: /^[\w|\-]+$/, + warning: 'username can only be letters, numbers, and dashes', + empty: false + }, + riffwabbles: { + pattern: /^[\w|\-]+$/, + message: 'riffwabbles can only be letters, numbers, and dashes', + default: 'foobizzles' + }, + number: { + type: 'number', + message: 'pick a number, any number', + default: 10 + }, + integer: { + type: 'integer' + }, + boolean: { + type: 'boolean' + }, + username: { + pattern: /^[\w|\-]+$/, + message: 'Username can only be letters, numbers, and dashes' + }, + notblank: { + required: true + }, + password: { + hidden: true, + required: true + }, + badValidator: { + pattern: ['cant', 'use', 'array'] + }, + animal: { + description: 'Enter an animal', + default: 'dog', + pattern: /dog|cat/ + }, + sound: { + description: 'What sound does this animal make?', + conform: 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'; + }, + message: 'fnvalidator must start with "fn"' + }, + fnconform: { + conform: function (line) { + return line.slice(0,2) == 'fn'; + }, + message: 'fnconform must start with "fn"' + }/*, + cbvalidator: { + conform: function (line, next) { + next(line.slice(0,2) == 'cb'); + }, + message: 'cbvalidator must start with "cb"' + }*/ + } +}; |