forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionals.spec.ts
More file actions
101 lines (94 loc) · 3.16 KB
/
Copy pathconditionals.spec.ts
File metadata and controls
101 lines (94 loc) · 3.16 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
import * as tstl from "../../src";
import * as util from "../util";
test.each([0, 1])("if (%p)", inp => {
util.testFunction`
let input: number = ${inp};
if (input === 0) {
return 0;
}
return 1;
`.expectToMatchJsResult();
});
test.each([0, 1])("ifelse (%p)", inp => {
util.testFunction`
let input: number = ${inp};
if (input === 0) {
return 0;
} else {
return 1;
}
`.expectToMatchJsResult();
});
test.each([0, 1, 2, 3])("ifelseif (%p)", inp => {
util.testFunction`
let input: number = ${inp};
if (input === 0) {
return 0;
} else if (input === 1){
return 1;
} else if (input === 2){
return 2;
}
return 3;
`.expectToMatchJsResult();
});
test.each([0, 1, 2, 3])("ifelseifelse (%p)", inp => {
util.testFunction`
let input: number = ${inp};
if (input === 0) {
return 0;
} else if (input === 1){
return 1;
} else if (input === 2){
return 2;
} else {
return 3;
}
`.expectToMatchJsResult();
});
test.each([
{ input: "true ? 'a' : 'b'" },
{ input: "false ? 'a' : 'b'" },
{ input: "true ? false : true" },
{ input: "false ? false : true" },
{ input: "true || true ? 'a' : 'b'" },
{ input: "true || false ? 'a' : 'b'" },
{ input: "false || true ? 'a' : 'b'" },
{ input: "false || false ? 'a' : 'b'" },
{ input: "true ? literalValue : true" },
{ input: "true ? variableValue : true" },
{ input: "true ? maybeUndefinedValue : true" },
{ input: "true ? maybeBooleanValue : true" },
{ input: "true ? maybeUndefinedValue : true", options: { strictNullChecks: true } },
{ input: "true ? maybeBooleanValue : true", options: { strictNullChecks: true } },
{ input: "true ? undefined : true", options: { strictNullChecks: true } },
{ input: "true ? null : true", options: { strictNullChecks: true } },
{ input: "true ? false : true", options: { luaTarget: tstl.LuaTarget.Lua51 } },
{ input: "false ? false : true", options: { luaTarget: tstl.LuaTarget.Lua51 } },
{ input: "true ? undefined : true", options: { luaTarget: tstl.LuaTarget.Lua51 } },
])("Ternary operator (%p)", ({ input, options }) => {
util.testFunction`
const literalValue = "literal";
let variableValue: string;
let maybeBooleanValue: string | boolean = false;
let maybeUndefinedValue: string | undefined;
return ${input};
`
.setOptions(options)
.expectToMatchJsResult();
});
test.each([
{ condition: true, lhs: 4, rhs: 5 },
{ condition: false, lhs: 4, rhs: 5 },
{ condition: 3, lhs: 4, rhs: 5 },
])("Ternary Conditional (%p)", ({ condition, lhs, rhs }) => {
util.testExpressionTemplate`${condition} ? ${lhs} : ${rhs}`.expectToMatchJsResult();
});
test.each(["true", "false", "a < 4", "a == 8"])("Ternary Conditional Delayed (%p)", condition => {
util.testFunction`
let a = 3;
let delay = () => ${condition} ? a + 3 : a + 5;
a = 8;
return delay();
`.expectToMatchJsResult();
});