-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
79 lines (69 loc) · 1.78 KB
/
test.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
var queue = require('./index.js');
function log(text) {
process.stdout.write(text + '\n');
}
function comment(text) {
log('# ' + text);
}
function bail() {
log('Bail out!');
}
function plan(start, end) {
log(start + '..' + end);
}
var n = 1;
var passes = 0;
var failures = 0;
function pass(description) {
passes++;
description = description.split('\n').join('\n ');
log('ok ' + (n++) + ' - ' + description);
}
function fail(description) {
failures++;
description = description.split('\n').join('\n ');
log('not ok ' + (n++) + ' - ' + description);
}
function expect(success, message, fmessage) {
if (success) pass(message || 'Assertion passed');
else fail(fmessage || message || 'Assertion failed');
}
function test(name, fn) {
comment(name);
try {
fn();
} catch (er) {
fail(er.toString());
}
}
function matchesSpec(q) {
expect(typeof q.enqueue === 'function', 'enqueue should be a function');
expect(typeof q.dequeue === 'function', 'dequeue should be a function');
expect(typeof q.peek === 'function', 'peek should be a function');
expect(typeof q.isEmpty === 'function', 'isEmpty should be a function');
q.enqueue(1);
q.enqueue(2);
expect(!q.isEmpty());
expect(q.peek() === 1);
expect(!q.isEmpty());
expect(q.dequeue() === 1);
expect(!q.isEmpty());
q.enqueue(3);
expect(!q.isEmpty());
expect(q.dequeue() === 2);
expect(!q.isEmpty());
expect(q.dequeue() === 3);
expect(q.isEmpty());
}
comment('As object');
matchesSpec(queue());
comment('As mixin');
function Foo() {}
var q = queue(new Foo());
expect(q instanceof Foo, 'works as mixin', 'didn\'t work as mixin');
matchesSpec(q);
plan(1, passes + failures);
comment('tests ' + (passes + failures));
comment('pass ' + passes);
comment('fail ' + failures);
process.stdout.end();