|
1 | 1 | import * as util from "../util"; |
2 | 2 |
|
3 | | -test.each([ |
4 | | - { inp: `{a:3,b:"4"}`, out: '{a = 3, b = "4"}' }, |
5 | | - { inp: `{"a":3,b:"4"}`, out: '{a = 3, b = "4"}' }, |
6 | | - { inp: `{["a"]:3,b:"4"}`, out: '{a = 3, b = "4"}' }, |
7 | | - { inp: `{["a"+123]:3,b:"4"}`, out: '{["a" .. 123] = 3, b = "4"}' }, |
8 | | - { inp: `{[myFunc()]:3,b:"4"}`, out: '{\n [myFunc(_G)] = 3,\n b = "4"\n}' }, |
9 | | - { inp: `{x}`, out: `{x = x}` }, |
10 | | -])("Object Literal (%p)", ({ inp, out }) => { |
11 | | - const lua = util.transpileString(`const myvar = ${inp};`); |
12 | | - expect(lua).toBe(`local myvar = ${out}`); |
| 3 | +test.each([`{ a: 3, b: "4" }`, `{ "a": 3, b: "4" }`, `{ ["a"]: 3, b: "4" }`, `{ ["a" + 123]: 3, b: "4" }`])( |
| 4 | + "Object Literal (%p)", |
| 5 | + inp => { |
| 6 | + util.testExpression(inp).expectToMatchJsResult(); |
| 7 | + } |
| 8 | +); |
| 9 | + |
| 10 | +test("object literal with function call to get key", () => { |
| 11 | + util.testFunction` |
| 12 | + const myFunc = () => "a"; |
| 13 | + return { [myFunc() + "b"]: 3 }; |
| 14 | + `.expectToMatchJsResult(); |
| 15 | +}); |
| 16 | + |
| 17 | +test("object literal with shorthand property", () => { |
| 18 | + util.testFunction` |
| 19 | + const x = 5; |
| 20 | + return { x }; |
| 21 | + `.expectToMatchJsResult(); |
13 | 22 | }); |
14 | 23 |
|
15 | 24 | describe("property shorthand", () => { |
16 | 25 | test("should support property shorthand", () => { |
17 | | - const result = util.transpileAndExecute(` |
| 26 | + util.testFunction` |
18 | 27 | const x = 1; |
19 | 28 | const o = { x }; |
20 | 29 | return o.x; |
21 | | - `); |
22 | | - |
23 | | - expect(result).toBe(1); |
| 30 | + `.expectToMatchJsResult(); |
24 | 31 | }); |
25 | 32 |
|
26 | 33 | test.each([NaN, Infinity])("should support %p shorthand", identifier => { |
27 | | - const result = util.transpileAndExecute(`return ({ ${identifier} }).${identifier}`); |
28 | | - |
29 | | - expect(result).toBe(identifier); |
| 34 | + util.testExpression`({ ${identifier} }).${identifier}`.expectToMatchJsResult(); |
30 | 35 | }); |
31 | 36 |
|
32 | 37 | test("should support _G shorthand", () => { |
33 | | - const result = util.transpileAndExecute( |
34 | | - `return ({ _G })._G.foobar;`, |
35 | | - undefined, |
36 | | - `foobar = "foobar"`, |
37 | | - "declare const _G: any;" |
38 | | - ); |
39 | | - |
40 | | - expect(result).toBe("foobar"); |
| 38 | + util.testExpression`({ _G })._G.foobar` |
| 39 | + .setTsHeader(`declare const _G: any;`) |
| 40 | + .setLuaHeader(`foobar = "foobar"`) |
| 41 | + .expectToEqual("foobar"); |
41 | 42 | }); |
42 | 43 |
|
43 | 44 | test("should support export property shorthand", () => { |
44 | | - const code = ` |
| 45 | + util.testModule` |
45 | 46 | export const x = 1; |
46 | 47 | const o = { x }; |
47 | 48 | export const y = o.x; |
48 | | - `; |
49 | | - expect(util.transpileExecuteAndReturnExport(code, "y")).toBe(1); |
| 49 | + `.expectToMatchJsResult(); |
50 | 50 | }); |
51 | 51 | }); |
52 | 52 |
|
53 | 53 | test("undefined as object key", () => { |
54 | | - const code = `const foo = {undefined: "foo"}; |
55 | | - return foo.undefined;`; |
56 | | - expect(util.transpileAndExecute(code)).toBe("foo"); |
| 54 | + util.testFunction` |
| 55 | + const foo = {undefined: "foo"}; |
| 56 | + return foo.undefined; |
| 57 | + `.expectToMatchJsResult(); |
57 | 58 | }); |
58 | 59 |
|
59 | 60 | test.each([`({x: "foobar"}.x)`, `({x: "foobar"}["x"])`, `({x: () => "foobar"}.x())`, `({x: () => "foobar"}["x"]())`])( |
60 | 61 | "object literal property access (%p)", |
61 | 62 | expression => { |
62 | | - const code = `return ${expression}`; |
63 | | - const expectResult = eval(expression); |
64 | | - expect(util.transpileAndExecute(code)).toBe(expectResult); |
| 63 | + util.testExpression(expression).expectToMatchJsResult(); |
65 | 64 | } |
66 | 65 | ); |
0 commit comments