This repository was archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10-gulp-di.js
298 lines (260 loc) · 7.84 KB
/
10-gulp-di.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'use strict';
const Stream = require('stream');
const path = require('path');
describe('GulpDI', () => {
const PI = Math.PI;
const RAD_TO_DEG = 180 / PI;
const INSTANCE_OPTIONS = { parentDir: basePath() };
/**
* Module declaration which assembles the "toDeg" function using PI and
* RAD_TO_DEG
*
* @method toDegModule
* @param {number} PI
* @param {number} RAD_TO_DEG
* @return {function}
*/
function toDegModule (PI, RAD_TO_DEG) {
return (radValue) => radValue * RAD_TO_DEG;
}
let gulp = null;
let di = null;
describe('Initialization', () => {
beforeEach(() => {
gulp = getGulpInstance();
di = null;
});
it('initializes', () => {
di = getDiInstance(gulp).resolve();
});
it('throws an error without gulp as first argument', () => {
assert.throws(() => getDiInstance());
});
it('byId throws an error when a dependency was not found', () => {
assert.throws(getDiInstance(gulp).byId);
});
it('byId with noError does not throw an error when a dependency was not found', () => {
di = getDiInstance(gulp).resolve();
let info = di.byId('UnknownDependency_', true);
assert.equal(info, null);
});
it('byId throws an error when a undesolved dependency was requested', () => {
assert.throws(() => {
di = getDiInstance(gulp, INSTANCE_OPTIONS)
.module('PI', './contrib/examples/pi')
.byId('PI');
});
});
it('provide', () => {
di = getDiInstance(gulp)
.provide('test', 'test')
.resolve();
assert.equal(di.byId('test'), 'test');
});
it('options.noModules', () => {
di = getDiInstance(gulp, { noModules: true }).resolve();
assert.throws(() => di.byId('_'));
assert.throws(() => di.byId('chalk'));
assert.throws(() => di.byId('gutil'));
assert.throws(() => di.byId('log'));
});
it('options.noBuiltin', () => {
di = getDiInstance(gulp, { noBuiltin: true }).resolve();
assert.throws(() => di.byId('basePath'));
assert.throws(() => di.byId('Package'));
});
it('options.noHelp', () => {
di = getDiInstance(gulp, { noHelp: true }).resolve();
assert.ok(!hasTask(gulp, 'help'));
});
});
describe('Dependency injection', () => {
it('throws an error when using invalid notation', () => {
di = getDiInstance(gulp);
assert.throws(() => {
di.task();
});
assert.throws(() => {
di.module('test', null);
});
assert.throws(() => {
di.task('null');
});
assert.throws(() => {
di.module('test', 0);
});
});
it('module', () => {
di = getDiInstance(gulp)
.provide('PI', PI)
.provide('RAD_TO_DEG', RAD_TO_DEG)
.module('toDeg', toDegModule);
di.resolve();
let toDeg = di.byId('toDeg');
let pi = di.byId('PI');
assert.ok(toDeg);
assert.equal(typeof toDeg, 'function');
assert.equal(typeof pi, 'number');
assert.equal(toDeg(pi), 180);
assert.equal(toDeg(2 * pi), 360);
});
it('module with relative and absolute paths', () => {
di = getDiInstance(gulp, INSTANCE_OPTIONS)
.module('PI', './contrib/examples/pi')
.module('DEG_TO_RAD', './contrib/examples/deg-to-rad')
.module('RAD_TO_DEG', './contrib/examples/rad-to-deg')
.module('toDeg', './contrib/examples/to-deg')
.module('toRad', path.join(__dirname, '..', 'contrib/examples/to-rad'));
di.resolve();
let toDeg = di.byId('toDeg');
let toRad = di.byId('toRad');
let pi = di.byId('PI');
assert.equal(typeof toDeg, 'function');
assert.equal(typeof toRad, 'function');
assert.equal(toDeg(pi), 180);
assert.equal(toDeg(2 * pi), 360);
assert.equal(toRad(180), pi);
assert.equal(toRad(360), 2 * pi);
});
it('module throws an error with invalid paths', () => {
assert.throws(() => {
di = getDiInstance(gulp)
.module('PI', './throws/an/error');
});
});
it('modules', (done) => {
di = getDiInstance(gulp, INSTANCE_OPTIONS)
.modules('./modules')
.resolve();
let paths = di.byId('paths');
assert.equal(typeof paths, 'object');
done();
});
it('inject (chainable)', (done) => {
di = getDiInstance(gulp)
.provide('PI', PI)
.provide('RAD_TO_DEG', RAD_TO_DEG)
.module('toDeg', toDegModule)
.resolve();
let injectCalled = 0;
di.inject((toDeg, PI) => {
++injectCalled;
assert.equal(toDeg(PI), 180);
assert.equal(toDeg(2 * PI), 360);
})
.inject(() => {
assert.equal(injectCalled, 1);
done();
});
});
it('inject (w/ return value)', () => {
di = getDiInstance(gulp)
.provide('PI', PI)
.provide('RAD_TO_DEG', RAD_TO_DEG)
.module('toDeg', toDegModule)
.resolve();
let injectCalled = 0;
let returnValue = di.inject((toDeg, PI) => {
++injectCalled;
return toDeg(2 * PI);
}, true);
assert.equal(injectCalled, 1);
assert.equal(returnValue, 360);
});
it('throws an error when a missing dependency is declared', () => {
di = getDiInstance(gulp)
.task(function (test) {});
assert.throws(() => di.resolve());
});
});
describe('Tasks', () => {
it('task', (done) => {
di = getDiInstance(gulp)
.provide('PI', PI)
.provide('RAD_TO_DEG', RAD_TO_DEG)
.module('toDeg', toDegModule)
.provide('test', 'test')
.task((test, toDeg, PI) => {
assert.equal(test, 'test');
assert.equal(toDeg(PI), 180);
assert.equal(toDeg(2 * PI), 360);
done();
})
.resolve();
});
it('task with an object', (done) => {
di = getDiInstance(gulp)
.task({
'first': () => {
done();
}
})
.resolve();
});
it('tasks', (done) => {
di = getDiInstance(gulp, INSTANCE_OPTIONS)
.modules('./modules')
.tasks('./tasks')
.resolve();
di.inject((gulp) => {
assert.ok(hasTask(gulp, 'semistandard'), 'has "semistandard" task');
done();
});
});
it('includes gulp and can run a task', (done) => {
let taskCalled = false;
di = getDiInstance(gulp)
.task((gulp, PI, toDeg) => {
assert.ok(gulp);
gulp.task('default', (cb) => {
taskCalled = true;
assert.equal(toDeg(2 * PI), 360);
cb();
});
})
.module('toDeg', toDegModule)
.provide('PI', PI)
.provide('RAD_TO_DEG', RAD_TO_DEG)
.resolve();
gulp.series('default', (cb) => {
assert.equal(taskCalled, true);
cb();
done();
})();
});
it('can concatenate all spec files', (done) => {
let di = getDiInstance(gulp, { parentDir: basePath(), lazy: false });
let s = new Stream.Transform();
let l = 0;
let count = 0;
let ended = false;
s.write = function (file) {
++count;
l += file._contents.length;
};
s.on('end', () => {
ended = true;
});
di.task((gulp, concat) => {
gulp.task('concat', () => {
/**
* This task comment should appear in this test
* and it might have multiple lines
*/
return gulp.src('test/**/*.js')
.pipe(concat('all_specs.js'))
.pipe(s);
// console.log('The concat command', concat)
});
});
di.resolve();
gulp.series('concat', (cb) => {
assert.ok(ended, 'Stream was closed');
assert.ok(l > 0, 'Read more than zero bytes');
assert.equal(count, 1, 'Solely one file written');
cb();
done();
})();
});
});
});