-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathextract.js
More file actions
103 lines (87 loc) · 2.59 KB
/
extract.js
File metadata and controls
103 lines (87 loc) · 2.59 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
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
const assert = require("assert");
const addon = require("..");
describe("Extractors", () => {
it("Single Argument", () => {
assert.strictEqual(addon.extract_single_add_one(41), 42);
});
it("Kitchen Sink", () => {
const symbol = Symbol("Test");
const values = [
true,
42,
undefined,
"hello",
new Date(),
symbol,
new ArrayBuffer(100),
new Uint8Array(Buffer.from("Buffer")),
Buffer.from("Uint8Array"),
];
// Pass `null` and `undefined` for `None`
assert.deepStrictEqual(addon.extract_values(...values, null), [
...values,
undefined,
undefined,
]);
// Pass values for optional
assert.deepStrictEqual(addon.extract_values(...values, 100, "exists"), [
...values,
100,
"exists",
]);
});
it("Buffers", () => {
const test = (TypedArray) => {
const buf = new ArrayBuffer(24);
const view = new TypedArray(buf);
view[0] = 8;
view[1] = 16;
view[2] = 18;
assert.strictEqual(addon.extract_buffer_sum(view), 42);
};
test(Uint8Array);
test(Uint16Array);
test(Uint32Array);
test(Int8Array);
test(Int16Array);
test(Int32Array);
test(Float32Array);
test(Float64Array);
});
it("TypedArray", () => {
assert.deepStrictEqual(
Buffer.from(addon.bufferConcat(Buffer.from("abc"), Buffer.from("def"))),
Buffer.from("abcdef")
);
assert.deepStrictEqual(
Buffer.from(addon.stringToBuf("Hello, World!")),
Buffer.from("Hello, World!")
);
});
it("JSON", () => {
assert.strictEqual(addon.extract_json_sum([1, 2, 3, 4]), 10);
assert.strictEqual(addon.extract_json_sum([8, 16, 18]), 42);
assert.strictEqual(addon.extractJsonOption(42), 42);
assert.strictEqual(addon.extractJsonOption(null), null);
assert.strictEqual(addon.extractJsonOption(), null);
});
it("Either", () => {
assert.strictEqual(addon.extractEither("hello"), "String: hello");
assert.strictEqual(addon.extractEither(42), "Number: 42");
assert.throws(
() => addon.extractEither({}),
(err) => {
assert.match(err.message, /expected either.*String.*f64/);
assert.match(err.left.message, /expected string/);
assert.match(err.right.message, /expected number/);
return true;
}
);
});
it("With", async () => {
assert.strictEqual(await addon.sleepWithJs(1.5), 1.5);
assert.strictEqual(await addon.sleepWithJsSync(1.5), 1.5);
assert.strictEqual(await addon.sleepWith(1.5), 1.5);
assert.strictEqual(await addon.sleepWithSync(1.5), 1.5);
});
});