UNPKG

2.85 kBJavaScriptView Raw
1// Load modules
2
3var Lab = require('lab');
4var Topo = require('..');
5
6
7// Declare internals
8
9var internals = {};
10
11
12// Test shortcuts
13
14var lab = exports.lab = Lab.script();
15var describe = lab.describe;
16var it = lab.it;
17var expect = Lab.expect;
18
19
20describe('Topo', function () {
21
22 var testDeps = function (scenario) {
23
24 var topo = new Topo();
25 scenario.forEach(function (record, i) {
26
27 var options = record.before || record.after || record.group ? { before: record.before, after: record.after, group: record.group } : null;
28 topo.add(record.id, options);
29 });
30
31 return topo.nodes.join('');
32 };
33
34 it('sorts dependencies', function (done) {
35
36 var scenario = [
37 { id: '0', before: 'a' },
38 { id: '1', after: 'f', group: 'a' },
39 { id: '2', before: 'a' },
40 { id: '3', before: ['b', 'c'], group: 'a' },
41 { id: '4', after: 'c', group: 'b' },
42 { id: '5', group: 'c' },
43 { id: '6', group: 'd' },
44 { id: '7', group: 'e' },
45 { id: '8', before: 'd' },
46 { id: '9', after: 'c', group: 'a' }
47 ];
48
49 expect(testDeps(scenario)).to.equal('0213547869');
50 done();
51 });
52
53 it('sorts dependencies (seq)', function (done) {
54
55 var scenario = [
56 { id: '0' },
57 { id: '1' },
58 { id: '2' },
59 { id: '3' }
60 ];
61
62 expect(testDeps(scenario)).to.equal('0123');
63 done();
64 });
65
66 it('sorts dependencies (explicit)', function (done) {
67
68 var set = '0123456789abcdefghijklmnopqrstuvwxyz';
69 var array = set.split('');
70
71 var scenario = [];
72 for (var i = 0, il = array.length; i < il; ++i) {
73 var item = {
74 id: array[i],
75 group: array[i],
76 after: i ? array.slice(0, i) : [],
77 before: array.slice(i + 1)
78 };
79 scenario.push(item);
80 }
81
82 var fisherYates = function (array) {
83
84 var i = array.length;
85 while (--i) {
86 var j = Math.floor(Math.random() * (i + 1));
87 var tempi = array[i];
88 var tempj = array[j];
89 array[i] = tempj;
90 array[j] = tempi;
91 }
92 };
93
94 fisherYates(scenario);
95 expect(testDeps(scenario)).to.equal(set);
96 done();
97 });
98
99 it('throws on circular dependency', function (done) {
100
101 var scenario = [
102 { id: '0', before: 'a', group: 'b' },
103 { id: '1', before: 'c', group: 'a' },
104 { id: '2', before: 'b', group: 'c' }
105 ];
106
107 expect(function () {
108
109 testDeps(scenario);
110 }).to.throw('item added into group c created a dependencies error');
111
112 done();
113 });
114});