forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.spec.ts
More file actions
156 lines (131 loc) · 3.49 KB
/
Copy pathenum.spec.ts
File metadata and controls
156 lines (131 loc) · 3.49 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import * as util from "../util";
// TODO: string.toString()
const serializeEnum = (identifier: string) => `(() => {
const mappedTestEnum: any = {};
for (const key in ${identifier}) {
mappedTestEnum[(key as any).toString()] = ${identifier}[key];
}
return mappedTestEnum;
})()`;
describe("initializers", () => {
test("string", () => {
util.testFunction`
enum TestEnum {
A = "A",
B = "B",
}
return ${serializeEnum("TestEnum")}
`.expectToMatchJsResult();
});
test("expression", () => {
util.testFunction`
const value = 6;
enum TestEnum {
A,
B = value,
}
return ${serializeEnum("TestEnum")}
`.expectToMatchJsResult();
});
test("expression with side effect", () => {
util.testFunction`
let value = 0;
enum TestEnum {
A = value++,
B = A,
}
return ${serializeEnum("TestEnum")}
`.expectToMatchJsResult();
});
test("inference", () => {
util.testFunction`
enum TestEnum {
A,
B,
C,
}
return ${serializeEnum("TestEnum")}
`.expectToMatchJsResult();
});
test("partial inference", () => {
util.testFunction`
enum TestEnum {
A = 3,
B,
C = 5,
}
return ${serializeEnum("TestEnum")}
`.expectToMatchJsResult();
});
test("member reference", () => {
util.testFunction`
enum TestEnum {
A,
B = A,
C = B,
}
return ${serializeEnum("TestEnum")}
`.expectToMatchJsResult();
});
test("string literal member reference", () => {
util.testFunction`
enum TestEnum {
["A"],
"B" = A,
C = B,
}
return ${serializeEnum("TestEnum")}
`.expectToMatchJsResult();
});
});
describe("const enum", () => {
const expectToBeConst: util.TapCallback = builder =>
expect(builder.getMainLuaCodeChunk()).not.toContain("TestEnum");
test.each(["", "declare "])("%swithout initializer", modifier => {
util.testModule`
${modifier} const enum TestEnum {
A,
B,
}
export const A = TestEnum.A;
`
.tap(expectToBeConst)
.expectToMatchJsResult();
});
test("with string initializer", () => {
util.testFunction`
const enum TestEnum {
A = "ONE",
B = "TWO",
}
return TestEnum.A;
`
.tap(expectToBeConst)
.expectToMatchJsResult();
});
test("access with string literal", () => {
util.testFunction`
const enum TestEnum {
A,
B,
C,
}
return TestEnum["C"];
`
.tap(expectToBeConst)
.expectToMatchJsResult();
});
});
test("toString", () => {
util.testFunction`
enum TestEnum {
A,
B,
C,
}
function foo(value: TestEnum) {
return value.toString();
}
return foo(TestEnum.A);
`.expectToMatchJsResult();
});