Skip to content
This repository has been archived by the owner on Mar 26, 2018. It is now read-only.

Commit

Permalink
refactor(app): Simplify subgenerator code
Browse files Browse the repository at this point in the history
Add a common function in the base script to simplify the common tasks
for many of the generators (services, filter, directive, controller).
Also remove some unused modules from the sub-generators (path and angularUtils).
  • Loading branch information
nwinkler committed Oct 11, 2013
1 parent 0dbe896 commit 118b352
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 57 deletions.
6 changes: 1 addition & 5 deletions constant/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
var path = require('path');
var util = require('util');
var ScriptBase = require('../script-base.js');
var angularUtils = require('../util.js');


var Generator = module.exports = function Generator() {
Expand All @@ -12,7 +10,5 @@ var Generator = module.exports = function Generator() {
util.inherits(Generator, ScriptBase);

Generator.prototype.createServiceFiles = function createServiceFiles() {
this.appTemplate('service/constant', 'scripts/services/' + this.name);
this.testTemplate('spec/service', 'services/' + this.name);
this.addScriptToIndex('services/' + this.name);
this.generateSourceAndTest('service/constant', 'spec/service', 'services');
};
5 changes: 1 addition & 4 deletions controller/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
var path = require('path');
var util = require('util');
var ScriptBase = require('../script-base.js');

Expand All @@ -17,7 +16,5 @@ var Generator = module.exports = function Generator() {
util.inherits(Generator, ScriptBase);

Generator.prototype.createControllerFiles = function createControllerFiles() {
this.appTemplate('controller', 'scripts/controllers/' + this.name);
this.testTemplate('spec/controller', 'controllers/' + this.name);
this.addScriptToIndex('controllers/' + this.name);
this.generateSourceAndTest('controller', 'spec/controller', 'controllers');
};
6 changes: 1 addition & 5 deletions directive/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
var path = require('path');
var util = require('util');
var ScriptBase = require('../script-base.js');
var angularUtils = require('../util.js');


var Generator = module.exports = function Generator() {
Expand All @@ -12,7 +10,5 @@ var Generator = module.exports = function Generator() {
util.inherits(Generator, ScriptBase);

Generator.prototype.createDirectiveFiles = function createDirectiveFiles() {
this.appTemplate('directive', 'scripts/directives/' + this.name);
this.testTemplate('spec/directive', 'directives/' + this.name);
this.addScriptToIndex('directives/' + this.name);
this.generateSourceAndTest('directive', 'spec/directive', 'directives');
};
6 changes: 1 addition & 5 deletions factory/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
var path = require('path');
var util = require('util');
var ScriptBase = require('../script-base.js');
var angularUtils = require('../util.js');


var Generator = module.exports = function Generator() {
Expand All @@ -12,7 +10,5 @@ var Generator = module.exports = function Generator() {
util.inherits(Generator, ScriptBase);

Generator.prototype.createServiceFiles = function createServiceFiles() {
this.appTemplate('service/factory', 'scripts/services/' + this.name);
this.testTemplate('spec/service', 'services/' + this.name);
this.addScriptToIndex('services/' + this.name);
this.generateSourceAndTest('service/factory', 'spec/service', 'services');
};
6 changes: 1 addition & 5 deletions filter/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
var path = require('path');
var util = require('util');
var ScriptBase = require('../script-base.js');
var angularUtils = require('../util.js');


var Generator = module.exports = function Generator() {
Expand All @@ -12,7 +10,5 @@ var Generator = module.exports = function Generator() {
util.inherits(Generator, ScriptBase);

Generator.prototype.createFilterFiles = function createFilterFiles() {
this.appTemplate('filter', 'scripts/filters/' + this.name);
this.testTemplate('spec/filter', 'filters/' + this.name);
this.addScriptToIndex('filters/' + this.name);
this.generateSourceAndTest('filter', 'spec/filter', 'filters');
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"load-grunt-tasks": "~0.1.0",
"marked": "~0.2.8",
"semver": "~2.1.0",
"underscore.string": "~2.3.1",
"grunt-release": "~0.5.1"
},
"engines": {
Expand Down
6 changes: 1 addition & 5 deletions provider/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
var path = require('path');
var util = require('util');
var ScriptBase = require('../script-base.js');
var angularUtils = require('../util.js');


var Generator = module.exports = function Generator() {
Expand All @@ -12,7 +10,5 @@ var Generator = module.exports = function Generator() {
util.inherits(Generator, ScriptBase);

Generator.prototype.createServiceFiles = function createServiceFiles() {
this.appTemplate('service/provider', 'scripts/services/' + this.name);
this.testTemplate('spec/service', 'services/' + this.name);
this.addScriptToIndex('services/' + this.name);
this.generateSourceAndTest('service/provider', 'spec/service', 'services');
};
6 changes: 6 additions & 0 deletions script-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,9 @@ Generator.prototype.addScriptToIndex = function (script) {
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + script + '.js ' + 'not added.\n'.yellow);
}
};

Generator.prototype.generateSourceAndTest = function (appTemplate, testTemplate, targetDirectory) {
this.appTemplate(appTemplate, path.join('scripts', targetDirectory, this.name));
this.testTemplate(testTemplate, path.join(targetDirectory, this.name));
this.addScriptToIndex(path.join(targetDirectory, this.name));
};
6 changes: 1 addition & 5 deletions service/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
var path = require('path');
var util = require('util');
var ScriptBase = require('../script-base.js');
var angularUtils = require('../util.js');


var Generator = module.exports = function Generator() {
Expand All @@ -12,7 +10,5 @@ var Generator = module.exports = function Generator() {
util.inherits(Generator, ScriptBase);

Generator.prototype.createServiceFiles = function createServiceFiles() {
this.appTemplate('service/service', 'scripts/services/' + this.name);
this.testTemplate('spec/service', 'services/' + this.name);
this.addScriptToIndex('services/' + this.name);
this.generateSourceAndTest('service/service', 'spec/service', 'services');
};
103 changes: 85 additions & 18 deletions test/test-file-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var path = require('path');
var util = require('util');
var generators = require('yeoman-generator');
var helpers = require('yeoman-generator').test;

var _ = require('underscore.string');

describe('Angular generator', function () {
var angular;
Expand Down Expand Up @@ -101,26 +101,93 @@ describe('Angular generator', function () {
});
});

/**
* Generic test function that can be used to cover the scenarios where a generator is creating both a source file
* and a test file. The function will run the respective generator, and then check for the existence of the two
* generated files. A RegExp check is done on each file, checking for the generated content with a pattern.
*
* The number of parameters is quite huge due to the many options in which the generated files differ,
* e.g. Services start with an upper case letter, whereas filters, directives or constants start with a lower case
* letter.
*
* The generated items all use the dummy name 'foo'.
*
* @param generatorType The type of generator to run, e.g. 'filter'.
* @param specType The type of the generated spec file, e.g. 'service' - all service types (constant, value, ...)
* use the same Service spec template.
* @param targetDirectory The directory into which the files are generated, e.g. 'directives' - this will be
* located under 'app/scripts' for the sources and 'test/spec' for the tests.
* @param scriptNameFn The function used to create the name of the created item, e.g. _.classify to generate 'Foo',
* or _.camelize to generate 'foo'.
* @param specNameFn Same as scriptNameFn, but for the describe text used in the Spec file. Some generators use
* _.classify, others use _.camelize.
* @param suffix An optional suffix to be appended to the generated item name, e.g. 'Ctrl' for controllers, which
* will generate 'FooCtrl'.
* @param done The done function.
*/
function generatorTest(generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) {
var angularGenerator;
var name = 'foo';
var deps = [path.join('../..', generatorType)];
angularGenerator = helpers.createGenerator('angular:' + generatorType, deps, [name]);

helpers.mockPrompt(angular, {
bootstrap: true,
compassBoostrap: true,
modules: []
});
angular.run([], function (){
angularGenerator.run([], function () {
helpers.assertFiles([
[path.join('app/scripts', targetDirectory, name + '.js'), new RegExp(generatorType + '\\(\'' + scriptNameFn(name) + suffix + '\'', 'g')],
[path.join('test/spec', targetDirectory, name + '.js'), new RegExp('describe\\(\'' + _.classify(specType) + ': ' + specNameFn(name) + suffix + '\'', 'g')]
]);
done();
});
});
}

describe('Controller', function () {
it('should generate a new controller', function (done) {
var angularCtrl;
var deps = ['../../controller'];
angularCtrl = helpers.createGenerator('angular:controller', deps, ['foo']);
generatorTest('controller', 'controller', 'controllers', _.classify, _.classify, 'Ctrl', done);
});
});

helpers.mockPrompt(angular, {
bootstrap: true,
compassBoostrap: true,
modules: []
});
angular.run([], function () {
angularCtrl.run([], function () {
helpers.assertFiles([
['app/scripts/controllers/foo.js', /controller\('FooCtrl'/],
['test/spec/controllers/foo.js', /describe\('Controller: FooCtrl'/]
]);
done();
});
});
describe('Directive', function () {
it('should generate a new directive', function (done) {
generatorTest('directive', 'directive', 'directives', _.camelize, _.camelize, '', done);
});
});

describe('Filter', function () {
it('should generate a new filter', function (done) {
generatorTest('filter', 'filter', 'filters', _.camelize, _.camelize, '', done);
});
});

describe('Service', function () {
function serviceTest (generatorType, nameFn, done) {
generatorTest(generatorType, 'service', 'services', nameFn, _.classify, '', done);
};

it('should generate a new constant', function (done) {
serviceTest('constant', _.camelize, done);
});

it('should generate a new service', function (done) {
serviceTest('service', _.classify, done);
});

it('should generate a new factory', function (done) {
serviceTest('factory', _.camelize, done);
});

it('should generate a new provider', function (done) {
serviceTest('provider', _.camelize, done);
});

it('should generate a new value', function (done) {
serviceTest('value', _.camelize, done);
});
});

Expand Down
6 changes: 1 addition & 5 deletions value/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';
var path = require('path');
var util = require('util');
var ScriptBase = require('../script-base.js');
var angularUtils = require('../util.js');


var Generator = module.exports = function Generator() {
Expand All @@ -12,7 +10,5 @@ var Generator = module.exports = function Generator() {
util.inherits(Generator, ScriptBase);

Generator.prototype.createServiceFiles = function createServiceFiles() {
this.appTemplate('service/value', 'scripts/services/' + this.name);
this.testTemplate('spec/service', 'services/' + this.name);
this.addScriptToIndex('services/' + this.name);
this.generateSourceAndTest('service/value', 'spec/service', 'services');
};

0 comments on commit 118b352

Please sign in to comment.