-
Notifications
You must be signed in to change notification settings - Fork 54
/
shell.js
48 lines (46 loc) · 1.58 KB
/
shell.js
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
// Checks that |a_orig| and |b_orig| are:
// 1. Both instances of |type|, and
// 2. Are structurally equivalent (as dictated by the structure of |type|).
function assertTypedEqual(type, a_orig, b_orig) {
try {
recur(type, a_orig, b_orig);
} catch (e) {
var msg = `failure during assertTypedEqual(${describeType(type)}, ${JSON.stringify(a_orig)}, ${JSON.stringify(b_orig)})`;
print(msg);
throw e;
}
function recur(type, a, b) {
if (type instanceof ArrayType) {
assertEq(a.length, type.length);
assertEq(b.length, type.length);
for (var i = 0; i < type.length; i++)
recur(type.elementType, a[i], b[i]);
} else if (type instanceof StructType) {
var fieldNames = Object.getOwnPropertyNames(type.fieldTypes);
for (var i = 0; i < fieldNames.length; i++) {
var fieldName = fieldNames[i];
recur(type.fieldTypes[fieldName], a[fieldName], b[fieldName]);
}
} else {
assertEq(a, b);
}
}
}
function describeType(type) {
if (type instanceof TypedObject.ArrayType) {
return `new ArrayType(${describeType(type.elementType)}, ${type.length})`;
} else if (type instanceof TypedObject.StructType) {
var fields = [];
for (var key of Object.getOwnPropertyNames(type.fieldTypes)) {
fields.push(`${key}: ${describeType(type.fieldTypes[key])}`);
}
return `new StructType({${fields.join(", ")}})`;
} else {
for (var key of Object.getOwnPropertyNames(TypedObject)) {
if (TypedObject[key] === type) {
return key;
}
}
return JSON.stringify(type);
}
}