-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtesting.ts
71 lines (67 loc) · 2.89 KB
/
testing.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
import {Node} from "../model/model";
import {strictEqual,deepStrictEqual,fail} from "node:assert";
export function assertASTsAreEqual(
expected: Node,
actual: Node,
context = "<root>",
considerPosition = false
): void {
if (areSameType(expected, actual)) {
if (considerPosition) {
deepStrictEqual(actual.position, expected.position, `${context}.position`);
}
expected.features.forEach(expectedProperty => {
const actualPropValue = actual.features.find(p => p.name == expectedProperty.name)!.value;
const expectedPropValue = expectedProperty.value;
if (actualPropValue instanceof Node && expectedPropValue instanceof Node) {
assertASTsAreEqual(
expectedPropValue as Node,
actualPropValue as Node,
`${context}.${expectedProperty.name}`,
considerPosition
);
}
else if (Array.isArray(actualPropValue) && Array.isArray(expectedPropValue)) {
strictEqual(
actualPropValue.length,
expectedPropValue.length,
`${context}, property ${expectedProperty.name} has length ${expectedPropValue.length}, but found ${actualPropValue.length}`
);
for (let i = 0; i < expectedPropValue.length; i++) {
const expectedElement = expectedPropValue[i];
const actualElement = actualPropValue[i];
if (expectedElement instanceof Node && actualElement instanceof Node) {
assertASTsAreEqual(
expectedElement as Node,
actualElement as Node,
`${context}.${expectedProperty.name}[${i}]`,
considerPosition
);
}
else if (typeof expectedElement != "object" && typeof actualElement != "object") {
strictEqual(
actualElement,
expectedElement,
`${context}, property ${expectedProperty.name}[${i}] is ${expectedPropValue[i]}, but found ${actualPropValue[i]}`,
);
}
}
}
else {
strictEqual(
actualPropValue,
expectedPropValue,
`${context}, property ${expectedProperty.name} is '${expectedPropValue}', but found '${actualPropValue}'`
);
}
});
}
else {
fail(`${context}: nodes are not of the same type`);
}
}
function areSameType(a, b) : boolean {
return typeof a == 'object' &&
typeof a == typeof b &&
Object.getPrototypeOf(a) === Object.getPrototypeOf(b);
}