Skip to content

Commit

Permalink
allow functions as defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Deverell committed Apr 26, 2016
1 parent bbd0593 commit 3e0856b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ prompt.stop = function () {
if (prompt.stopped || !prompt.started) {
return;
}

stdin.destroy();
prompt.emit('stop');
prompt.stopped = true;
Expand Down Expand Up @@ -515,6 +515,13 @@ prompt.getInput = function (prop, callback) {
//
prompt.emit('prompt', prop);

//
// If defaultLine is a function, execute it and store it back to defaultLine
//
if(typeof defaultLine === 'function') {
defaultLine = defaultLine();
}

//
// If there is no default line, set it to an empty string
//
Expand Down
10 changes: 10 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ helpers.schema = {
message: 'riffwabbles can only be letters, numbers, and dashes',
default: 'foobizzles'
},
functiondefaulttest: {
message: 'function default test',
default: function () {
return 'test';
}
},
functiondefaultundefined: {
message: 'function default undefined',
default: function () { }
},
number: {
type: 'number',
message: 'pick a number, any number',
Expand Down
65 changes: 62 additions & 3 deletions test/prompt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ vows.describe('prompt').addBatch({
"the get() method": {
"with a simple string prompt": {
"that is a property name in prompt.properties": {
"with a default value": {
"with a string literal default value": {
topic: function () {
var that = this;

Expand All @@ -383,8 +383,8 @@ vows.describe('prompt').addBatch({
},
"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.notStrictEqual(this.msg.indexOf('riffwabbles'), -1);
assert.notStrictEqual(this.msg.indexOf('(foobizzles)'), -1);
assert.include(result, 'riffwabbles');
assert.equal(result['riffwabbles'], schema.properties['riffwabbles'].default);
}
Expand All @@ -393,6 +393,65 @@ vows.describe('prompt').addBatch({
}
}
}
}).addBatch({
"When using prompt": {
"the get() method": {
"with a simple string prompt": {
"that is a property name in prompt.properties": {
"with a function default returning a string literal": {
topic: function () {
var that = this;

helpers.stdout.once('data', function (msg) {
that.msg = msg;
});

prompt.properties.functiondefaulttest = schema.properties.functiondefaulttest;
prompt.get('functionDefaultTest', this.callback);
helpers.stdin.writeNextTick('\n');
},
"should respond with the default value function's return value": function (err, result) {
assert.isNull(err);
assert.notStrictEqual(this.msg.indexOf('function default test'), -1);
assert.notStrictEqual(this.msg.indexOf('(test)'), -1);
assert.include(result, 'functionDefaultTest');
assert.strictEqual(result['functionDefaultTest'], 'test');
}
},
}
}
}
}
}).addBatch({
"When using prompt": {
"the get() method": {
"with a simple string prompt": {
"that is a property name in prompt.properties": {
"with a function default that returns undefined": {
topic: function () {
var that = this;

helpers.stdout.once('data', function (msg) {
that.msg = msg;
});

prompt.properties.functiondefaultundefined =
schema.properties.functiondefaultundefined;
prompt.get('functionDefaultUndefined', this.callback);
helpers.stdin.writeNextTick('\n');
},
"should prompt without a default value": function (err, result) {
assert.isNull(err);
assert.notStrictEqual(this.msg.indexOf('function default undefined'), -1);
assert.strictEqual(this.msg.indexOf('('), -1);
assert.include(result, 'functionDefaultUndefined');
assert.strictEqual(result['functionDefaultUndefined'], '');
}
},
}
}
}
}
}).addBatch({
"When using prompt": {
"the get() method": {
Expand Down

0 comments on commit 3e0856b

Please sign in to comment.