Skip to content

Commit

Permalink
Merge pull request #153 from devrelm/devrelm.function-defaults
Browse files Browse the repository at this point in the history
Allow functions as defaults
  • Loading branch information
gangstead committed Aug 5, 2016
2 parents 3046431 + a9792b8 commit e493cb8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/prompt.js
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
16 changes: 16 additions & 0 deletions test/helpers.js
Expand Up @@ -99,6 +99,22 @@ helpers.schema = {
message: 'riffwabbles can only be letters, numbers, and dashes',
default: 'foobizzles'
},
functiondefaultpluralanimal: {
message: 'function default plural animal',
default: function () {
return prompt.history('animal').value + 's';
}
},
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
101 changes: 98 additions & 3 deletions test/prompt-test.js
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,101 @@ 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 returning a string literal": {
topic: function () {
var that = this;

helpers.stdout.once('data', function (msg) {
// we really need the second message, so we'll
// ignore the first and look out for the second
helpers.stdout.once('data', function (msg) {
that.msg = msg;
})
});

prompt.properties.animal = schema.properties.animal;
prompt.properties.functiondefaultpluralanimal =
schema.properties.functiondefaultpluralanimal;
prompt.get(['animal', 'functiondefaultpluralanimal'], this.callback);
helpers.stdin.writeSequence(['cat\n', '\n']);
},
"should respond with the default value function's return value": function (err, result) {
assert.isNull(err);
assert.notStrictEqual(this.msg.indexOf('function default plural animal'), -1);
assert.notStrictEqual(this.msg.indexOf('(cats)'), -1);
assert.strictEqual(result['animal'], 'cat');
assert.include(result, 'functiondefaultpluralanimal');
assert.strictEqual(result['functiondefaultpluralanimal'], 'cats');
}
},
}
}
}
}
}).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 e493cb8

Please sign in to comment.