-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathexpressions.spec.ts
More file actions
212 lines (187 loc) · 6.64 KB
/
expressions.spec.ts
File metadata and controls
212 lines (187 loc) · 6.64 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import * as tstl from "../../src";
import { unsupportedForTarget, unsupportedRightShiftOperator } from "../../src/transformation/utils/diagnostics";
import * as util from "../util";
test.each([
"i++",
"++i",
"i--",
"--i",
"!a",
"-a",
"+a",
"let a = delete tbl['test']",
"delete tbl['test']",
"let a = delete tbl.test",
"delete tbl.test",
])("Unary expressions basic (%p)", input => {
util.testFunction(input).disableSemanticCheck().expectLuaToMatchSnapshot();
});
for (const expression of ["3+4", "5-2", "6*3", "6**3", "20/5", "15/10", "15%3"]) {
util.testEachVersion(
`Binary expressions basic numeric (${expression})`,
() => util.testExpression(expression),
util.expectEachVersionExceptJit(builder => builder.expectToMatchJsResult())
);
}
test.each(["1==1", "1===1", "1!=1", "1!==1", "1>1", "1>=1", "1<1", "1<=1", "1&&1", "1||1"])(
"Binary expressions basic boolean (%p)",
input => {
util.testExpression(input).expectToMatchJsResult();
}
);
test.each(["'key' in obj", "'existingKey' in obj", "0 in obj", "9 in obj"])("Binary expression in (%p)", input => {
util.testFunction`
let obj = { existingKey: 1 };
return ${input};
`.expectToMatchJsResult();
});
test.each(["a+=b", "a-=b", "a*=b", "a/=b", "a%=b", "a**=b"])("Binary expressions overridden operators (%p)", input => {
util.testFunction`
let a = 5;
let b = 3;
${input};
return a;
`.expectToMatchJsResult();
});
const supportedInAll = ["~a", "a&b", "a&=b", "a|b", "a|=b", "a^b", "a^=b", "a<<b", "a<<=b", "a>>>b", "a>>>=b"];
const unsupportedIn53And54 = ["a>>b", "a>>=b"];
const allBinaryOperators = [...supportedInAll, ...unsupportedIn53And54];
test.each(allBinaryOperators)("Bitop [5.0] (%p)", input => {
// Bit operations not supported in 5.0, expect an exception
util.testExpression(input)
.setOptions({ luaTarget: tstl.LuaTarget.Lua50, luaLibImport: tstl.LuaLibImportKind.None })
.disableSemanticCheck()
.expectDiagnosticsToMatchSnapshot([unsupportedForTarget.code]);
});
test.each(allBinaryOperators)("Bitop [5.1] (%p)", input => {
// Bit operations not supported in 5.1, expect an exception
util.testExpression(input)
.setOptions({ luaTarget: tstl.LuaTarget.Lua51, luaLibImport: tstl.LuaLibImportKind.None })
.disableSemanticCheck()
.expectDiagnosticsToMatchSnapshot([unsupportedForTarget.code]);
});
test.each(allBinaryOperators)("Bitop [JIT] (%p)", input => {
util.testExpression(input)
.setOptions({ luaTarget: tstl.LuaTarget.LuaJIT, luaLibImport: tstl.LuaLibImportKind.None })
.disableSemanticCheck()
.expectLuaToMatchSnapshot();
});
test.each(allBinaryOperators)("Bitop [5.2] (%p)", input => {
util.testExpression(input)
.setOptions({ luaTarget: tstl.LuaTarget.Lua52, luaLibImport: tstl.LuaLibImportKind.None })
.disableSemanticCheck()
.expectLuaToMatchSnapshot();
});
test.each(supportedInAll)("Bitop [5.3] (%p)", input => {
util.testExpression(input)
.setOptions({ luaTarget: tstl.LuaTarget.Lua53, luaLibImport: tstl.LuaLibImportKind.None })
.disableSemanticCheck()
.expectLuaToMatchSnapshot();
});
test.each(supportedInAll)("Bitop [5.4] (%p)", input => {
util.testExpression(input)
.setOptions({ luaTarget: tstl.LuaTarget.Lua54, luaLibImport: tstl.LuaLibImportKind.None })
.disableSemanticCheck()
.expectLuaToMatchSnapshot();
});
test.each(supportedInAll)("Bitop [5.5] (%p)", input => {
util.testExpression(input)
.setOptions({ luaTarget: tstl.LuaTarget.Lua55, luaLibImport: tstl.LuaLibImportKind.None })
.disableSemanticCheck()
.expectLuaToMatchSnapshot();
});
test.each(unsupportedIn53And54)("Unsupported bitop 5.3 (%p)", input => {
util.testExpression(input)
.setOptions({ luaTarget: tstl.LuaTarget.Lua53, luaLibImport: tstl.LuaLibImportKind.None })
.disableSemanticCheck()
.expectDiagnosticsToMatchSnapshot([unsupportedRightShiftOperator.code]);
});
test.each(unsupportedIn53And54)("Unsupported bitop 5.4 (%p)", input => {
util.testExpression(input)
.setOptions({ luaTarget: tstl.LuaTarget.Lua54, luaLibImport: tstl.LuaLibImportKind.None })
.disableSemanticCheck()
.expectDiagnosticsToMatchSnapshot([unsupportedRightShiftOperator.code]);
});
test.each(["1+1", "-1+1", "1*30+4", "1*(3+4)", "1*(3+4*2)", "10-(4+5)"])(
"Binary expressions ordering parentheses (%p)",
input => {
util.testExpression(input).expectLuaToMatchSnapshot();
}
);
test("Assignment order of operations is preserved", () => {
util.testFunction`
let x = 0;
x *= 2 + 3;
return x;
`.expectToMatchJsResult();
});
test.each(["bar(),foo()", "foo(),bar()", "foo(),bar(),baz()"])("Binary Comma (%p)", input => {
util.testFunction`
function foo() { return 1; }
function bar() { return 2; };
function baz() { return 3; };
return (${input});
`.expectToMatchJsResult();
});
test("Binary Comma Statement in For Loop", () => {
util.testFunction`
let x: number, y: number;
for (x = 0, y = 17; x < 5; ++x, --y) {}
return y;
`.expectToMatchJsResult();
});
test("Null Expression", () => {
util.testExpression("null").expectLuaToMatchSnapshot();
});
test("Undefined Expression", () => {
util.testExpression("undefined").expectLuaToMatchSnapshot();
});
test.each(["i++", "i--", "++i", "--i"])("Incrementor value (%p)", expression => {
util.testFunction`
let i = 10;
return ${expression};
`.expectToMatchJsResult();
});
test("Non-null expression", () => {
util.testFunction`
function abc(): number | undefined { return 3; }
const a: number = abc()!;
return a;
`.expectToMatchJsResult();
});
test("Typescript 4.7 instantiation expression", () => {
util.testFunction`
function foo<T>(x: T): T { return x; }
const bar = foo<number>;
return bar(3);
`.expectToMatchJsResult();
});
test("Typescript 4.9 satisfies expression", () => {
util.testFunction`
const foo = { a: 1 } satisfies { a: number };
return foo.a;
`.expectToMatchJsResult();
});
test.each([
'"foobar"',
"17",
"true",
"{}",
"[]",
"[].length",
"foo() + foo()",
"!foo()",
"foo()",
"typeof foo",
'"foo" in bar',
"foo as Function",
"Math.log2(2)",
"Math.log10(2)",
'"".indexOf("")',
])("Expression statements (%p)", input => {
util.testFunction`
function foo() { return 17; }
const bar = { foo };
${input};
`.expectNoExecutionError();
});