-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.js
104 lines (95 loc) · 2.42 KB
/
queue.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
/**
* Queue class.
*
* Stores every candidate test for later execution.
*/
class Queue {
/**
* Queue constructor.
*
* Builds a new Queue and make it frozen.
*/
constructor() {
/** @private */
this.items = {};
Object.freeze(this);
Object.seal(this);
}
/**
* Enqueues a test.
*
* ```js
* const Queue = require('./queue');
* const Test = require('./test');
*
* const queue = new Queue();
* const test = new Test('file', 'item', 42, 'console.log(true)', {}, {});
*
* queue.push(test).should.equal(queue);
* ```
*
* @param {Test} test
*
* @return {Queue}
*/
push(test) {
if (!this.items[test.file]) {
this.items[test.file] = {};
}
if (!this.items[test.file][test.item]) {
this.items[test.file][test.item] = {
line: test.line,
tests: []
};
}
this.items[test.file][test.item].tests.push(test);
return this;
}
/**
* Runs all tests in the queue.
*
* ```js
* const Queue = require('./queue');
* const Test = require('./test');
*
* const queue = new Queue();
* const file = 'some/file.js';
* const item = 'some~item';
* const line = 42;
* const test = new Test(file, item, line, 'console.log(true)', {}, {});
*
* queue.push(test);
*
* queue.run(
* (f) => { f.should.equal(file); },
* (i, l, f) => {
* i.should.equal(item);
* l.should.equal(line);
* f.should.equal(file);
* },
* (t, i) => {
* t.should.equal(test);
* i.should.equal(0);
* }
* );
* ```
*
* @param {Function(file: String)} fileCb
* @param {Function(item: String, line: Number, file: String)} itemCb
* @param {Function(test: Test, index: Number)} testCb
*
* @return {Queue}
*/
run(fileCb, itemCb, testCb) {
Object.keys(this.items).forEach((file) => {
fileCb(file);
Object.keys(this.items[file]).forEach((item) => {
itemCb(item, this.items[file][item].line, file);
this.items[file][item].tests.forEach((test, index) => {
testCb(test, index);
});
});
});
}
}
module.exports = Queue;