1 | var assert = require('assert');
|
2 |
|
3 | var wagner = require('../');
|
4 |
|
5 | describe('core', function() {
|
6 | afterEach(function() {
|
7 | wagner.clear();
|
8 | });
|
9 |
|
10 | it('works', function(done) {
|
11 | wagner.task('tristan', function(callback) {
|
12 | setTimeout(function() {
|
13 | callback(null, 'tristan');
|
14 | }, 50);
|
15 | });
|
16 |
|
17 | wagner.invokeAsync(function(error, tristan) {
|
18 | assert.ok(!error);
|
19 | assert.equal(tristan, 'tristan');
|
20 | done();
|
21 | }, {});
|
22 | });
|
23 |
|
24 | it('returns a promise', function(done) {
|
25 | wagner.task('tristan', function(callback) {
|
26 | setTimeout(function() {
|
27 | callback(null, 'tristan');
|
28 | }, 50);
|
29 | });
|
30 |
|
31 | var promise = wagner.invokeAsync(function(error, tristan) {
|
32 | return tristan;
|
33 | }, {});
|
34 |
|
35 | promise.then(function(v) {
|
36 | assert.equal(v, 'tristan');
|
37 | done();
|
38 | });
|
39 | });
|
40 |
|
41 | it('error', function(done) {
|
42 | wagner.task('tristan', function(callback) {
|
43 | setTimeout(function() {
|
44 | callback(null, 'tristan');
|
45 | }, 50);
|
46 | });
|
47 |
|
48 | wagner.task('isolde', function(callback) {
|
49 | setTimeout(function() {
|
50 | callback('I got an error');
|
51 | }, 25);
|
52 | });
|
53 |
|
54 | wagner.invokeAsync(function(error, tristan, isolde) {
|
55 | assert.equal(error, 'I got an error');
|
56 | assert.ok(!tristan);
|
57 | assert.ok(!isolde);
|
58 | done();
|
59 | });
|
60 | });
|
61 |
|
62 | it('sync', function(done) {
|
63 | wagner.task('tristan', function() {
|
64 | return 'tristan';
|
65 | });
|
66 |
|
67 | wagner.task('isolde', function() {
|
68 | return 'isolde';
|
69 | });
|
70 |
|
71 | var e;
|
72 | var t;
|
73 | var i;
|
74 | var returnValue = wagner.invoke(function(error, tristan, isolde) {
|
75 | e = error;
|
76 | t = tristan;
|
77 | i = isolde;
|
78 |
|
79 | return 'done';
|
80 | });
|
81 |
|
82 | assert.ok(!e);
|
83 | assert.equal(t, 'tristan');
|
84 | assert.equal(i, 'isolde');
|
85 | assert.equal(returnValue, 'done');
|
86 | process.nextTick(function() {
|
87 | done();
|
88 | });
|
89 | });
|
90 |
|
91 | it('sync errors', function(done) {
|
92 | wagner.task('tristan', function() {
|
93 | return 'tristan';
|
94 | });
|
95 |
|
96 | wagner.task('isolde', function() {
|
97 | throw 'Problem!';
|
98 | });
|
99 |
|
100 | assert.throws(function() {
|
101 | var returnValue = wagner.invoke(function(error, tristan, isolde) {
|
102 | return 'done';
|
103 | });
|
104 | }, 'Problem!');
|
105 |
|
106 | done();
|
107 | });
|
108 |
|
109 | it('recursive', function(done) {
|
110 | wagner.task('tristan', function(callback) {
|
111 | setTimeout(function() {
|
112 | callback(null, 'tristan');
|
113 | }, 50);
|
114 | });
|
115 |
|
116 | wagner.task('isolde', function(tristan, callback) {
|
117 | setTimeout(function() {
|
118 | callback(null, tristan + ' & isolde');
|
119 | }, 25);
|
120 | });
|
121 |
|
122 | wagner.invokeAsync(function(error, tristan, isolde) {
|
123 | assert.ok(!error);
|
124 | assert.equal(tristan, 'tristan');
|
125 | assert.equal(isolde, 'tristan & isolde');
|
126 | done();
|
127 | });
|
128 | });
|
129 |
|
130 | it('without error', function(done) {
|
131 | wagner.task('tristan', function(callback) {
|
132 | setTimeout(function() {
|
133 | callback(null, 'tristan');
|
134 | }, 50);
|
135 | });
|
136 |
|
137 | wagner.task('isolde', function(tristan, callback) {
|
138 | setTimeout(function() {
|
139 | callback(null, tristan + ' & isolde');
|
140 | }, 25);
|
141 | });
|
142 |
|
143 | wagner.invokeAsync(function(tristan, isolde) {
|
144 | assert.equal(tristan, 'tristan');
|
145 | assert.equal(isolde, 'tristan & isolde');
|
146 | done();
|
147 | });
|
148 | });
|
149 |
|
150 | it('factory', function(done) {
|
151 | wagner.factory('nothung', function() {
|
152 | return { from: 'barnstokkr' };
|
153 | });
|
154 |
|
155 | wagner.task('sigfried', function(nothung, callback) {
|
156 | setTimeout(function() {
|
157 | callback(null, { sword: nothung });
|
158 | }, 25);
|
159 | });
|
160 |
|
161 | wagner.invokeAsync(function(error, sigfried) {
|
162 | assert.ok(!error);
|
163 | assert.equal(sigfried.sword.from, 'barnstokkr');
|
164 | done();
|
165 | });
|
166 | });
|
167 |
|
168 | it('only executes necessary tasks', function(done) {
|
169 | wagner.factory('nothung', function() {
|
170 | return { from: 'barnstokkr' };
|
171 | });
|
172 |
|
173 | wagner.task('sigfried', function(nothung, callback) {
|
174 | setTimeout(function() {
|
175 | callback(null, { sword: nothung });
|
176 | }, 25);
|
177 | });
|
178 |
|
179 | var sigmund = 0;
|
180 | wagner.task('sigmund', function(nothung, callback) {
|
181 | ++sigmund;
|
182 | setTimeout(function() {
|
183 | callback(null, { sword: nothung });
|
184 | }, 25);
|
185 | });
|
186 |
|
187 | wagner.invokeAsync(function(error, sigfried) {
|
188 | assert.ok(!error);
|
189 | assert.equal(sigfried.sword.from, 'barnstokkr');
|
190 | assert.ok(!sigmund);
|
191 | done();
|
192 | });
|
193 | });
|
194 |
|
195 | it('locals', function(done) {
|
196 | wagner.task('sigfried', function(nothung, callback) {
|
197 | setTimeout(function() {
|
198 | callback(null, { sword: nothung });
|
199 | }, 25);
|
200 | });
|
201 |
|
202 | wagner.invokeAsync(
|
203 | function(error, sigfried) {
|
204 | assert.ok(!error);
|
205 | assert.equal(sigfried.sword.from, 'barnstokkr');
|
206 | done();
|
207 | },
|
208 | { nothung: { from: 'barnstokkr' } });
|
209 | });
|
210 |
|
211 | it('async errors', function(done) {
|
212 | wagner.task('sigfried', function(callback) {
|
213 | throw 'Problem!';
|
214 | });
|
215 |
|
216 | wagner.invokeAsync(
|
217 | function(error, sigfried) {
|
218 | assert.ok(error);
|
219 | assert.ok(!sigfried);
|
220 | assert.equal(error, 'Problem!');
|
221 | done();
|
222 | });
|
223 | });
|
224 | });
|
225 |
|
226 | describe('parallel', function() {
|
227 | it('works', function(done) {
|
228 | wagner.parallel(
|
229 | { first: 'parsifal', second: 'gotterdammerung' },
|
230 | function(value, key, callback) {
|
231 | callback(null, value.toUpperCase());
|
232 | },
|
233 | function(error, results) {
|
234 | assert.ok(!error);
|
235 | assert.equal(results.first.result, 'PARSIFAL');
|
236 | assert.equal(results.second.result, 'GOTTERDAMMERUNG');
|
237 | done();
|
238 | });
|
239 | });
|
240 |
|
241 | it('returns errors', function(done) {
|
242 | wagner.parallel(
|
243 | { first: 'parsifal', second: 'gotterdammerung' },
|
244 | function(value, key, callback) {
|
245 | callback(key + ' invalid');
|
246 | },
|
247 | function(error, results) {
|
248 | assert.ok(!!error);
|
249 | assert.equal(2, error.length);
|
250 | assert.ok(error.indexOf('first invalid') !== -1);
|
251 | assert.ok(error.indexOf('second invalid') !== -1);
|
252 | assert.equal(results.first.error, 'first invalid');
|
253 | assert.equal(results.second.error, 'second invalid');
|
254 | done();
|
255 | });
|
256 | });
|
257 |
|
258 | it('catches errors', function(done) {
|
259 | wagner.parallel(
|
260 | { first: 'parsifal', second: 'gotterdammerung' },
|
261 | function(value, key, callback) {
|
262 | throw key + ' invalid';
|
263 | },
|
264 | function(error, results) {
|
265 | assert.ok(!!error);
|
266 | assert.equal(2, error.length);
|
267 | assert.ok(error.indexOf('first invalid') !== -1);
|
268 | assert.ok(error.indexOf('second invalid') !== -1);
|
269 | assert.equal(results.first.error, 'first invalid');
|
270 | assert.equal(results.second.error, 'second invalid');
|
271 | done();
|
272 | });
|
273 | });
|
274 | });
|
275 |
|
276 | describe('series', function() {
|
277 | it('works', function(done) {
|
278 | wagner.series(
|
279 | ['parsifal', 'gotterdammerung'],
|
280 | function(value, index, callback) {
|
281 | callback(null, value.toUpperCase());
|
282 | },
|
283 | function(error, results) {
|
284 | assert.ok(!error);
|
285 | assert.equal(results[0], 'PARSIFAL');
|
286 | assert.equal(results[1], 'GOTTERDAMMERUNG');
|
287 | done();
|
288 | });
|
289 | });
|
290 |
|
291 | it('catches errors', function(done) {
|
292 | wagner.series(
|
293 | ['parsifal', 'gotterdammerung'],
|
294 | function(value, index, callback) {
|
295 | if (index > 0) {
|
296 | throw value;
|
297 | } else {
|
298 | callback(null, value);
|
299 |
|
300 | }
|
301 | },
|
302 | function(error, results) {
|
303 | assert.ok(!!error);
|
304 | assert.ok(!results);
|
305 | assert.equal(error.error, 'gotterdammerung');
|
306 | assert.equal(error.index, 1);
|
307 | done();
|
308 | });
|
309 | });
|
310 | });
|
311 |
|
312 | describe('modules', function() {
|
313 | it('works', function(done) {
|
314 | var foods = wagner.module('foods');
|
315 | foods.factory('bacon', function() {
|
316 | return 'bacon';
|
317 | });
|
318 | foods.factory('eggs', function() {
|
319 | return 'eggs';
|
320 | });
|
321 |
|
322 | var breakfast = wagner.module('breakfast', ['foods']);
|
323 | breakfast.invoke(function(error, bacon, eggs) {
|
324 | assert.ok(!error);
|
325 | assert.equal(bacon, 'bacon');
|
326 | assert.equal(eggs, 'eggs');
|
327 | done();
|
328 | });
|
329 | });
|
330 | }); |
\ | No newline at end of file |