Skip to content

Commit f51f178

Browse files
committed
Merge pull request #380 from nwinkler/simplify_generators
refactor(app): Simplify subgenerator code
2 parents a967907 + 118b352 commit f51f178

File tree

11 files changed

+100
-57
lines changed

11 files changed

+100
-57
lines changed

constant/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
2-
var path = require('path');
32
var util = require('util');
43
var ScriptBase = require('../script-base.js');
5-
var angularUtils = require('../util.js');
64

75

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

1412
Generator.prototype.createServiceFiles = function createServiceFiles() {
15-
this.appTemplate('service/constant', 'scripts/services/' + this.name);
16-
this.testTemplate('spec/service', 'services/' + this.name);
17-
this.addScriptToIndex('services/' + this.name);
13+
this.generateSourceAndTest('service/constant', 'spec/service', 'services');
1814
};

controller/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
var path = require('path');
32
var util = require('util');
43
var ScriptBase = require('../script-base.js');
54

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

1918
Generator.prototype.createControllerFiles = function createControllerFiles() {
20-
this.appTemplate('controller', 'scripts/controllers/' + this.name);
21-
this.testTemplate('spec/controller', 'controllers/' + this.name);
22-
this.addScriptToIndex('controllers/' + this.name);
19+
this.generateSourceAndTest('controller', 'spec/controller', 'controllers');
2320
};

directive/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
2-
var path = require('path');
32
var util = require('util');
43
var ScriptBase = require('../script-base.js');
5-
var angularUtils = require('../util.js');
64

75

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

1412
Generator.prototype.createDirectiveFiles = function createDirectiveFiles() {
15-
this.appTemplate('directive', 'scripts/directives/' + this.name);
16-
this.testTemplate('spec/directive', 'directives/' + this.name);
17-
this.addScriptToIndex('directives/' + this.name);
13+
this.generateSourceAndTest('directive', 'spec/directive', 'directives');
1814
};

factory/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
2-
var path = require('path');
32
var util = require('util');
43
var ScriptBase = require('../script-base.js');
5-
var angularUtils = require('../util.js');
64

75

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

1412
Generator.prototype.createServiceFiles = function createServiceFiles() {
15-
this.appTemplate('service/factory', 'scripts/services/' + this.name);
16-
this.testTemplate('spec/service', 'services/' + this.name);
17-
this.addScriptToIndex('services/' + this.name);
13+
this.generateSourceAndTest('service/factory', 'spec/service', 'services');
1814
};

filter/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
2-
var path = require('path');
32
var util = require('util');
43
var ScriptBase = require('../script-base.js');
5-
var angularUtils = require('../util.js');
64

75

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

1412
Generator.prototype.createFilterFiles = function createFilterFiles() {
15-
this.appTemplate('filter', 'scripts/filters/' + this.name);
16-
this.testTemplate('spec/filter', 'filters/' + this.name);
17-
this.addScriptToIndex('filters/' + this.name);
13+
this.generateSourceAndTest('filter', 'spec/filter', 'filters');
1814
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"load-grunt-tasks": "~0.1.0",
3737
"marked": "~0.2.8",
3838
"semver": "~2.1.0",
39+
"underscore.string": "~2.3.1",
3940
"grunt-release": "~0.5.1"
4041
},
4142
"engines": {

provider/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
2-
var path = require('path');
32
var util = require('util');
43
var ScriptBase = require('../script-base.js');
5-
var angularUtils = require('../util.js');
64

75

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

1412
Generator.prototype.createServiceFiles = function createServiceFiles() {
15-
this.appTemplate('service/provider', 'scripts/services/' + this.name);
16-
this.testTemplate('spec/service', 'services/' + this.name);
17-
this.addScriptToIndex('services/' + this.name);
13+
this.generateSourceAndTest('service/provider', 'spec/service', 'services');
1814
};

script-base.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ Generator.prototype.addScriptToIndex = function (script) {
9898
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + script + '.js ' + 'not added.\n'.yellow);
9999
}
100100
};
101+
102+
Generator.prototype.generateSourceAndTest = function (appTemplate, testTemplate, targetDirectory) {
103+
this.appTemplate(appTemplate, path.join('scripts', targetDirectory, this.name));
104+
this.testTemplate(testTemplate, path.join(targetDirectory, this.name));
105+
this.addScriptToIndex(path.join(targetDirectory, this.name));
106+
};

service/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
2-
var path = require('path');
32
var util = require('util');
43
var ScriptBase = require('../script-base.js');
5-
var angularUtils = require('../util.js');
64

75

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

1412
Generator.prototype.createServiceFiles = function createServiceFiles() {
15-
this.appTemplate('service/service', 'scripts/services/' + this.name);
16-
this.testTemplate('spec/service', 'services/' + this.name);
17-
this.addScriptToIndex('services/' + this.name);
13+
this.generateSourceAndTest('service/service', 'spec/service', 'services');
1814
};

