Skip to content

Commit

Permalink
Allow passing arguments to a Generator constructor in the same way as…
Browse files Browse the repository at this point in the history
… we pass options
  • Loading branch information
SBoudrias committed Dec 2, 2016
1 parent f6f4dda commit a852f62
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ Base.prototype.parseOptions = function () {
this._arguments.forEach(function (config, index) {
var value;
if (index >= parsedOpts._.length) {
if ('default' in config) {
if (config.name in this.options) {
value = this.options[config.name];
} else if ('default' in config) {
value = config.default;
} else {
return;
Expand All @@ -332,7 +334,7 @@ Base.prototype.parseOptions = function () {
}

parsedOpts[config.name] = value;
});
}.bind(this));

// Make the parsed options available to the instance
_.extend(this.options, parsedOpts);
Expand Down
52 changes: 52 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,58 @@ describe('Base', function () {
assert.equal(this.dummy.options.foo, 'bar');
});

it('allows specifying default argument values', function () {
var Generator = Base.extend({
constructor: function () {
Base.apply(this, arguments);

this.argument('bar', {default: 'baz'});
}
});

var gen = new Generator({
env: this.env,
resolved: 'test'
});

assert.equal(gen.options.bar, 'baz');
});

it('allows specifying default argument values', function () {
var Generator = Base.extend({
constructor: function () {
Base.apply(this, arguments);

this.argument('bar', {default: 'baz'});
}
});

var gen = new Generator({
env: this.env,
resolved: 'test'
});

assert.equal(gen.options.bar, 'baz');
});

it('properly uses arguments values passed from constructor', function () {
var Generator = Base.extend({
constructor: function () {
Base.apply(this, arguments);

this.argument('bar', {default: 'baz'});
}
});

var gen = new Generator({
env: this.env,
resolved: 'test',
bar: 'foo'
});

assert.equal(gen.options.bar, 'foo');
});

it('slice positional arguments when config.type is Array', function () {
this.dummy.argument('bar', { type: Array });
assert.deepEqual(this.dummy.options.bar, ['bar', 'baz', 'bom']);
Expand Down

0 comments on commit a852f62

Please sign in to comment.