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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const { on, emit } = require("sdk/event/core");
const { filter, map, merge, expand, pipe, stripListeners } = require("sdk/event/utils");
const $ = require("./event/helpers");
function isEven(x) {
return !(x % 2);
}
function inc(x) {
return x + 1;
}
exports["test filter events"] = function(assert) {
let input = {};
let evens = filter(input, isEven);
let actual = [];
on(evens, "data", e => actual.push(e));
[1, 2, 3, 4, 5, 6, 7].forEach(x => emit(input, "data", x));
assert.deepEqual(actual, [2, 4, 6], "only even numbers passed through");
};
exports["test filter emits"] = $.emits(function(input, assert) {
let output = filter(input, isEven);
assert(output, [1, 2, 3, 4, 5], [2, 4], "this is `output` & evens passed");
});;
exports["test filter reg once"] = $.registerOnce(function(input, assert) {
assert(filter(input, isEven), [1, 2, 3, 4, 5, 6], [2, 4, 6],
"listener can be registered only once");
});
exports["test filter ignores new"] = $.ignoreNew(function(input, assert) {
assert(filter(input, isEven), [1, 2, 3], [2],
"new listener is ignored")
});
exports["test filter is FIFO"] = $.FIFO(function(input, assert) {
assert(filter(input, isEven), [1, 2, 3, 4], [2, 4],
"listeners are invoked in fifo order")
});
exports["test map events"] = function(assert) {
let input = {};
let incs = map(input, inc);
let actual = [];
on(incs, "data", e => actual.push(e));
[1, 2, 3, 4].forEach(x => emit(input, "data", x));
assert.deepEqual(actual, [2, 3, 4, 5], "all numbers were incremented");
};
exports["test map emits"] = $.emits(function(input, assert) {
let output = map(input, inc);
assert(output, [1, 2, 3], [2, 3, 4], "this is `output` & evens passed");
});
exports["test map reg once"] = $.registerOnce(function(input, assert) {
assert(map(input, inc), [1, 2, 3], [2, 3, 4],
"listener can be registered only once");
});
exports["test map ignores new"] = $.ignoreNew(function(input, assert) {
assert(map(input, inc), [1], [2],
"new listener is ignored")
});
exports["test map is FIFO"] = $.FIFO(function(input, assert) {
assert(map(input, inc), [1, 2, 3, 4], [2, 3, 4, 5],
"listeners are invoked in fifo order")
});
exports["test merge stream[stream]"] = function(assert) {
let a = {}, b = {}, c = {};
let inputs = {};
let actual = [];
on(merge(inputs), "data", $ => actual.push($))
emit(inputs, "data", a);
emit(a, "data", "a1");
emit(inputs, "data", b);
emit(b, "data", "b1");
emit(a, "data", "a2");
emit(inputs, "data", c);
emit(c, "data", "c1");
emit(c, "data", "c2");
emit(b, "data", "b2");
emit(a, "data", "a3");
assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
"all inputs data merged into one");
};
exports["test merge array[stream]"] = function(assert) {
let a = {}, b = {}, c = {};
let inputs = {};
let actual = [];
on(merge([a, b, c]), "data", $ => actual.push($))
emit(a, "data", "a1");
emit(b, "data", "b1");
emit(a, "data", "a2");
emit(c, "data", "c1");
emit(c, "data", "c2");
emit(b, "data", "b2");
emit(a, "data", "a3");
assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
"all inputs data merged into one");
};
exports["test merge emits"] = $.emits(function(input, assert) {
let evens = filter(input, isEven)
let output = merge([evens, input]);
assert(output, [1, 2, 3], [1, 2, 2, 3], "this is `output` & evens passed");
});
exports["test merge reg once"] = $.registerOnce(function(input, assert) {
let evens = filter(input, isEven)
let output = merge([input, evens]);
assert(output, [1, 2, 3, 4], [1, 2, 2, 3, 4, 4],
"listener can be registered only once");
});
exports["test merge ignores new"] = $.ignoreNew(function(input, assert) {
let evens = filter(input, isEven)
let output = merge([input, evens])
assert(output, [1], [1],
"new listener is ignored")
});
exports["test marge is FIFO"] = $.FIFO(function(input, assert) {
let evens = filter(input, isEven)
let output = merge([input, evens])
assert(output, [1, 2, 3, 4], [1, 2, 2, 3, 4, 4],
"listeners are invoked in fifo order")
});
exports["test expand"] = function(assert) {
let a = {}, b = {}, c = {};
let inputs = {};
let actual = [];
on(expand(inputs, $ => $()), "data", $ => actual.push($))
emit(inputs, "data", () => a);
emit(a, "data", "a1");
emit(inputs, "data", () => b);
emit(b, "data", "b1");
emit(a, "data", "a2");
emit(inputs, "data", () => c);
emit(c, "data", "c1");
emit(c, "data", "c2");
emit(b, "data", "b2");
emit(a, "data", "a3");
assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
"all inputs data merged into one");
};
exports["test pipe"] = function (assert, done) {
let src = {};
let dest = {};
let aneventCount = 0, multiargsCount = 0;
let wildcardCount = {};
on(dest, 'an-event', arg => {
assert.equal(arg, 'my-arg', 'piped argument to event');
++aneventCount;
check();
});
on(dest, 'multiargs', (...data) => {
assert.equal(data[0], 'a', 'multiple arguments passed via pipe');
assert.equal(data[1], 'b', 'multiple arguments passed via pipe');
assert.equal(data[2], 'c', 'multiple arguments passed via pipe');
++multiargsCount;
check();
});
on(dest, '*', (name, ...data) => {
wildcardCount[name] = (wildcardCount[name] || 0) + 1;
if (name === 'multiargs') {
assert.equal(data[0], 'a', 'multiple arguments passed via pipe, wildcard');
assert.equal(data[1], 'b', 'multiple arguments passed via pipe, wildcard');
assert.equal(data[2], 'c', 'multiple arguments passed via pipe, wildcard');
}
if (name === 'an-event')
assert.equal(data[0], 'my-arg', 'argument passed via pipe, wildcard');
check();
});
pipe(src, dest);
for (let i = 0; i < 3; i++)
emit(src, 'an-event', 'my-arg');
emit(src, 'multiargs', 'a', 'b', 'c');
function check () {
if (aneventCount === 3 && multiargsCount === 1 &&
wildcardCount['an-event'] === 3 &&
wildcardCount['multiargs'] === 1)
done();
}
};
exports["test pipe multiple targets"] = function (assert) {
let src1 = {};
let src2 = {};
let middle = {};
let dest = {};
pipe(src1, middle);
pipe(src2, middle);
pipe(middle, dest);
let middleFired = 0;
let destFired = 0;
let src1Fired = 0;
let src2Fired = 0;
on(src1, '*', () => src1Fired++);
on(src2, '*', () => src2Fired++);
on(middle, '*', () => middleFired++);
on(dest, '*', () => destFired++);
emit(src1, 'ev');
assert.equal(src1Fired, 1, 'event triggers in source in pipe chain');
assert.equal(middleFired, 1, 'event passes through the middle of pipe chain');
assert.equal(destFired, 1, 'event propagates to end of pipe chain');
assert.equal(src2Fired, 0, 'event does not fire on alternative chain routes');
emit(src2, 'ev');
assert.equal(src2Fired, 1, 'event triggers in source in pipe chain');
assert.equal(middleFired, 2,
'event passes through the middle of pipe chain from different src');
assert.equal(destFired, 2,
'event propagates to end of pipe chain from different src');
assert.equal(src1Fired, 1, 'event does not fire on alternative chain routes');
emit(middle, 'ev');
assert.equal(middleFired, 3,
'event triggers in source of pipe chain');
assert.equal(destFired, 3,
'event propagates to end of pipe chain from middle src');
assert.equal(src1Fired, 1, 'event does not fire on alternative chain routes');
assert.equal(src2Fired, 1, 'event does not fire on alternative chain routes');
};
exports['test stripListeners'] = function (assert) {
var options = {
onAnEvent: noop1,
onMessage: noop2,
verb: noop1,
value: 100
};
var stripped = stripListeners(options);
assert.ok(stripped !== options, 'stripListeners should return a new object');
assert.equal(options.onAnEvent, noop1, 'stripListeners does not affect original');
assert.equal(options.onMessage, noop2, 'stripListeners does not affect original');
assert.equal(options.verb, noop1, 'stripListeners does not affect original');
assert.equal(options.value, 100, 'stripListeners does not affect original');
assert.ok(!stripped.onAnEvent, 'stripListeners removes `on*` values');
assert.ok(!stripped.onMessage, 'stripListeners removes `on*` values');
assert.equal(stripped.verb, noop1, 'stripListeners leaves not `on*` values');
assert.equal(stripped.value, 100, 'stripListeners leaves not `on*` values');
function noop1 () {}
function noop2 () {}
};
require('sdk/test').run(exports);
|