test/test-file-creation.js

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var path = require('path');
66
var util = require('util');
77
var generators = require('yeoman-generator');
88
var helpers = require('yeoman-generator').test;
9-
9+
var _ = require('underscore.string');
1010

1111
describe('Angular generator', function () {
1212
var angular;
@@ -101,26 +101,93 @@ describe('Angular generator', function () {
101101
});
102102
});
103103

104+
/**
105+
* Generic test function that can be used to cover the scenarios where a generator is creating both a source file
106+
* and a test file. The function will run the respective generator, and then check for the existence of the two
107+
* generated files. A RegExp check is done on each file, checking for the generated content with a pattern.
108+
*
109+
* The number of parameters is quite huge due to the many options in which the generated files differ,
110+
* e.g. Services start with an upper case letter, whereas filters, directives or constants start with a lower case
111+
* letter.
112+
*
113+
* The generated items all use the dummy name 'foo'.
114+
*
115+
* @param generatorType The type of generator to run, e.g. 'filter'.
116+
* @param specType The type of the generated spec file, e.g. 'service' - all service types (constant, value, ...)
117+
* use the same Service spec template.
118+
* @param targetDirectory The directory into which the files are generated, e.g. 'directives' - this will be
119+
* located under 'app/scripts' for the sources and 'test/spec' for the tests.
120+
* @param scriptNameFn The function used to create the name of the created item, e.g. _.classify to generate 'Foo',
121+
* or _.camelize to generate 'foo'.
122+
* @param specNameFn Same as scriptNameFn, but for the describe text used in the Spec file. Some generators use
123+
* _.classify, others use _.camelize.
124+
* @param suffix An optional suffix to be appended to the generated item name, e.g. 'Ctrl' for controllers, which
125+
* will generate 'FooCtrl'.
126+
* @param done The done function.
127+
*/
128+
function generatorTest(generatorType, specType, targetDirectory, scriptNameFn, specNameFn, suffix, done) {
129+
var angularGenerator;
130+
var name = 'foo';
131+
var deps = [path.join('../..', generatorType)];
132+
angularGenerator = helpers.createGenerator('angular:' + generatorType, deps, [name]);
133+
134+
helpers.mockPrompt(angular, {
135+
bootstrap: true,
136+
compassBoostrap: true,
137+
modules: []
138+
});
139+
angular.run([], function (){
140+
angularGenerator.run([], function () {
141+
helpers.assertFiles([
142+
[path.join('app/scripts', targetDirectory, name + '.js'), new RegExp(generatorType + '\\(\'' + scriptNameFn(name) + suffix + '\'', 'g')],
143+
[path.join('test/spec', targetDirectory, name + '.js'), new RegExp('describe\\(\'' + _.classify(specType) + ': ' + specNameFn(name) + suffix + '\'', 'g')]
144+
]);
145+
done();
146+
});
147+
});
148+
}
149+
104150
describe('Controller', function () {
105151
it('should generate a new controller', function (done) {
106-
var angularCtrl;
107-
var deps = ['../../controller'];
108-
angularCtrl = helpers.createGenerator('angular:controller', deps, ['foo']);
152+
generatorTest('controller', 'controller', 'controllers', _.classify, _.classify, 'Ctrl', done);
153+
});
154+
});
109155

110-
helpers.mockPrompt(angular, {
111-
bootstrap: true,
112-
compassBoostrap: true,
113-
modules: []
114-
});
115-
angular.run([], function () {
116-
angularCtrl.run([], function () {
117-
helpers.assertFiles([
118-
['app/scripts/controllers/foo.js', /controller\('FooCtrl'/],
119-
['test/spec/controllers/foo.js', /describe\('Controller: FooCtrl'/]
120-
]);
121-
done();
122-
});
123-
});
156+
describe('Directive', function () {
157+
it('should generate a new directive', function (done) {
158+
generatorTest('directive', 'directive', 'directives', _.camelize, _.camelize, '', done);
159+
});
160+
});
161+
162+
describe('Filter', function () {
163+
it('should generate a new filter', function (done) {
164+
generatorTest('filter', 'filter', 'filters', _.camelize, _.camelize, '', done);
165+
});
166+
});
167+
168+
describe('Service', function () {
169+
function serviceTest (generatorType, nameFn, done) {
170+
generatorTest(generatorType, 'service', 'services', nameFn, _.classify, '', done);
171+
};
172+
173+
it('should generate a new constant', function (done) {
174+
serviceTest('constant', _.camelize, done);
175+
});
176+
177+
it('should generate a new service', function (done) {
178+
serviceTest('service', _.classify, done);
179+
});
180+
181+
it('should generate a new factory', function (done) {
182+
serviceTest('factory', _.camelize, done);
183+
});
184+
185+
it('should generate a new provider', function (done) {
186+
serviceTest('provider', _.camelize, done);
187+
});
188+
189+
it('should generate a new value', function (done) {
190+
serviceTest('value', _.camelize, done);
124191
});
125192
});
126193

0 commit comments

Comments
 (0)