Skip to content
Mickael Daniel edited this page Jan 30, 2013 · 2 revisions

test-base

yeoman.generators.Base

generator.appname

should be set with the project directory name without non-alphanums.

assert.equal(this.dummy.appname, "temp dev");

generator.run(args, cb)

should run all methods in the given generator.

this.dummy.run();

should have the _running flag turned on.

assert.ok(this.dummy._running);

generator.runHooks(cb)

should go through all registered hooks, and invoke them in series.

this.dummy.runHooks(function (err) {
  if (err) {
    return err;
  }
  fs.stat('app/scripts/models/application-model.js', done);
});

generator.argument(name, config)

should add a new argument to the generator instance.

assert.equal(this.dummy._arguments.length, 0);
this.dummy.argument('foo');
assert.equal(this.dummy._arguments.length, 1);

should create the property specified with value from positional args.

assert.equal(this.dummy.foo, 'bar');

should slice positional arguments when config.type is Array.

this.dummy.argument('bar', {
  type: Array
});

assert.deepEqual(this.dummy.bar, ['baz', 'bom']);

generator.option(name, config)

should add a new option to the set of generator expected options.

// every generator have the --help options
var generator = new this.Dummy([], {
  env: this.env,
  resolved: 'test'
});

assert.equal(generator._options.length, 1);
generator.option('foo');
assert.equal(generator._options.length, 2);
assert.deepEqual(generator._options.pop(), {
  desc: 'Description for foo',
  name: 'foo',
  type: Boolean,
  defaults: false,
  hide: false
});

generator.hookFor(name, config)

should emit errors if called when running.

try {
  this.dummy.hookFor('maoow');
} catch (err) {
  assert.equal(err.message, 'hookFor must be used within the constructor only');
}

should create the macthing option.

this.dummy._running = false;
this.dummy.hookFor('something');
assert.deepEqual(this.dummy._options.pop(), {
  desc: 'Something to be invoked',
  name: 'something',
  type: Boolean,
  defaults: 'else',
  hide: false
});

should update the internal hooks holder.

assert.deepEqual(this.dummy._hooks.pop(), {
  name: 'something'
});

generator.defaultFor(config)

should return the value for the option name, doing lookup in options and Grunt config.

var name = this.dummy.defaultFor('something');
assert.equal(name, 'else');

generator.desc(decription)

should update the internal description.

this.dummy.desc('A new desc for this generator');
assert.equal(this.dummy.description, 'A new desc for this generator');

generator.help()

should return the expected help / usage output.

this.dummy.option('ooOoo');
var help = this.dummy.help();

assert.ok(help.match('Usage:'));
assert.ok(help.match('yeoman init FOO one two three \\[options\\]'));
assert.ok(help.match('A new desc for this generator'));
assert.ok(help.match('Options:'));
assert.ok(help.match('--help   # Print generator\'s options and usage'));
assert.ok(help.match('--ooOoo  # Description for ooOoo'));

generator.usage()

should return the expected help / usage output.

var usage = this.dummy.usage();
assert.equal(usage, 'yeoman init FOO one two three [options]\n\nA new desc for this generator');