-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathtypeof.spec.ts
More file actions
99 lines (84 loc) · 3.41 KB
/
typeof.spec.ts
File metadata and controls
99 lines (84 loc) · 3.41 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
import * as util from "../util";
test.each(["0", "30", "30_000", "30.00"])("typeof number (%p)", inp => {
util.testExpression`typeof ${inp}`.expectToMatchJsResult();
});
test.each(['"abc"', "`abc`"])("typeof string (%p)", inp => {
util.testExpression`typeof ${inp}`.expectToMatchJsResult();
});
test.each(["false", "true"])("typeof boolean (%p)", inp => {
util.testExpression`typeof ${inp}`.expectToMatchJsResult();
});
test.each(["{}", "[]"])("typeof object literal (%p)", inp => {
util.testExpression`typeof ${inp}`.expectToMatchJsResult();
});
test("typeof class", () => {
util.testFunction`
class MyClass {}
return typeof MyClass;
`.expectToEqual("object");
});
test("typeof class instance", () => {
util.testFunction`
class MyClass {}
return typeof new MyClass();
`.expectToMatchJsResult();
});
test("typeof function", () => {
util.testExpression`typeof (() => 3)`.expectToMatchJsResult();
});
test.each(["null", "undefined"])("typeof %s", inp => {
util.testExpression`typeof ${inp}`.expectToEqual("undefined");
});
interface ComparisonCase {
expression: string;
operator: string;
compareTo: string;
}
const equalityComparisonCases: ComparisonCase[] = [
{ expression: "{}", operator: "===", compareTo: "object" },
{ expression: "{}", operator: "!==", compareTo: "object" },
{ expression: "{}", operator: "==", compareTo: "object" },
{ expression: "{}", operator: "!=", compareTo: "object" },
{ expression: "undefined", operator: "===", compareTo: "undefined" },
{ expression: "() => {}", operator: "===", compareTo: "function" },
{ expression: "1", operator: "===", compareTo: "number" },
{ expression: "true", operator: "===", compareTo: "boolean" },
{ expression: '"foo"', operator: "===", compareTo: "string" },
];
const relationalComparisonCases: ComparisonCase[] = [
{ expression: "undefined", operator: "<=", compareTo: "object" },
{ expression: "undefined", operator: "<", compareTo: "object" },
{ expression: "undefined", operator: ">=", compareTo: "object" },
{ expression: "undefined", operator: ">", compareTo: "object" },
];
const expectTypeOfHelper: util.TapCallback = builder => expect(builder.getMainLuaCodeChunk()).toMatch("__TS__TypeOf");
const expectNoTypeOfHelper: util.TapCallback = builder =>
expect(builder.getMainLuaCodeChunk()).not.toMatch("__TS__TypeOf");
test.each(equalityComparisonCases)("typeof literal equality comparison (%p)", ({ expression, operator, compareTo }) => {
util.testFunction`
const value = ${expression};
return typeof value ${operator} "${compareTo}";
`
.tap(expectNoTypeOfHelper)
.expectToMatchJsResult();
});
test.each(relationalComparisonCases)("typeof literal comparison (%p)", ({ expression, operator, compareTo }) => {
util.testFunction`
const value = ${expression};
return typeof value ${operator} "${compareTo}";
`
.tap(expectTypeOfHelper)
.expectToMatchJsResult();
});
test.each([...equalityComparisonCases, ...relationalComparisonCases])(
"typeof non-literal comparison (%p)",
({ expression, operator, compareTo }) => {
util.testFunction`
const value = ${expression};
const compareTo = "${compareTo}";
return typeof value ${operator} compareTo;
`
.tap(expectTypeOfHelper)
.expectToMatchJsResult();
}
);