-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
73 lines (61 loc) · 1.98 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
!function(root) {
var common = typeof module != 'undefined' && !!module.exports
, aok = common ? require('aok') : root.aok
, energy = common ? require('./') : root.energy
, emitters = [energy(), new energy]
, key = 'key'
, indexOf = [].indexOf
function noop() {}
function yes() { return true }
function test(emitter, i) {
var ran = []
aok('instance-' + i, emitter instanceof energy)
aok('listeners-' + i, typeof emitter.listeners(key).length == 'number')
aok('listeners()', function() {
try {
emitter.listeners()
} catch(e) {
return 'message' in e ? '@listeners' == e.message : true
}
})
!function(id) {
var bool = true, gone = indexOf ? function(what) {
return -1 === emitter.listeners(key).indexOf(what)
} : yes
emitter.on(key, noop).off(key, noop)
if (bool = gone(noop)) {
emitter.on(key, noop).on(key, noop).off(key, noop)
bool = gone(noop)
}
aok(id, bool)
emitter.off(key, noop)
}('off')
emitter.off(key).once(key, function() {
ran[i] = true
aok('once-' + i, 0 === emitter.listeners(key).length)
}).emit(key)
aok('to', function() {
var bool = wannabe === energy.to(wannabe) && typeof wannabe.emit == 'function'
function wannabe() {
wannabe.emit('called')
}
bool && wannabe.once('called', function() {
bool = wannabe === this
}) && wannabe()
return bool
})
!function(id, n) {
var i = n, initial = emitter.listeners(key).length
emitter.off(key, noop)
while (i--) emitter.on(key, noop)
aok(id, n === emitter.listeners(key).length - initial)
emitter.off(key, noop)
}('repeat', 3)
setTimeout(function() {
if (ran[i]) aok.info('All tests ran for group ' + i)
else aok.warn('Group ' + i + ' tests did not complete.')
}, 0)
}
aok.pass(emitters, test)
if (common) aok(require('deep-equal').apply(null, emitters))
}(this);