a90889a7d4b3de20c0fd2fb2e9b6664022cc4974.svn-base
1.75 KB
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
var test = require('tap').test
var EE = require('events').EventEmitter
var etoa = require('../etoa.js')
test('basic', function (t) {
var emitter = new EE()
var array = etoa(emitter, ['ignore', 'alsoignore'])
emitter.emit('foo', 1, 2, 3)
emitter.emit('ignore', 'should not see this')
emitter.emit('bar', { x: 1 })
// nested events get tracked as well
var subemit = new EE()
emitter.emit('sub', subemit)
subemit.emit('childEvent', { some: 'data' })
subemit.emit('alsoignore', 'should not see this')
subemit.emit('anotherone', { some: 'data' }, 'many', 'args')
// CAVEAT!
emitter.emit('blaz', 'blorrg')
subemit.emit('order', 'not', 'preserved between child and parent')
// check out the array whenever
t.same(array,
[ [ 'foo', 1, 2, 3 ],
[ 'bar', { x: 1 } ],
[ 'sub',
[ [ 'childEvent', { some: 'data' } ],
[ 'anotherone', { some: 'data' }, 'many', 'args' ],
[ 'order', 'not', 'preserved between child and parent' ] ] ],
[ 'blaz', 'blorrg' ] ])
t.end()
})
test('ignore nothing', function (t) {
var emitter = new EE()
var array = etoa(emitter)
emitter.emit('foo', 1, 2, 3)
emitter.emit('ignore', 'should see this')
emitter.emit('bar', { x: 1 })
t.same(array,
[ [ 'foo', 1, 2, 3 ],
[ 'ignore', 'should see this' ],
[ 'bar', { x: 1 } ] ])
t.end()
})
test('the map is not the territory', function (t) {
var emitter = new EE()
// cast all to strings
var array = etoa(emitter, ['ignore'], function (arg) {
return arg + ''
})
emitter.emit('foo', new Buffer('hello'))
var sub = new EE()
emitter.emit('sub', sub)
sub.emit('obj', { toString: function () { return 'toString fn' } })
t.same(array,
[ ['foo', 'hello' ],
[ 'sub', [ [ 'obj', 'toString fn' ] ] ] ])
t.end()
})