UNPKG

11.7 kBJavaScriptView Raw
1'use strict';
2
3var assert = require('chai').assert,
4 DEPS = require('..').require('./techs/v2/deps.js.js'),
5 Deps = DEPS.Deps,
6 DepsItem = DEPS.DepsItem;
7
8/**
9 * Mocha BDD interface.
10 *
11 * @name describe @function
12 * @name it @function
13 * @name before @function
14 * @name after @function
15 * @name beforeEach @function
16 * @name afterEach @function
17 */
18
19function assertDepsParse(deps, expected) {
20 return function () {
21 assert.deepEqual(new Deps().parse(deps).serialize(), expected);
22 };
23}
24
25function assertBuildKey(item, expected) {
26 return function () {
27 assert.equal(new DepsItem(item).buildKey(), expected);
28 };
29}
30
31describe('Deps', function() {
32
33 describe('parsing:', function() {
34
35 describe('old format with names', function() {
36
37 it('block', assertDepsParse(
38 [ { name: 'b1' } ],
39 { '': { '': [ { block: 'b1' } ] } }
40 ));
41
42 it('block with elem', assertDepsParse(
43 [ { name: 'b1', elems: [ { name: 'e1' } ] } ],
44 { '': { '': [ { block: 'b1' }, { block: 'b1', elem: 'e1' } ] } }
45 ));
46
47 it('block with elem with mods with vals', assertDepsParse(
48 [
49 { name: 'b1', elems: [
50 { name: 'e1', mods: [
51 { name: 'm1', vals: [ 'v1', 'v2' ] } ] } ] }
52 ],
53 { '': { '': [
54 { block: 'b1' },
55 { block: 'b1', elem: 'e1' },
56 { block: 'b1', elem: 'e1', mod: 'm1' },
57 { block: 'b1', elem: 'e1', mod: 'm1', val: 'v1' },
58 { block: 'b1', elem: 'e1', mod: 'm1', val: 'v2' }
59 ] } }
60 ));
61
62 it('block with mods with vals and with elems', assertDepsParse(
63 [ { name: 'b1',
64 elems: [ 'e1', 'e2' ],
65 mods: [
66 { name: 'm1', val: 'v1' },
67 { name: 'm2', val: 'v2' }
68 ]
69 } ],
70 { '': { '': [
71 { block: 'b1' },
72 { block: 'b1', elem: 'e1' },
73 { block: 'b1', elem: 'e2' },
74 { block: 'b1', mod: 'm1', val: 'v1' },
75 { block: 'b1', mod: 'm2', val: 'v2' }
76 ] } }
77 ));
78
79 });
80
81 describe('new format', function() {
82
83 it('block', assertDepsParse(
84 [ { block: 'b1' } ],
85 { '': { '': [ { block: 'b1' } ] } }
86 ));
87
88 it('elem', assertDepsParse(
89 [ { block: 'b1', elem: 'e1' } ],
90 { '': { '': [ { block: 'b1', elem: 'e1' } ] } }
91 ));
92
93 it('block with shouldDeps and mustDeps', assertDepsParse(
94 [ { block: 'b1', shouldDeps: [ { block: 'b2', mustDeps: 'b3' }, 'b3' ] } ],
95 { '': { '': [ { block: 'b1' }, { block: 'b3' }, { block: 'b2' } ] } }
96 ));
97
98 it('simple blocks', assertDepsParse(
99 [ 'b1', 'b2' ],
100 { '': { '': [ { block: 'b1' }, { block: 'b2' } ] } }
101 ));
102
103 it('block with elems', assertDepsParse(
104 [ { block: 'b1', elems: [ 'e1', 'e2' ] } ],
105 { '': { '': [
106 {block: 'b1'},
107 {block: 'b1', elem: 'e1'},
108 {block: 'b1', elem: 'e2'},
109 ] } }
110 ));
111
112 it('block with elem array', assertDepsParse(
113 [ { block: 'b1', elem: ['e1', 'e2'] } ],
114 { '': {'': [
115 {block: 'b1', elem: 'e1'},
116 {block: 'b1', elem: 'e2'}
117 ] } }
118 ));
119
120 });
121
122 describe('new format with techs', function() {
123
124 it('block', assertDepsParse(
125 [ { tech: 't1', block: 'b1' } ],
126 { 't1': { 't1': [ { tech: 't1', block: 'b1' } ] } }
127 ));
128
129 it('elem', assertDepsParse(
130 [ { block: 'b1', elem: 'e1' } ],
131 { '': { '': [ { block: 'b1', elem: 'e1' } ] } }
132 ));
133
134 it('block with tech', assertDepsParse(
135 { block: 'b1', tech: 't1', shouldDeps: [ 'b2', 'b3' ], mustDeps: [ 'b0', 'b4' ] },
136 { 't1': { 't1': [
137 { block: 'b0', tech: 't1' },
138 { block: 'b4', tech: 't1' },
139 { block: 'b1', tech: 't1' },
140 { block: 'b2', tech: 't1' },
141 { block: 'b3', tech: 't1' }
142 ] } }
143 ));
144
145 it('block with techs', assertDepsParse(
146 { block: 'b1', tech: 't1', shouldDeps: { block: 'b2', tech: 't2' } },
147 { 't1': {
148 't1': [ { block: 'b1', tech: 't1' } ],
149 't2': [ { block: 'b2', tech: 't2' } ]
150 } }
151 ));
152
153 it('block with tech shortcut', assertDepsParse(
154 {block: 'b1', tech: 't1', shouldDeps: {tech: 't2'}},
155 { 't1': {
156 't1': [ { block: 'b1', tech: 't1'} ],
157 't2': [ { block: 'b1', tech: 't2'} ]
158 }
159 }
160 ));
161
162 it('block with and without tech', assertDepsParse(
163 { block: 'b1', shouldDeps: { block: 'b2', tech: 't2', shouldDeps: { block: 'b3' } } },
164 { '': {
165 '': [ { block: 'b1' } ],
166 't2': [ { block: 'b2', tech: 't2' }, { block: 'b3', tech: 't2' } ]
167 } }
168 ));
169
170 });
171
172 describe('noDeps', function() {
173
174 it('block', assertDepsParse(
175 [
176 { block: 'b1', shouldDeps: [ 'b2', 'b3' ], mustDeps: [ 'b0', 'b4' ] },
177 { block: 'b1', noDeps: ['b2', 'b4'] }
178 ],
179 { '': { '': [ { block: 'b0' }, { block: 'b1' }, { block: 'b3' } ] } }
180 ));
181
182 });
183
184 describe('include: false', function() {
185
186 it('block', assertDepsParse(
187 [
188 {
189 block: 'b1',
190 shouldDeps: [
191 { block: 'b2', include: false },
192 { block: 'b3', include: false }
193 ],
194 mustDeps: [
195 { block: 'b4', include: false },
196 { block: 'b5', include: false }
197 ]
198 },
199 { block: 'b3' },
200 { block: 'b4' }
201 ],
202 { '': { '': [ { block: 'b4' }, { block: 'b1' }, { block: 'b3' } ] } }
203 ));
204
205 });
206
207 });
208
209 describe('serialize:', function() {
210
211 describe('empty deps serialize to {}', function() {
212
213 var empty = {};
214
215 it('empty deps object: new Deps()', function() {
216 assert.deepEqual(new Deps().serialize(), empty);
217 });
218
219 it('empty object: {}', assertDepsParse({}, empty));
220
221 it('empty array: []', assertDepsParse([], empty));
222
223 it('undefined', assertDepsParse(undefined, empty));
224
225 });
226
227 });
228
229 describe('clone', function() {
230
231 var deps1, deps2, deps;
232
233 beforeEach(function(){
234 deps1 = new Deps().parse([{ block: 'b1', bla: 1 }, 'b2']);
235 deps2 = deps1.clone();
236 deps = [deps1, deps2];
237 });
238
239 it('.items', function() {
240 assert.deepEqual(deps[1].items, deps[0].items);
241 });
242
243 it('.itemsByOrder', function() {
244 assert.deepEqual(deps[1].itemsByOrder, deps[0].itemsByOrder);
245 });
246
247 });
248
249 describe('subtract', function() {
250
251 var deps1, deps2, deps3;
252
253 beforeEach(function() {
254 deps1 = new DEPS.Deps().parse([
255 { block: 'b1' },
256 { block: 'b2' },
257 { block: 'b3' },
258 { block: 'b5' }
259 ]);
260
261 deps2 = new DEPS.Deps().parse([
262 { block: 'b1' },
263 { block: 'b3' },
264 { block: 'b4' }
265 ]);
266
267 deps3 = new DEPS.Deps().parse([
268 { block: 'b5' }
269 ]);
270
271 deps1.subtract(deps2).subtract(deps3);
272 });
273
274 it('works correctly', function() {
275 assert.deepEqual(deps1.serialize(), {
276 '': {
277 '': [ { block: 'b2' } ]
278 }
279 });
280 });
281
282 });
283
284 describe('intersect', function() {
285
286 var deps1, deps2, deps3;
287
288 beforeEach(function() {
289 deps1 = new DEPS.Deps().parse([
290 { block: 'b1' },
291 { block: 'b2' },
292 { block: 'b3' },
293 { block: 'b5' }
294 ]);
295
296 deps2 = new DEPS.Deps().parse([
297 { block: 'b3' },
298 { block: 'b1' },
299 { block: 'b4' }
300 ]);
301
302 deps3 = new DEPS.Deps().parse([
303 { block: 'b3' },
304 { block: 'b6' },
305 { block: 'b1' }
306 ]);
307
308 deps1.intersect(deps2).intersect(deps3);
309 });
310
311 it('works correctly', function() {
312 assert.deepEqual(deps1.serialize(), {
313 '': {
314 '': [
315 { block: 'b1' },
316 { block: 'b3' }
317 ]
318 }
319 });
320 });
321
322 });
323
324 describe('DepsItem', function() {
325
326 describe('buildKey', function() {
327
328 it('block without tech', assertBuildKey(
329 { block: 'b1' },
330 'b1'));
331
332 it('block modifier without tech', assertBuildKey(
333 { block: 'b1', mod: 'm1' },
334 'b1_m1'));
335
336 it('block modifier value without tech', assertBuildKey(
337 { block: 'b1', mod: 'm1', val: 'v1' },
338 'b1_m1_v1'));
339
340 it('block element without tech', assertBuildKey(
341 { block: 'b1', elem: 'e1' },
342 'b1__e1'));
343
344 it('element modifier without tech', assertBuildKey(
345 { block: 'b1', elem: 'e1', mod: 'm1' },
346 'b1__e1_m1'));
347
348 it('element modifier value without tech', assertBuildKey(
349 { block: 'b1', elem: 'e1', mod: 'm1', val: 'v1' },
350 'b1__e1_m1_v1'));
351
352 it('block with tech', assertBuildKey(
353 { tech: 't1', block: 'b1' },
354 'b1.t1'));
355
356 it('block modifier with tech', assertBuildKey(
357 { tech: 't1', block: 'b1', mod: 'm1' },
358 'b1_m1.t1'));
359
360 it('block modifier value with tech', assertBuildKey(
361 { tech: 't1', block: 'b1', mod: 'm1', val: 'v1' },
362 'b1_m1_v1.t1'));
363
364 it('block element with tech', assertBuildKey(
365 { tech: 't1', block: 'b1', elem: 'e1' },
366 'b1__e1.t1'));
367
368 it('element modifier with tech', assertBuildKey(
369 { tech: 't1', block: 'b1', elem: 'e1', mod: 'm1' },
370 'b1__e1_m1.t1'));
371
372 it('element modifier value with tech', assertBuildKey(
373 { tech: 't1', block: 'b1', elem: 'e1', mod: 'm1', val: 'v1' },
374 'b1__e1_m1_v1.t1'));
375
376 it('just tech', assertBuildKey(
377 { tech: 't1' },
378 '.t1'));
379
380 it('empty', assertBuildKey(
381 {},
382 ''));
383
384 });
385
386 });
387
388});