Skip to content

Commit 8578376

Browse files
authored
Remove legacy test utils (#957)
* Delete legacy-utils.ts * Updated some test suites to use new testbuilder * Added module capabilities to testbuilder * Fixed more test cases * Updated remaining test cases that used legacy utils * Fixed unused variable * Fix prettier * Fixed remaining tests Fixed function assignment test according to discord discussion (https://discord.com/channels/515854149821267971/600291243523702805/800403280512417792) Fixed hoisting by hardcoding the expected value, because the current TestBuilder.executeJS() setup does not handle module hoisting * Addressed review comments * Addressed 2nd review
1 parent a67b61a commit 8578376

32 files changed

+991
-1234
lines changed

test/json.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,4 @@ encode = function(val, stack)
142142
error("unexpected type '" .. t .. "'")
143143
end
144144

145-
146-
-- TODO: Since it supports NaN and Infinity it is considered a superset of JSON, so it probably should be renamed
147-
function JSONStringify(val)
148-
return ( encode(val) )
149-
end
145+
return {stringify = function(val) return ( encode(val) ) end}

test/legacy-utils.ts

Lines changed: 0 additions & 168 deletions
This file was deleted.

test/transpile/bundle.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ test("should transpile into one file", () => {
1515
// Verify the name is as specified in tsconfig
1616
expect(name).toBe("bundle/bundle.lua");
1717
// Verify exported module by executing
18-
expect(util.executeLuaModule(text)).toEqual({ myNumber: 3 });
18+
// Use an empty TS string because we already transpiled the TS project
19+
util.testModule("").setLuaHeader(text).expectToEqual({ myNumber: 3 });
1920
});

test/unit/__snapshots__/expressions.spec.ts.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ ____exports.__result = bit.bor(a, b)
431431
return ____exports"
432432
`;
433433
434+
exports[`Null Expression 1`] = `
435+
"local ____exports = {}
436+
____exports.__result = nil
437+
return ____exports"
438+
`;
439+
434440
exports[`Unary expressions basic ("!a") 1`] = `
435441
"local ____exports = {}
436442
function ____exports.__main(self)
@@ -523,6 +529,12 @@ end
523529
return ____exports"
524530
`;
525531
532+
exports[`Undefined Expression 1`] = `
533+
"local ____exports = {}
534+
____exports.__result = nil
535+
return ____exports"
536+
`;
537+
526538
exports[`Unsupported bitop 5.3 ("a>>=b"): code 1`] = `
527539
"local ____exports = {}
528540
____exports.__result = (function()

test/unit/__snapshots__/identifiers.spec.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ exports[`ambient identifier must be a valid lua identifier (object literal short
128128

129129
exports[`ambient identifier must be a valid lua identifier (object literal shorthand) ("ɥɣɎɌͼƛಠ"): diagnostics 1`] = `"main.ts(3,27): error TSTL: Invalid ambient identifier name 'ɥɣɎɌͼƛಠ'. Ambient identifiers must be valid lua identifiers."`;
130130

131+
exports[`declaration-only variable with lua keyword as name is not renamed 1`] = `
132+
"local ____exports = {}
133+
function ____exports.__main(self)
134+
type(7)
135+
end
136+
return ____exports"
137+
`;
138+
131139
exports[`undeclared identifier must be a valid lua identifier ("$$"): code 1`] = `"foo = _____24_24_24"`;
132140

133141
exports[`undeclared identifier must be a valid lua identifier ("$$"): diagnostics 1`] = `"main.ts(2,21): error TSTL: Invalid ambient identifier name '$$$'. Ambient identifiers must be valid lua identifiers."`;

test/unit/annotations/customConstructor.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ test("CustomCreate", () => {
1919
}
2020
`;
2121

22-
const result = util.transpileAndExecute("return new Point2D(1, 2).x;", undefined, luaHeader, tsHeader);
23-
24-
expect(result).toBe(1);
22+
// Can't use expectToMatchJsResult because above is not valid TS/JS
23+
util.testModule`export default new Point2D(1, 2).x;`
24+
.setTsHeader(tsHeader)
25+
.setLuaHeader(luaHeader)
26+
.setReturnExport("default")
27+
.expectToEqual(1);
2528
});
2629

2730
test("IncorrectUsage", () => {

test/unit/annotations/luaIterator.spec.ts

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as util from "../../util";
22
import { luaIteratorForbiddenUsage } from "../../../src/transformation/utils/diagnostics";
33

44
test("forof lua iterator", () => {
5-
const code = `
5+
util.testFunction`
66
const arr = ["a", "b", "c"];
77
/** @luaIterator */
88
interface Iter extends Iterable<string> {}
@@ -13,13 +13,11 @@ test("forof lua iterator", () => {
1313
let result = "";
1414
for (let e of luaIter()) { result += e; }
1515
return result;
16-
`;
17-
const result = util.transpileAndExecute(code);
18-
expect(result).toBe("abc");
16+
`.expectToEqual("abc");
1917
});
2018

2119
test("forof array lua iterator", () => {
22-
const code = `
20+
util.testFunction`
2321
const arr = ["a", "b", "c"];
2422
/** @luaIterator */
2523
interface Iter extends Array<string> {}
@@ -30,13 +28,11 @@ test("forof array lua iterator", () => {
3028
let result = "";
3129
for (let e of luaIter()) { result += e; }
3230
return result;
33-
`;
34-
const result = util.transpileAndExecute(code);
35-
expect(result).toBe("abc");
31+
`.expectToEqual("abc");
3632
});
3733

3834
test("forof lua iterator with existing variable", () => {
39-
const code = `
35+
util.testFunction`
4036
const arr = ["a", "b", "c"];
4137
/** @luaIterator */
4238
interface Iter extends Iterable<string> {}
@@ -48,13 +44,11 @@ test("forof lua iterator with existing variable", () => {
4844
let e: string;
4945
for (e of luaIter()) { result += e; }
5046
return result;
51-
`;
52-
const result = util.transpileAndExecute(code);
53-
expect(result).toBe("abc");
47+
`.expectToEqual("abc");
5448
});
5549

5650
test("forof lua iterator destructuring", () => {
57-
const code = `
51+
util.testFunction`
5852
const arr = ["a", "b", "c"];
5953
/** @luaIterator */
6054
interface Iter extends Iterable<[string, string]> {}
@@ -65,13 +59,11 @@ test("forof lua iterator destructuring", () => {
6559
let result = "";
6660
for (let [a, b] of luaIter()) { result += a + b; }
6761
return result;
68-
`;
69-
const result = util.transpileAndExecute(code);
70-
expect(result).toBe("0a1b2c");
62+
`.expectToEqual("0a1b2c");
7163
});
7264

7365
test("forof lua iterator destructuring with existing variables", () => {
74-
const code = `
66+
util.testFunction`
7567
const arr = ["a", "b", "c"];
7668
/** @luaIterator */
7769
interface Iter extends Iterable<[string, string]> {}
@@ -84,13 +76,11 @@ test("forof lua iterator destructuring with existing variables", () => {
8476
let b: string;
8577
for ([a, b] of luaIter()) { result += a + b; }
8678
return result;
87-
`;
88-
const result = util.transpileAndExecute(code);
89-
expect(result).toBe("0a1b2c");
79+
`.expectToEqual("0a1b2c");
9080
});
9181

9282
test("forof lua iterator tuple-return", () => {
93-
const code = `
83+
util.testFunction`
9484
const arr = ["a", "b", "c"];
9585
/**
9686
* @luaIterator
@@ -106,13 +96,11 @@ test("forof lua iterator tuple-return", () => {
10696
let result = "";
10797
for (let [a, b] of luaIter()) { result += a + b; }
10898
return result;
109-
`;
110-
const result = util.transpileAndExecute(code);
111-
expect(result).toBe("0a1b2c");
99+
`.expectToEqual("0a1b2c");
112100
});
113101

114102
test("forof lua iterator tuple-return with existing variables", () => {
115-
const code = `
103+
util.testFunction`
116104
const arr = ["a", "b", "c"];
117105
/**
118106
* @luaIterator
@@ -130,9 +118,7 @@ test("forof lua iterator tuple-return with existing variables", () => {
130118
let b: string;
131119
for ([a, b] of luaIter()) { result += a + b; }
132120
return result;
133-
`;
134-
const result = util.transpileAndExecute(code);
135-
expect(result).toBe("0a1b2c");
121+
`.expectToEqual("0a1b2c");
136122
});
137123

138124
test("forof lua iterator tuple-return single variable", () => {
@@ -161,7 +147,7 @@ test("forof lua iterator tuple-return single existing variable", () => {
161147
});
162148

163149
test("forof forwarded lua iterator", () => {
164-
const code = `
150+
util.testFunction`
165151
const arr = ["a", "b", "c"];
166152
/** @luaIterator */
167153
interface Iter extends Iterable<string> {}
@@ -177,13 +163,11 @@ test("forof forwarded lua iterator", () => {
177163
let result = "";
178164
for (let a of forward()) { result += a; }
179165
return result;
180-
`;
181-
const result = util.transpileAndExecute(code);
182-
expect(result).toBe("abc");
166+
`.expectToEqual("abc");
183167
});
184168

185169
test("forof forwarded lua iterator with tupleReturn", () => {
186-
const code = `
170+
util.testFunction`
187171
const arr = ["a", "b", "c"];
188172
/**
189173
* @luaIterator
@@ -203,7 +187,5 @@ test("forof forwarded lua iterator with tupleReturn", () => {
203187
let result = "";
204188
for (let [a, b] of forward()) { result += a + b; }
205189
return result;
206-
`;
207-
const result = util.transpileAndExecute(code);
208-
expect(result).toBe("0a1b2c");
190+
`.expectToEqual("0a1b2c");
209191
});

0 commit comments

Comments
 (0)