@gjsify/events 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -2
- package/cjs-compat.cjs +6 -0
- package/lib/esm/event-emitter.js +591 -0
- package/lib/esm/index.js +24 -4
- package/lib/types/event-emitter.d.ts +69 -0
- package/lib/types/index.d.ts +13 -0
- package/package.json +14 -22
- package/src/event-emitter.spec.ts +1250 -24
- package/src/event-emitter.ts +726 -0
- package/src/index.ts +20 -11
- package/tsconfig.json +21 -9
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/cjs/index.js +0 -6
- package/test.gjs.js +0 -34942
- package/test.gjs.mjs +0 -34799
- package/test.gjs.mjs.meta.json +0 -1
- package/test.node.js +0 -1410
- package/test.node.mjs +0 -418
- package/tsconfig.types.json +0 -8
package/test.node.mjs
DELETED
|
@@ -1,418 +0,0 @@
|
|
|
1
|
-
// ../../../node_modules/@girs/gjs/gjs.js
|
|
2
|
-
var imports = globalThis.imports || {};
|
|
3
|
-
|
|
4
|
-
// ../../gjs/unit/lib/esm/index.js
|
|
5
|
-
import nodeAssert from "assert";
|
|
6
|
-
var mainloop = globalThis?.imports?.mainloop;
|
|
7
|
-
var countTestsOverall = 0;
|
|
8
|
-
var countTestsFailed = 0;
|
|
9
|
-
var countTestsIgnored = 0;
|
|
10
|
-
var runtime = "";
|
|
11
|
-
var RED = "\x1B[31m";
|
|
12
|
-
var GREEN = "\x1B[32m";
|
|
13
|
-
var BLUE = "\x1B[34m";
|
|
14
|
-
var GRAY = "\x1B[90m";
|
|
15
|
-
var RESET = "\x1B[39m";
|
|
16
|
-
var print = globalThis.print || console.log;
|
|
17
|
-
var MatcherFactory = class _MatcherFactory {
|
|
18
|
-
constructor(actualValue, positive, negated) {
|
|
19
|
-
this.actualValue = actualValue;
|
|
20
|
-
this.positive = positive;
|
|
21
|
-
if (negated) {
|
|
22
|
-
this.not = negated;
|
|
23
|
-
} else {
|
|
24
|
-
this.not = new _MatcherFactory(actualValue, !positive, this);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
not;
|
|
28
|
-
triggerResult(success, msg) {
|
|
29
|
-
if (success && !this.positive || !success && this.positive) {
|
|
30
|
-
++countTestsFailed;
|
|
31
|
-
throw new Error(msg);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
to(callback) {
|
|
35
|
-
this.triggerResult(
|
|
36
|
-
callback(this.actualValue),
|
|
37
|
-
` Expected callback to validate`
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
toBe(expectedValue) {
|
|
41
|
-
this.triggerResult(
|
|
42
|
-
this.actualValue === expectedValue,
|
|
43
|
-
` Expected values to match using ===
|
|
44
|
-
Expected: ${expectedValue} (${typeof expectedValue})
|
|
45
|
-
Actual: ${this.actualValue} (${typeof this.actualValue})`
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
toEqual(expectedValue) {
|
|
49
|
-
this.triggerResult(
|
|
50
|
-
this.actualValue == expectedValue,
|
|
51
|
-
` Expected values to match using ==
|
|
52
|
-
Expected: ${expectedValue} (${typeof expectedValue})
|
|
53
|
-
Actual: ${this.actualValue} (${typeof this.actualValue})`
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
toEqualArray(expectedValue) {
|
|
57
|
-
let success = Array.isArray(this.actualValue) && Array.isArray(expectedValue) && this.actualValue.length === expectedValue.length;
|
|
58
|
-
for (let i = 0; i < this.actualValue.length; i++) {
|
|
59
|
-
const actualVal = this.actualValue[i];
|
|
60
|
-
const expectedVal = expectedValue[i];
|
|
61
|
-
success = actualVal == expectedVal;
|
|
62
|
-
if (!success)
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
this.triggerResult(
|
|
66
|
-
success,
|
|
67
|
-
` Expected array items to match using ==
|
|
68
|
-
Expected: ${expectedValue} (${typeof expectedValue})
|
|
69
|
-
Actual: ${this.actualValue} (${typeof this.actualValue})`
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
toMatch(expectedValue) {
|
|
73
|
-
if (typeof this.actualValue.match !== "function") {
|
|
74
|
-
throw new Error(`You can not use toMatch on type ${typeof this.actualValue}`);
|
|
75
|
-
}
|
|
76
|
-
this.triggerResult(
|
|
77
|
-
!!this.actualValue.match(expectedValue),
|
|
78
|
-
" Expected values to match using regular expression\n Expression: " + expectedValue + "\n Actual: " + this.actualValue
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
toBeDefined() {
|
|
82
|
-
this.triggerResult(
|
|
83
|
-
typeof this.actualValue !== "undefined",
|
|
84
|
-
` Expected value to be defined`
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
toBeUndefined() {
|
|
88
|
-
this.triggerResult(
|
|
89
|
-
typeof this.actualValue === "undefined",
|
|
90
|
-
` Expected value to be undefined`
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
toBeNull() {
|
|
94
|
-
this.triggerResult(
|
|
95
|
-
this.actualValue === null,
|
|
96
|
-
` Expected value to be null`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
toBeTruthy() {
|
|
100
|
-
this.triggerResult(
|
|
101
|
-
this.actualValue,
|
|
102
|
-
` Expected value to be truthy`
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
toBeFalsy() {
|
|
106
|
-
this.triggerResult(
|
|
107
|
-
!this.actualValue,
|
|
108
|
-
` Expected value to be falsy`
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
toContain(needle) {
|
|
112
|
-
this.triggerResult(
|
|
113
|
-
this.actualValue instanceof Array && this.actualValue.indexOf(needle) !== -1,
|
|
114
|
-
` Expected ` + this.actualValue + ` to contain ` + needle
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
toBeLessThan(greaterValue) {
|
|
118
|
-
this.triggerResult(
|
|
119
|
-
this.actualValue < greaterValue,
|
|
120
|
-
` Expected ` + this.actualValue + ` to be less than ` + greaterValue
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
toBeGreaterThan(smallerValue) {
|
|
124
|
-
this.triggerResult(
|
|
125
|
-
this.actualValue > smallerValue,
|
|
126
|
-
` Expected ` + this.actualValue + ` to be greater than ` + smallerValue
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
toBeCloseTo(expectedValue, precision) {
|
|
130
|
-
const shiftHelper = Math.pow(10, precision);
|
|
131
|
-
this.triggerResult(
|
|
132
|
-
Math.round(this.actualValue * shiftHelper) / shiftHelper === Math.round(expectedValue * shiftHelper) / shiftHelper,
|
|
133
|
-
` Expected ` + this.actualValue + ` with precision ` + precision + ` to be close to ` + expectedValue
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
toThrow(ErrorType) {
|
|
137
|
-
let errorMessage = "";
|
|
138
|
-
let didThrow = false;
|
|
139
|
-
let typeMatch = true;
|
|
140
|
-
try {
|
|
141
|
-
this.actualValue();
|
|
142
|
-
didThrow = false;
|
|
143
|
-
} catch (e) {
|
|
144
|
-
errorMessage = e.message || "";
|
|
145
|
-
didThrow = true;
|
|
146
|
-
if (ErrorType) {
|
|
147
|
-
typeMatch = e instanceof ErrorType;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const functionName = this.actualValue.name || typeof this.actualValue === "function" ? "[anonymous function]" : this.actualValue.toString();
|
|
151
|
-
this.triggerResult(
|
|
152
|
-
didThrow,
|
|
153
|
-
` Expected ${functionName} to ${this.positive ? "throw" : "not throw"} an exception ${!this.positive && errorMessage ? `, but an error with the message "${errorMessage}" was thrown` : ""}`
|
|
154
|
-
);
|
|
155
|
-
if (ErrorType) {
|
|
156
|
-
this.triggerResult(
|
|
157
|
-
typeMatch,
|
|
158
|
-
` Expected Error type '${ErrorType.name}', but the error is not an instance of it`
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
var describe = async function(moduleName, callback) {
|
|
164
|
-
print("\n" + moduleName);
|
|
165
|
-
await callback();
|
|
166
|
-
beforeEachCb = null;
|
|
167
|
-
afterEachCb = null;
|
|
168
|
-
};
|
|
169
|
-
var beforeEachCb;
|
|
170
|
-
var afterEachCb;
|
|
171
|
-
var it = async function(expectation, callback) {
|
|
172
|
-
try {
|
|
173
|
-
if (typeof beforeEachCb === "function") {
|
|
174
|
-
await beforeEachCb();
|
|
175
|
-
}
|
|
176
|
-
await callback();
|
|
177
|
-
if (typeof afterEachCb === "function") {
|
|
178
|
-
await afterEachCb();
|
|
179
|
-
}
|
|
180
|
-
print(` ${GREEN}\u2714${RESET} ${GRAY}${expectation}${RESET}`);
|
|
181
|
-
} catch (e) {
|
|
182
|
-
print(` ${RED}\u274C${RESET} ${GRAY}${expectation}${RESET}`);
|
|
183
|
-
print(`${RED}${e.message}${RESET}`);
|
|
184
|
-
if (e.stack)
|
|
185
|
-
print(e.stack);
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
var expect = function(actualValue) {
|
|
189
|
-
++countTestsOverall;
|
|
190
|
-
const expecter = new MatcherFactory(actualValue, true);
|
|
191
|
-
return expecter;
|
|
192
|
-
};
|
|
193
|
-
var assert = function(success, message) {
|
|
194
|
-
++countTestsOverall;
|
|
195
|
-
if (!success) {
|
|
196
|
-
++countTestsFailed;
|
|
197
|
-
}
|
|
198
|
-
nodeAssert(success, message);
|
|
199
|
-
};
|
|
200
|
-
assert.strictEqual = function(actual, expected, message) {
|
|
201
|
-
++countTestsOverall;
|
|
202
|
-
try {
|
|
203
|
-
nodeAssert.strictEqual(actual, expected, message);
|
|
204
|
-
} catch (error) {
|
|
205
|
-
++countTestsFailed;
|
|
206
|
-
throw error;
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
assert.throws = function(promiseFn, ...args) {
|
|
210
|
-
++countTestsOverall;
|
|
211
|
-
let error;
|
|
212
|
-
try {
|
|
213
|
-
promiseFn();
|
|
214
|
-
} catch (e) {
|
|
215
|
-
error = e;
|
|
216
|
-
}
|
|
217
|
-
if (!error)
|
|
218
|
-
++countTestsFailed;
|
|
219
|
-
nodeAssert.throws(() => {
|
|
220
|
-
if (error)
|
|
221
|
-
throw error;
|
|
222
|
-
}, args[0], args[1]);
|
|
223
|
-
};
|
|
224
|
-
assert.deepStrictEqual = function(actual, expected, message) {
|
|
225
|
-
++countTestsOverall;
|
|
226
|
-
try {
|
|
227
|
-
nodeAssert.deepStrictEqual(actual, expected, message);
|
|
228
|
-
} catch (error) {
|
|
229
|
-
++countTestsFailed;
|
|
230
|
-
throw error;
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
var runTests = async function(namespaces) {
|
|
234
|
-
for (const subNamespace in namespaces) {
|
|
235
|
-
const namespace = namespaces[subNamespace];
|
|
236
|
-
if (typeof namespace === "function") {
|
|
237
|
-
await namespace();
|
|
238
|
-
} else if (typeof namespace === "object") {
|
|
239
|
-
await runTests(namespace);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
var printResult = () => {
|
|
244
|
-
if (countTestsIgnored) {
|
|
245
|
-
print(`
|
|
246
|
-
${BLUE}\u2714 ${countTestsIgnored} ignored test${countTestsIgnored > 1 ? "s" : ""}${RESET}`);
|
|
247
|
-
}
|
|
248
|
-
if (countTestsFailed) {
|
|
249
|
-
print(`
|
|
250
|
-
${RED}\u274C ${countTestsFailed} of ${countTestsOverall} tests failed${RESET}`);
|
|
251
|
-
} else {
|
|
252
|
-
print(`
|
|
253
|
-
${GREEN}\u2714 ${countTestsOverall} completed${RESET}`);
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
var getRuntime = async () => {
|
|
257
|
-
if (runtime && runtime !== "Unknown") {
|
|
258
|
-
return runtime;
|
|
259
|
-
}
|
|
260
|
-
if (globalThis.Deno?.version?.deno) {
|
|
261
|
-
return "Deno " + globalThis.Deno?.version?.deno;
|
|
262
|
-
} else {
|
|
263
|
-
let process = globalThis.process;
|
|
264
|
-
if (!process) {
|
|
265
|
-
try {
|
|
266
|
-
process = await import("process");
|
|
267
|
-
} catch (error) {
|
|
268
|
-
console.error(error);
|
|
269
|
-
console.warn(error.message);
|
|
270
|
-
runtime = "Unknown";
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (process?.versions?.gjs) {
|
|
274
|
-
runtime = "Gjs " + process.versions.gjs;
|
|
275
|
-
} else if (process?.versions?.node) {
|
|
276
|
-
runtime = "Node.js " + process.versions.node;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
return runtime || "Unknown";
|
|
280
|
-
};
|
|
281
|
-
var printRuntime = async () => {
|
|
282
|
-
const runtime2 = await getRuntime();
|
|
283
|
-
print(`
|
|
284
|
-
Running on ${runtime2}`);
|
|
285
|
-
};
|
|
286
|
-
var run = async (namespaces) => {
|
|
287
|
-
printRuntime().then(async () => {
|
|
288
|
-
return runTests(namespaces).then(() => {
|
|
289
|
-
printResult();
|
|
290
|
-
print();
|
|
291
|
-
mainloop?.quit();
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
mainloop?.run();
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
// src/event-emitter.spec.ts
|
|
298
|
-
import { EventEmitter } from "events";
|
|
299
|
-
var event_emitter_spec_default = async () => {
|
|
300
|
-
await describe("events.EventEmitter: emit", async () => {
|
|
301
|
-
await it("1. Add two listeners on a single event and emit the event", async () => {
|
|
302
|
-
var emitter = new EventEmitter({ verbose: true });
|
|
303
|
-
let count = 0;
|
|
304
|
-
function functionA() {
|
|
305
|
-
count++;
|
|
306
|
-
}
|
|
307
|
-
function functionB() {
|
|
308
|
-
count++;
|
|
309
|
-
}
|
|
310
|
-
emitter.on("test2", functionA);
|
|
311
|
-
emitter.on("test2", functionB);
|
|
312
|
-
emitter.emit("test2");
|
|
313
|
-
expect(count).toBe(2);
|
|
314
|
-
});
|
|
315
|
-
await it("2. Add two listeners on a single event and emit the event twice", async () => {
|
|
316
|
-
var emitter = new EventEmitter({ verbose: true });
|
|
317
|
-
let count = 0;
|
|
318
|
-
function functionA() {
|
|
319
|
-
count++;
|
|
320
|
-
}
|
|
321
|
-
function functionB() {
|
|
322
|
-
count++;
|
|
323
|
-
}
|
|
324
|
-
emitter.on("test2", functionA);
|
|
325
|
-
emitter.on("test2", functionB);
|
|
326
|
-
emitter.emit("test2");
|
|
327
|
-
emitter.emit("test2");
|
|
328
|
-
expect(count).toBe(4);
|
|
329
|
-
});
|
|
330
|
-
await it("3. Add two listeners on a single event and emit the event with a parameter", async () => {
|
|
331
|
-
var emitter = new EventEmitter({ verbose: true });
|
|
332
|
-
let count = 0;
|
|
333
|
-
function functionA(value1) {
|
|
334
|
-
count++;
|
|
335
|
-
expect(typeof value1).toBe("string");
|
|
336
|
-
}
|
|
337
|
-
function functionB(value1) {
|
|
338
|
-
count++;
|
|
339
|
-
expect(typeof value1).toBe("string");
|
|
340
|
-
}
|
|
341
|
-
emitter.on("test2", functionA);
|
|
342
|
-
emitter.on("test2", functionB);
|
|
343
|
-
emitter.emit("test2", "Hello, Node");
|
|
344
|
-
expect(count).toBe(2);
|
|
345
|
-
});
|
|
346
|
-
await it(`4. Add two listeners on an single event and emit the event twice with a parameter.`, async () => {
|
|
347
|
-
var emitter = new EventEmitter({ verbose: true });
|
|
348
|
-
let count = 0;
|
|
349
|
-
function functionA(value1) {
|
|
350
|
-
count++;
|
|
351
|
-
expect(typeof value1).toBe("string");
|
|
352
|
-
}
|
|
353
|
-
function functionB(value1) {
|
|
354
|
-
count++;
|
|
355
|
-
expect(typeof value1).toBe("string");
|
|
356
|
-
}
|
|
357
|
-
emitter.on("test2", functionA);
|
|
358
|
-
emitter.on("test2", functionB);
|
|
359
|
-
emitter.emit("test2", "Hello, Node1");
|
|
360
|
-
emitter.emit("test2", "Hello, Node2");
|
|
361
|
-
expect(count).toBe(4);
|
|
362
|
-
});
|
|
363
|
-
await it(`5. Add two listeners on an single event and emit the event twice with multiple parameters.`, async () => {
|
|
364
|
-
var emitter = new EventEmitter({ verbose: true });
|
|
365
|
-
let count = 0;
|
|
366
|
-
function functionA(value1, value2, value3) {
|
|
367
|
-
count++;
|
|
368
|
-
expect(true).toBeTruthy();
|
|
369
|
-
expect(typeof value1).toBe("string");
|
|
370
|
-
expect(typeof value2).toBe("string");
|
|
371
|
-
expect(typeof value3).toBe("string");
|
|
372
|
-
}
|
|
373
|
-
function functionB(value1, value2, value3) {
|
|
374
|
-
count++;
|
|
375
|
-
expect(true).toBeTruthy();
|
|
376
|
-
expect(typeof value1).toBe("string");
|
|
377
|
-
expect(typeof value2).toBe("string");
|
|
378
|
-
expect(typeof value3).toBe("string");
|
|
379
|
-
}
|
|
380
|
-
emitter.on("test2", functionA);
|
|
381
|
-
emitter.on("test2", functionB);
|
|
382
|
-
emitter.emit("test2", "Hello, Node1", "Hello, Node2", "Hello, Node3");
|
|
383
|
-
emitter.emit("test2", "Hello, Node1", "Hello, Node2", "Hello, Node3");
|
|
384
|
-
expect(count).toBe(4);
|
|
385
|
-
});
|
|
386
|
-
await it("6. Check return values of emit.", async () => {
|
|
387
|
-
let count = 0;
|
|
388
|
-
var emitter = new EventEmitter({ verbose: true });
|
|
389
|
-
function functionA() {
|
|
390
|
-
count++;
|
|
391
|
-
expect(true).toBeTruthy();
|
|
392
|
-
}
|
|
393
|
-
emitter.on("test6", functionA);
|
|
394
|
-
expect(emitter.emit("test6")).toBeTruthy();
|
|
395
|
-
expect(emitter.emit("other")).toBeFalsy();
|
|
396
|
-
expect(() => {
|
|
397
|
-
emitter.onAny(functionA);
|
|
398
|
-
}).toThrow();
|
|
399
|
-
expect(emitter.emit("other")).toBeFalsy();
|
|
400
|
-
expect(count).toBe(1);
|
|
401
|
-
});
|
|
402
|
-
await it("7. Emit event with more than 2 arguments", async () => {
|
|
403
|
-
let count = 0;
|
|
404
|
-
var emitter = new EventEmitter({ verbose: true });
|
|
405
|
-
emitter.on("test", function(x, y, z) {
|
|
406
|
-
count++;
|
|
407
|
-
expect(x).toBe(1);
|
|
408
|
-
expect(y).toBe(2);
|
|
409
|
-
expect(z).toBe(3);
|
|
410
|
-
});
|
|
411
|
-
emitter.emit("test", 1, 2, 3);
|
|
412
|
-
expect(count).toBe(1);
|
|
413
|
-
});
|
|
414
|
-
});
|
|
415
|
-
};
|
|
416
|
-
|
|
417
|
-
// src/test.mts
|
|
418
|
-
run({ eventEmitterTestSuite: event_emitter_spec_default });
|