Skip to content

Commit 2f96f64

Browse files
committed
fix more tests
1 parent ed817d7 commit 2f96f64

File tree

15 files changed

+154
-145
lines changed

15 files changed

+154
-145
lines changed

test/unit/builtins/array.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,16 @@ test.each([
421421
{ array: [0, 1, 2, 3], start: 1, deleteCount: undefined },
422422
{ array: [0, 1, 2, 3], start: 1, deleteCount: null },
423423
])("array.splice (%p)", ({ array, start, deleteCount, newElements = [] }) => {
424+
const deleteCountCode =
425+
deleteCount === undefined
426+
? "undefined as any"
427+
: deleteCount === null
428+
? "null as any"
429+
: util.formatCode(deleteCount);
430+
const newElementsCode = newElements.length > 0 ? ", " + util.formatCode(...newElements) : "";
424431
util.testFunction`
425432
const array: number[] = ${util.formatCode(array)};
426-
array.splice(${util.formatCode(start, deleteCount, ...newElements)});
433+
array.splice(${util.formatCode(start)}, ${deleteCountCode}${newElementsCode});
427434
return array;
428435
`.expectToMatchJsResult();
429436
});

test/unit/builtins/async-await.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test("high amount of chained awaits doesn't cause stack overflow", () => {
3838
await delay();
3939
}
4040
result = "success";
41-
} catch (e) {
41+
} catch (e: any) {
4242
result = e;
4343
}
4444
}
@@ -51,7 +51,7 @@ test("high amount of chained awaits doesn't cause stack overflow", () => {
5151

5252
test("can await already resolved promise", () => {
5353
util.testFunction`
54-
const result = [];
54+
const result: unknown[] = [];
5555
async function abc() {
5656
return await Promise.resolve(30);
5757
}
@@ -63,7 +63,7 @@ test("can await already resolved promise", () => {
6363

6464
test("can await already rejected promise", () => {
6565
util.testFunction`
66-
const result = [];
66+
const result: unknown[] = [];
6767
async function abc() {
6868
return await Promise.reject("test rejection");
6969
}
@@ -604,7 +604,7 @@ describe("try/catch in async function", () => {
604604
() =>
605605
util.testModule`
606606
export let reason = "";
607-
let reject: (reason: string) => void;
607+
let reject: (reason: string) => void = () => { throw "should be overridden!"; }
608608
609609
async function foo(): Promise<number> {
610610
try {
@@ -675,6 +675,7 @@ describe("try/catch in async function", () => {
675675
}
676676
catch
677677
{
678+
return "catch";
678679
}
679680
}
680681

test/unit/builtins/map.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe.each(iterationMethods)("map.%s() handles mutation", iterationMethod =>
251251
describe("Map.groupBy", () => {
252252
test("empty", () => {
253253
util.testFunction`
254-
const array = [];
254+
const array: number[] = [];
255255
256256
const map = Map.groupBy(array, (num, index) => {
257257
return num % 2 === 0 ? "even": "odd";

test/unit/builtins/object.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe("Object.defineProperty", () => {
147147

148148
test.each(trueFalseTests)("configurable (%p)", value => {
149149
util.testFunction`
150-
const foo = { bar: true };
150+
const foo: { bar?: boolean } = { bar: true };
151151
Object.defineProperty(foo, "bar", { configurable: ${value} });
152152
try { delete foo.bar } catch {};
153153
return foo.bar;
@@ -296,7 +296,7 @@ describe("Object.getOwnPropertyDescriptors", () => {
296296
describe("delete from object", () => {
297297
test("delete from object", () => {
298298
util.testFunction`
299-
const obj = { foo: "bar", bar: "baz" };
299+
const obj: { foo?: string, bar: string } = { foo: "bar", bar: "baz" };
300300
return [delete obj["foo"], obj];
301301
`.expectToMatchJsResult();
302302
});
@@ -311,7 +311,7 @@ describe("delete from object", () => {
311311
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/993
312312
test("delete from object with metatable", () => {
313313
util.testFunction`
314-
const obj = { foo: "bar", bar: "baz" };
314+
const obj: { foo?: string, bar: string } = { foo: "bar", bar: "baz" };
315315
setmetatable(obj, {});
316316
return [delete obj["foo"], obj];
317317
`
@@ -343,7 +343,7 @@ describe("delete from object", () => {
343343
describe("Object.groupBy", () => {
344344
test("empty", () => {
345345
util.testFunction`
346-
const array = [];
346+
const array: number[] = [];
347347
348348
return Object.groupBy(array, (num, index) => {
349349
return num % 2 === 0 ? "even": "odd";

test/unit/builtins/set.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test("set has false", () => {
100100

101101
test("set has null", () => {
102102
util.testFunction`
103-
let myset = new Set(["a", "c"]);
103+
let myset = new Set<string | null>(["a", "c"]);
104104
return myset.has(null);
105105
`.expectToMatchJsResult();
106106
});

test/unit/builtins/string.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe.each(["replace", "replaceAll"])("string.%s", method => {
8484
test.each(testCases)("function replacer %p", ({ inp, searchValue, replaceValue }) => {
8585
util.testFunction`
8686
const result = {
87-
args: [],
87+
args: [] as string[],
8888
string: ""
8989
}
9090
function replacer(...args: any[]): string {

test/unit/builtins/weakMap.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ test("weakMap has null", () => {
7474
util.testFunction`
7575
${initRefsTs}
7676
let mymap = new WeakMap([[{}, true]]);
77-
return mymap.has(null);
77+
return mymap.has(null as any);
7878
`.expectToMatchJsResult();
7979
});
8080

test/unit/classes/classes.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ test("get inherted __index member from super (DotA 2 inheritance) (#1537)", () =
894894
interface Connected extends I {}
895895
class Connected {}
896896
897-
declare function setmetatable(this: void, t: any, mt: any);
897+
declare function setmetatable(this: void, t: any, mt: any): void;
898898
899899
const A = {
900900
foo() {

test/unit/classes/decorators.spec.ts

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ test("Class decorator with no parameters", () => {
2222
}
2323
2424
return { decoratedClass: new TestClass(), context: {
25-
kind: classDecoratorContext.kind,
26-
name: classDecoratorContext.name,
25+
kind: classDecoratorContext!.kind,
26+
name: classDecoratorContext!.name,
2727
} };
2828
`.expectToMatchJsResult();
2929
});
@@ -40,7 +40,7 @@ test("Class decorator with parameters", () => {
4040
4141
@setNum(420)
4242
class TestClass {
43-
public decoratorNum;
43+
public decoratorNum?: number;
4444
}
4545
4646
return new TestClass();
@@ -64,8 +64,8 @@ test("Multiple class decorators", () => {
6464
@setTen
6565
@setNum
6666
class TestClass {
67-
public decoratorTen;
68-
public decoratorNum;
67+
public decoratorTen?: number;
68+
public decoratorNum?: number;
6969
}
7070
7171
return new TestClass();
@@ -95,7 +95,7 @@ test("Class decorator with inheritance", () => {
9595

9696
test("Class decorators are applied in order and executed in reverse order", () => {
9797
util.testFunction`
98-
const order = [];
98+
const order: string[] = [];
9999
100100
function pushOrder(index: number) {
101101
order.push("eval " + index);
@@ -232,18 +232,18 @@ test("class method decorator", () => {
232232
}
233233
234234
return { result: new TestClass().myMethod(4), context: {
235-
kind: methodDecoratorContext.kind,
236-
name: methodDecoratorContext.name,
237-
private: methodDecoratorContext.private,
238-
static: methodDecoratorContext.static
235+
kind: methodDecoratorContext!.kind,
236+
name: methodDecoratorContext!.name,
237+
private: methodDecoratorContext!.private,
238+
static: methodDecoratorContext!.static
239239
} };
240240
`.expectToMatchJsResult();
241241
});
242242

243243
test("this in decorator points to class being decorated", () => {
244244
util.testFunction`
245245
function methodDecorator(method: (v: number) => number, context: ClassMethodDecoratorContext) {
246-
return function() {
246+
return function(this: TestClass) {
247247
const thisCallTime = this.myInstanceVariable;
248248
return thisCallTime;
249249
};
@@ -280,10 +280,10 @@ test("class getter decorator", () => {
280280
}
281281
282282
return { result: new TestClass().getterValue, context: {
283-
kind: getterDecoratorContext.kind,
284-
name: getterDecoratorContext.name,
285-
private: getterDecoratorContext.private,
286-
static: getterDecoratorContext.static
283+
kind: getterDecoratorContext!.kind,
284+
name: getterDecoratorContext!.name,
285+
private: getterDecoratorContext!.private,
286+
static: getterDecoratorContext!.static
287287
} };
288288
`.expectToMatchJsResult();
289289
});
@@ -295,13 +295,13 @@ test("class setter decorator", () => {
295295
function setterDecorator(setter: (v: number) => void, context: ClassSetterDecoratorContext) {
296296
setterDecoratorContext = context;
297297
298-
return function(v: number) {
298+
return function(this: TestClass, v: number) {
299299
setter.call(this, v + 15);
300300
};
301301
}
302302
303303
class TestClass {
304-
public value: number;
304+
public value?: number;
305305
306306
@setterDecorator
307307
set valueSetter(v: number) { this.value = v; }
@@ -310,10 +310,10 @@ test("class setter decorator", () => {
310310
const instance = new TestClass();
311311
instance.valueSetter = 23;
312312
return { result: instance.value, context: {
313-
kind: setterDecoratorContext.kind,
314-
name: setterDecoratorContext.name,
315-
private: setterDecoratorContext.private,
316-
static: setterDecoratorContext.static
313+
kind: setterDecoratorContext!.kind,
314+
name: setterDecoratorContext!.name,
315+
private: setterDecoratorContext!.private,
316+
static: setterDecoratorContext!.static
317317
} };
318318
`.expectToMatchJsResult();
319319
});
@@ -332,10 +332,10 @@ test("class field decorator", () => {
332332
}
333333
334334
return { result: new TestClass(), context: {
335-
kind: fieldDecoratorContext.kind,
336-
name: fieldDecoratorContext.name,
337-
private: fieldDecoratorContext.private,
338-
static: fieldDecoratorContext.static,
335+
kind: fieldDecoratorContext!.kind,
336+
name: fieldDecoratorContext!.name,
337+
private: fieldDecoratorContext!.private,
338+
static: fieldDecoratorContext!.static,
339339
} };
340340
`.expectToEqual({
341341
result: {
@@ -399,7 +399,7 @@ describe("legacy experimentalDecorators", () => {
399399
400400
@setNum(420)
401401
class TestClass {
402-
public decoratorNum;
402+
public decoratorNum?: number;
403403
}
404404
405405
return new TestClass();
@@ -425,8 +425,8 @@ describe("legacy experimentalDecorators", () => {
425425
@setTen
426426
@setNum
427427
class TestClass {
428-
public decoratorTen;
429-
public decoratorNum;
428+
public decoratorTen?: number;
429+
public decoratorNum?: number;
430430
}
431431
432432
return new TestClass();
@@ -460,7 +460,7 @@ describe("legacy experimentalDecorators", () => {
460460

461461
test("Class decorators are applied in order and executed in reverse order", () => {
462462
util.testFunction`
463-
const order = [];
463+
const order: string[] = [];
464464
465465
function pushOrder(index: number) {
466466
order.push("eval " + index);
@@ -509,25 +509,25 @@ describe("legacy experimentalDecorators", () => {
509509

510510
test.each([
511511
["@decorator method() {}"],
512-
["@decorator property;"],
512+
["@decorator property: any;"],
513513
["@decorator propertyWithInitializer = () => {};"],
514-
["@decorator ['evaluated property'];"],
514+
["@decorator ['evaluated property']: any;"],
515515
["@decorator get getter() { return 5 }"],
516-
["@decorator set setter(value) {}"],
516+
["@decorator set setter(value: any) {}"],
517517
["@decorator static method() {}"],
518-
["@decorator static property;"],
518+
["@decorator static property: any;"],
519519
["@decorator static propertyWithInitializer = () => {}"],
520520
["@decorator static get getter() { return 5 }"],
521-
["@decorator static set setter(value) {}"],
522-
["@decorator static ['evaluated property'];"],
523-
["method(@decorator a) {}"],
524-
["static method(@decorator a) {}"],
525-
["constructor(@decorator a) {}"],
521+
["@decorator static set setter(value: any) {}"],
522+
["@decorator static ['evaluated property']: any;"],
523+
["method(@decorator a: any) {}"],
524+
["static method(@decorator a: any) {}"],
525+
["constructor(@decorator a: any) {}"],
526526
])("Decorate class member (%p)", classMember => {
527527
util.testFunction`
528528
let decoratorParameters: any;
529529
530-
const decorator = (target, key, index?) => {
530+
const decorator = (target: any, key: any, index?: any) => {
531531
const targetKind = target === Foo ? "Foo" : target === Foo.prototype ? "Foo.prototype" : "unknown";
532532
decoratorParameters = { targetKind, key, index: typeof index };
533533
};
@@ -548,10 +548,10 @@ describe("legacy experimentalDecorators", () => {
548548
["desc.writable = true", "return { configurable: true }"],
549549
])("Combine decorators (%p + %p)", (decorateA, decorateB) => {
550550
util.testFunction`
551-
const A = (target, key, desc): any => { ${decorateA} };
552-
const B = (target, key, desc): any => { ${decorateB} };
551+
const A = (target: any, key: any, desc: any): any => { ${decorateA} };
552+
const B = (target: any, key: any, desc: any): any => { ${decorateB} };
553553
class Foo { @A @B static method() {} }
554-
const { value, ...rest } = Object.getOwnPropertyDescriptor(Foo, "method");
554+
const { value, ...rest } = Object.getOwnPropertyDescriptor(Foo, "method")!;
555555
return rest;
556556
`
557557
.setOptions({ experimentalDecorators: true })
@@ -562,7 +562,7 @@ describe("legacy experimentalDecorators", () => {
562562
"Use decorator to override method value %s",
563563
overrideStatement => {
564564
util.testFunction`
565-
const decorator = (target, key, desc): any => { ${overrideStatement} };
565+
const decorator = (target: any, key: any, desc: any): any => { ${overrideStatement} };
566566
class Foo { @decorator static method() {} }
567567
return Foo.method;
568568
`

0 commit comments

Comments
 (0)