Skip to content

Commit c562810

Browse files
committed
fix more tests
1 parent d51a82b commit c562810

9 files changed

Lines changed: 36 additions & 32 deletions

test/unit/assignments.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ test.each([
342342

343343
test("local variable declaration referencing self indirectly", () => {
344344
util.testFunction`
345-
let cb: () => void;
345+
let cb: () => void = () => { throw "Expecting this to be overwritten"; };
346346
347347
function foo(newCb: () => void) {
348348
cb = newCb;
@@ -360,7 +360,7 @@ test("local variable declaration referencing self indirectly", () => {
360360

361361
test("local multiple variable declaration referencing self indirectly", () => {
362362
util.testFunction`
363-
let cb: () => void;
363+
let cb: () => void = () => { throw "Expecting this to be overwritten"; };
364364
365365
function foo(newCb: () => void) {
366366
cb = newCb;
@@ -395,7 +395,7 @@ describe.each(["x &&= y", "x ||= y"])("boolean compound assignment (%p)", assign
395395

396396
test.each([undefined, 3])("nullish coalescing compound assignment", initialValue => {
397397
util.testFunction`
398-
let x: number = ${util.formatCode(initialValue)};
398+
let x: number | undefined = ${util.formatCode(initialValue)};
399399
x ??= 5;
400400
return x;
401401
`.expectToMatchJsResult();

test/unit/conditionals.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ test.each([
7575
])("Ternary operator (%p)", ({ input, options }) => {
7676
util.testFunction`
7777
const literalValue = "literal";
78-
let variableValue: string;
78+
let variableValue: string = "variable";
7979
let maybeBooleanValue: string | boolean = false;
8080
let maybeUndefinedValue: string | undefined;
8181
return ${input};

test/unit/enum.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as util from "../util";
44
const serializeEnum = (identifier: string) => `(() => {
55
const mappedTestEnum: any = {};
66
for (const key in ${identifier}) {
7-
mappedTestEnum[(key as any).toString()] = ${identifier}[key];
7+
mappedTestEnum[(key as any).toString()] = (${identifier} as any)[key];
88
}
99
return mappedTestEnum;
1010
})()`;

test/unit/error.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ test("multi return from try", () => {
8888
} catch {
8989
}
9090
}
91-
const [foo, bar] = foobar();
91+
const [foo, bar] = foobar()!;
9292
return foo + bar;
9393
`.withLanguageExtensions();
9494
expect(testBuilder.getMainLuaCodeChunk()).not.toMatch("unpack(foobar");
@@ -129,7 +129,7 @@ test("multi return from catch", () => {
129129
function foobar(): LuaMultiReturn<[string, string]> {
130130
try {
131131
throw "foobar";
132-
} catch (e) {
132+
} catch (e: any) {
133133
return $multi(e.toString(), " catch");
134134
}
135135
}
@@ -243,7 +243,7 @@ test("multi return from catch->finally", () => {
243243
function foobar() {
244244
try {
245245
throw "foo";
246-
} catch (e) {
246+
} catch (e: any) {
247247
return $multi(evaluate(e), "bar");
248248
} finally {
249249
return $multi("final", "ly");

test/unit/functions/functions.spec.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { unsupportedForTarget } from "../../../src/transformation/utils/diagnost
55

66
test("Arrow Function Expression", () => {
77
util.testFunction`
8-
const add = (a, b) => a + b;
8+
const add = (a: number, b: number) => a + b;
99
return add(1, 2);
1010
`.expectToMatchJsResult();
1111
});
@@ -25,17 +25,22 @@ test.each(["i++", "i--", "++i", "--i"])("Arrow function unary expression (%p)",
2525
`.expectToMatchJsResult();
2626
});
2727

28-
test.each(["b => a = b", "b => a += b", "b => a -= b", "b => a *= b", "b => a /= b", "b => a **= b", "b => a %= b"])(
29-
"Arrow function assignment (%p)",
30-
lambda => {
31-
util.testFunction`
28+
test.each([
29+
"(b: number) => a = b",
30+
"(b: number) => a += b",
31+
"(b: number) => a -= b",
32+
"(b: number) => a *= b",
33+
"(b: number) => a /= b",
34+
"(b: number) => a **= b",
35+
"(b: number) => a %= b",
36+
])("Arrow function assignment (%p)", lambda => {
37+
util.testFunction`
3238
let a = 10;
3339
let lambda = ${lambda};
3440
lambda(5);
3541
return a;
3642
`.expectToMatchJsResult();
37-
}
38-
);
43+
});
3944

4045
test.each([{ args: [] }, { args: [1] }, { args: [1, 2] }])("Arrow default values (%p)", ({ args }) => {
4146
util.testFunction`
@@ -46,7 +51,7 @@ test.each([{ args: [] }, { args: [1] }, { args: [1, 2] }])("Arrow default values
4651

4752
test("Function Expression", () => {
4853
util.testFunction`
49-
let add = function(a, b) {return a+b};
54+
let add = function(a: number, b: number) {return a+b};
5055
return add(1,2);
5156
`.expectToMatchJsResult();
5257
});
@@ -251,13 +256,13 @@ test.each(functionTypeDeclarations)("Function call (%s)", (_, type) => {
251256
});
252257

253258
test.each([
254-
"function fn() {}",
255-
"function fn(x, y, z) {}",
256-
"function fn(x, y, z, ...args) {}",
257-
"function fn(...args) {}",
258-
"function fn(this: void) {}",
259-
"function fn(this: void, x, y, z) {}",
260-
"function fnReference(x, y, z) {} const fn = fnReference;",
259+
"function fn(): void {}",
260+
"function fn(x: any, y: any, z: any): void {}",
261+
"function fn(x: any, y: any, z: any, ...args: any[]): void {}",
262+
"function fn(...args: any[]): void {}",
263+
"function fn(this: void): void {}",
264+
"function fn(this: void, x: any, y: any, z: any): void {}",
265+
"function fnReference(x: any, y: any, z: any): void {} const fn = fnReference;",
261266
"const wrap = (fn: (...args: any[]) => any) => (...args: any[]) => fn(...args); const fn = wrap((x, y, z) => {});",
262267
])("function.length (%p)", declaration => {
263268
util.testFunction`
@@ -432,7 +437,7 @@ test("Complex element access call no args", () => {
432437

433438
test("Complex element access call statement", () => {
434439
util.testFunction`
435-
let foo: string;
440+
let foo: string | undefined;
436441
class C {
437442
prop = "bar";
438443
method(s: string) { foo = s + this.prop; }

test/unit/hoisting.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ test("Hoisting variable without initializer", () => {
254254
function foo() {
255255
return x;
256256
}
257-
let x: number;
257+
let x: number | undefined;
258258
return foo();
259259
`.expectToMatchJsResult();
260260
});

test/unit/identifiers.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ test.each([
9393
"const foo: any, bar: any, $$$: any;",
9494
"class $$$ {}",
9595
"namespace $$$ { export const bar: any; }",
96-
"module $$$ { export const bar: any; }",
9796
"enum $$$ {}",
98-
"function $$$();",
97+
"function $$$(): void;",
9998
])("ambient identifier must be a valid lua identifier (%p)", statement => {
10099
util.testModule`
101100
declare ${statement}

test/unit/loops.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ test.each([
208208
},
209209
])("forin[Object] (%p)", ({ inp }) => {
210210
util.testFunctionTemplate`
211-
let objTest = ${inp};
211+
let objTest: Record<string, number> = ${inp};
212212
for (let key in objTest) {
213213
objTest[key] = objTest[key] + 1;
214214
}
@@ -218,7 +218,7 @@ test.each([
218218

219219
test("forin[Array]", () => {
220220
util.testFunction`
221-
const array = [];
221+
const array = [1,2,3];
222222
for (const key in array) {}
223223
`.expectDiagnosticsToMatchSnapshot([forbiddenForIn.code]);
224224
});
@@ -238,7 +238,7 @@ test.each(
238238
)
239239
)("forin with continue (%s %p)", (luaTarget, { inp }) => {
240240
util.testFunctionTemplate`
241-
let obj = ${inp};
241+
let obj: Record<string, number> = ${inp};
242242
for (let i in obj) {
243243
if (obj[i] % 2 == 0) {
244244
continue;

test/unit/precedingStatements.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,8 @@ describe("assignment execution order", () => {
516516
test("function method call", () => {
517517
util.testFunction`
518518
let o = {val: 3};
519-
let a = function(x: number) { return this.val + x; };
520-
let b = function(x: number) { return (this.val + x) * 10; };
519+
let a = function(this: typeof o, x: number) { return this.val + x; };
520+
let b = function(this: typeof o, x: number) { return (this.val + x) * 10; };
521521
function foo(x: number) { return (x > 0) ? b : a; }
522522
let i = 0;
523523
const result = foo(i).call(o, i++);

0 commit comments

Comments
 (0)