-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.ts
129 lines (109 loc) · 3.13 KB
/
test_utils.ts
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
import { check, CheckOptions, Property } from "npm:testcheck@1.0.0-rc.2";
import {
assert,
assertEquals,
fail,
} from "https://deno.land/std@0.205.0/assert/mod.ts";
import {
fromFileUrl,
join,
resolve,
} from "https://deno.land/std@0.205.0/path/mod.ts";
import { dumpBuffer, format, pad } from "./lib/utils.ts";
import initTrace from "./lib/debug.ts";
export * from "https://deno.land/std@0.205.0/assert/mod.ts";
const trace = initTrace("capnp-ts-test:util");
const testdataDir = resolve(
fromFileUrl(import.meta.url),
"..",
"testdata",
);
function diffHex(found: ArrayBuffer, wanted: ArrayBuffer): string {
const a = new Uint8Array(found);
const b = new Uint8Array(wanted);
for (let i = 0; i < a.byteLength && i < b.byteLength; i++) {
if (a[i] !== b[i]) {
trace(dumpBuffer(found));
trace(dumpBuffer(wanted));
return format(
"addr:%a,found:%s,wanted:%s",
i,
pad(a[i].toString(16), 2),
pad(b[i].toString(16), 2),
);
}
}
if (a.byteLength > b.byteLength) {
return format(
"addr:%a,found:%s,wanted:EOF",
b.byteLength,
pad(a[b.byteLength].toString(16), 2),
);
} else if (b.byteLength > a.byteLength) {
return format(
"addr:%a,found:EOF,wanted:%s",
a.byteLength,
pad(b[a.byteLength].toString(16), 2),
);
}
return "equal";
}
export async function compareBuffers(
t: Deno.TestContext,
found: ArrayBuffer,
wanted: ArrayBuffer,
name = "should have the same buffer contents",
): Promise<void> {
await t.step(name, () => {
assertEquals(
found.byteLength,
wanted.byteLength,
`should have the same byte length (diff=${diffHex(found, wanted)}).`,
);
// End the comparison prematurely if the buffer lengths differ.
if (found.byteLength !== wanted.byteLength) {
return;
}
const a = new Uint8Array(found);
const b = new Uint8Array(wanted);
for (let i = 0; i < a.byteLength; i++) {
if (a[i] !== b[i]) {
fail(`bytes are not equal (${diffHex(found, wanted)})`);
// Don't check any of the other bytes or else we might flood with failures.
return;
}
}
});
}
export function readFileBuffer(filename: string): ArrayBuffer {
const absoluteFilePath = join(testdataDir, filename);
const buffer = Deno.readFileSync(absoluteFilePath);
return buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
}
export function readTextFile(filename: string): string {
const absoluteFilePath = join(testdataDir, filename);
return Deno.readTextFileSync(absoluteFilePath);
}
export async function runTestCheck<TArgs>(
t: Deno.TestContext,
property: Property<TArgs>,
options?: CheckOptions,
name = "should satisfy property check",
): Promise<void> {
await t.step(name, () => {
const out = check(property, options);
assertEquals(
out.result,
true,
`property check failed ${JSON.stringify(out)}`,
);
});
}
export function assertDoesNotThrow(fn: () => unknown, message?: string) {
// TODO: Better assertion
try {
fn();
} catch (_error) {
assert(false, message ?? "expected to not throw");
}
}