forked from hapijs/topo
/
index.js
executable file
·137 lines (103 loc) · 3.52 KB
/
index.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
// Load modules
var Lab = require('lab');
var Topo = require('..');
// Declare internals
var internals = {};
// Test shortcuts
var expect = Lab.expect;
var before = Lab.before;
var after = Lab.after;
var describe = Lab.experiment;
var it = Lab.test;
describe('Topo', function () {
var testDeps = function (scenario) {
var topo = new Topo();
scenario.forEach(function (record, i) {
var options = record.before || record.after || record.group ? { before: record.before, after: record.after, group: record.group } : null;
topo.add(record.id, options);
});
return topo.nodes.join('');
};
it('sorts dependencies', function (done) {
var scenario = [
{ id: '0', before: 'a' },
{ id: '1', after: 'f', group: 'a' },
{ id: '2', before: 'a' },
{ id: '3', before: ['b', 'c'], group: 'a' },
{ id: '4', after: 'c', group: 'b' },
{ id: '5', group: 'c' },
{ id: '6', group: 'd' },
{ id: '7', group: 'e' },
{ id: '8', before: 'd' },
{ id: '9', after: 'c', group: 'a' }
];
expect(testDeps(scenario)).to.equal('0213547869');
done();
});
it('sorts dependencies (seq)', function (done) {
var scenario = [
{ id: '0' },
{ id: '1' },
{ id: '2' },
{ id: '3' }
];
expect(testDeps(scenario)).to.equal('0123');
done();
});
it('sorts dependencies (explicit)', function (done) {
var set = '0123456789abcdefghijklmnopqrstuvwxyz';
var array = set.split('');
var scenario = [];
for (var i = 0, il = array.length; i < il; ++i) {
var item = {
id: array[i],
group: array[i],
after: i ? array.slice(0, i) : [],
before: array.slice(i + 1)
};
scenario.push(item);
}
var fisherYates = function (array) {
var i = array.length;
while (--i) {
var j = Math.floor(Math.random() * (i + 1));
var tempi = array[i];
var tempj = array[j];
array[i] = tempj;
array[j] = tempi;
}
};
fisherYates(scenario);
expect(testDeps(scenario)).to.equal(set);
done();
});
it('throws on circular dependency', function (done) {
var scenario = [
{ id: '0', before: 'a', group: 'b' },
{ id: '1', before: 'c', group: 'a' },
{ id: '2', before: 'b', group: 'c' }
];
expect(function () {
testDeps(scenario);
}).to.throw('item added into group c created a dependencies error');
done();
});
it('allows a user to add enumerable properties to the Array object', function (done) {
var scenario = [
{ id: '0', before: 'a' },
{ id: '1', after: 'f', group: 'a' },
{ id: '2', before: 'a' },
{ id: '3', before: ['b', 'c'], group: 'a' },
{ id: '4', after: 'c', group: 'b' },
{ id: '5', group: 'c' },
{ id: '6', group: 'd' },
{ id: '7', group: 'e' },
{ id: '8', before: 'd' },
{ id: '9', after: 'c', group: 'a' }
];
Array.prototype.bogusProperty = 'gotcha!';
expect(testDeps(scenario)).to.equal('0213547869');
done();
delete Array.prototype.bogusProperty;
});
});