-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.getGenerator.js
244 lines (200 loc) · 7.13 KB
/
app.getGenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'use strict';
require('mocha');
const assert = require('assert');
const Generator = require('..');
let base;
describe('.getGenerator', () => {
beforeEach(() => {
base = new Generator();
});
it('should get a generator from the base instance', () => {
base.register('abc', () => {});
const generator = base.getGenerator('abc');
assert(Generator.isGenerator(generator));
assert.equal(generator.name, 'abc');
});
it('should throw an error when a generator is not found', () => {
base.register('foo', () => {});
assert.throws(() => base.getGenerator('whatever'), /is not registered/);
assert.throws(() => base.getGenerator('foo.whatever'), /is not registered/);
});
it('should get a generator from the base instance from a nested generator', () => {
let count = 0;
base.register('abc', () => {});
base.register('xyz', app => {
app.register('one', function() {
const generator = this.base.getGenerator('abc');
assert(generator);
assert.equal(typeof generator, 'object');
assert.equal(generator.name, 'abc');
count++;
});
app.register('two', () => {
const generator = base.getGenerator('abc');
assert(generator);
assert.equal(typeof generator, 'object');
assert.equal(generator.name, 'abc');
count++;
});
app.register('three', function(three) {
const generator = three.base.getGenerator('abc');
assert(generator);
assert.equal(typeof generator, 'object');
assert.equal(generator.name, 'abc');
count++;
});
});
base.getGenerator('xyz.one');
base.getGenerator('xyz.two');
base.getGenerator('xyz.three');
assert.equal(count, 3);
});
it('should get a generator from the base instance using `this`', () => {
base.register('abc', () => {});
base.register('xyz', app => {
app.register('sub', function(sub) {
const generator = this.getGenerator('abc');
assert(generator);
assert.equal(typeof generator, 'object');
assert.equal(generator.name, 'abc');
});
});
base.getGenerator('xyz');
});
it('should get a base generator from "app" from a nested generator', () => {
base.register('abc', () => {});
base.register('xyz', app => {
app.register('sub', function(sub) {
const generator = app.getGenerator('abc');
assert(generator);
assert.equal(typeof generator, 'object');
assert.equal(generator.name, 'abc');
});
});
base.getGenerator('xyz');
});
it('should get a nested generator', () => {
base.register('abc', function(abc) {
abc.register('def', function(def) {});
});
const generator = base.getGenerator('abc.def');
assert(generator);
assert.equal(typeof generator, 'object');
assert.equal(generator.name, 'def');
});
it('should get a deeply nested generator', () => {
base.register('abc', function(abc) {
abc.register('def', function(def) {
def.register('ghi', function(ghi) {
ghi.register('jkl', function(jkl) {
jkl.register('mno', () => {});
});
});
});
});
const generator = base.getGenerator('abc.def.ghi.jkl.mno');
assert(generator);
assert.equal(typeof generator, 'object');
assert.equal(generator.name, 'mno');
});
it('should set deeply nested generators on the base instance cache', () => {
base.disableInspect();
base.register('abc', function(abc) {
this.register('def', function(def) {
this.register('ghi', function(ghi) {
this.register('jkl', function(jkl) {
this.register('mno', () => {});
});
});
});
});
base.getGenerator('abc.def.ghi.jkl.mno');
assert(base.namespaces.has('generate.abc'));
assert(base.namespaces.has('generate.abc.def'));
assert(base.namespaces.has('generate.abc.def.ghi'));
assert(base.namespaces.has('generate.abc.def.ghi.jkl'));
assert(base.namespaces.has('generate.abc.def.ghi.jkl.mno'));
});
it('should allow a generator to be called on multiple instances', () => {
const parents = [];
function generator() {
parents.push(this.parent.name);
}
base.register('one', function() {
this.register('sub', generator);
});
base.register('two', function() {
this.register('sub', generator);
});
assert.equal(typeof base.getGenerator('one.sub'), 'object');
assert.equal(typeof base.getGenerator('two.sub'), 'object');
assert.equal(base.findGenerator('one.sub').called, 1);
assert.equal(base.findGenerator('two.sub').called, 1);
});
it('should only invoke generators once by default', () => {
let called = 0;
base.register('one', function() {
this.register('child', function() {
called++;
});
});
// _shouldn't_ be called by ".findGenerator"
const wrappedFn = base.findGenerator('one.child');
assert.equal(called, 0);
assert.equal(base.findGenerator('one.child').called, 0);
// _should_ be called by ".getGenerator"
base.getGenerator('one.child');
assert.equal(called, 1);
assert.equal(base.findGenerator('one.child').called, called);
wrappedFn.call(base, base);
assert.equal(called, 1);
assert.equal(base.findGenerator('one.child').called, called);
wrappedFn.call(base, base);
assert.equal(called, 1);
assert.equal(base.findGenerator('one.child').called, called);
});
it('should invoke generator functions multiple times when options.once is false', () => {
let called = 0;
base.register('one', function() {
this.register('child', { once: false }, function() {
called++;
});
});
// _shouldn't_ be called by ".findGenerator"
const wrappedFn = base.findGenerator('one.child');
assert.equal(called, 0);
assert.equal(base.findGenerator('one.child').called, 0);
// _should_ be called by ".getGenerator"
base.getGenerator('one.child');
assert.equal(called, 1);
assert.equal(base.findGenerator('one.child').called, called);
wrappedFn.call(base, base);
assert.equal(called, 2);
assert.equal(base.findGenerator('one.child').called, called);
wrappedFn.call(base, base);
assert.equal(called, 3);
assert.equal(base.findGenerator('one.child').called, called);
});
it('should keep the original (unwrapped) function on generator.fn', () => {
let called = 0;
base.register('one', function() {
this.register('child', function() {
called++;
});
});
// the wrapped generator function should not be called, but the
// original function will be invoked each time
const originalFn = base.findGenerator('one.child').fn;
assert.equal(called, 0);
assert.equal(base.findGenerator('one.child').called, 0);
originalFn.call(base, base);
assert.equal(called, 1);
assert.equal(base.findGenerator('one.child').called, 0);
originalFn.call(base, base);
assert.equal(called, 2);
assert.equal(base.findGenerator('one.child').called, 0);
originalFn.call(base, base);
assert.equal(called, 3);
assert.equal(base.findGenerator('one.child').called, 0);
});
});