Skip to content

Commit e11d018

Browse files
committed
Add expected diagnostics to function assignability cast tests
1 parent 5ef5ff2 commit e11d018

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

test/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ expect.extend({
3434
const message = this.isNot
3535
? diagnosticMessages
3636
: expected
37-
? `Expected:\n${expected.join("\n")}\nReceived:\n${diagnosticMessages}\n`
37+
? `Expected:\n${expected.join("\n")}\nReceived:\n${diagnostics.map(diag => diag.code).join("\n")}\n`
3838
: `Received: ${this.utils.printReceived([])}\n`;
3939

4040
return matcherHint + "\n\n" + message;

test/unit/functions/validation/__snapshots__/invalidFunctionAssignments.spec.ts.snap

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,43 +1353,43 @@ exports[`Invalid function return ({"value": "function(this: void, s) { return s;
13531353
exports[`Invalid function return ({"value": "function(this: void, s) { return s; }"}): diagnostics 2`] = `"main.ts(4,17): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."`;
13541354

13551355
exports[`Invalid function return with cast ({"definition": "/** @noSelfInFile */ let noSelfInFileFunc: {(s: string): string} = function(s) { return s; };", "value": "noSelfInFileFunc"}): diagnostics 1`] = `
1356-
"main.ts(4,13): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1357-
main.ts(4,20): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
1356+
"main.ts(4,17): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1357+
main.ts(4,24): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
13581358
`;
13591359

13601360
exports[`Invalid function return with cast ({"definition": "/** @noSelfInFile */ let noSelfInFileFunc: {(s: string): string} = function(s) { return s; };", "value": "noSelfInFileFunc"}): diagnostics 2`] = `
1361-
"main.ts(4,13): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1362-
main.ts(4,20): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
1361+
"main.ts(4,17): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1362+
main.ts(4,24): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
13631363
`;
13641364

13651365
exports[`Invalid function return with cast ({"definition": "let selfFunc: {(this: any, s: string): string} = function(s) { return s; };", "value": "selfFunc"}): diagnostics 1`] = `
1366-
"main.ts(4,13): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'.
1367-
main.ts(4,20): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'."
1366+
"main.ts(4,17): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'.
1367+
main.ts(4,24): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'."
13681368
`;
13691369

13701370
exports[`Invalid function return with cast ({"definition": "let selfFunc: {(this: any, s: string): string} = function(s) { return s; };", "value": "selfFunc"}): diagnostics 2`] = `
1371-
"main.ts(4,13): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'.
1372-
main.ts(4,20): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'."
1371+
"main.ts(4,17): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'.
1372+
main.ts(4,24): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'."
13731373
`;
13741374

13751375
exports[`Invalid function return with cast ({"definition": "let voidFunc: {(this: void, s: string): string} = function(s) { return s; };", "value": "voidFunc"}): diagnostics 1`] = `
1376-
"main.ts(4,13): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1377-
main.ts(4,20): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
1376+
"main.ts(4,17): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1377+
main.ts(4,24): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
13781378
`;
13791379

13801380
exports[`Invalid function return with cast ({"definition": "let voidFunc: {(this: void, s: string): string} = function(s) { return s; };", "value": "voidFunc"}): diagnostics 2`] = `
1381-
"main.ts(4,13): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1382-
main.ts(4,20): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
1381+
"main.ts(4,17): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1382+
main.ts(4,24): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
13831383
`;
13841384

13851385
exports[`Invalid function return with cast ({"definition": "let voidFunc: {(this: void, s: string): string} = function(s) { return s; };", "value": "voidFunc"}): diagnostics 3`] = `
1386-
"main.ts(4,13): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1387-
main.ts(4,20): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
1386+
"main.ts(4,17): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1387+
main.ts(4,24): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
13881388
`;
13891389

13901390
exports[`Invalid function return with cast ({"definition": "let voidFunc: {(this: void, s: string): string} = function(s) { return s; };", "value": "voidFunc"}): diagnostics 4`] = `
1391-
"main.ts(4,13): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1392-
main.ts(4,20): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
1391+
"main.ts(4,17): error TSTL: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
1392+
main.ts(4,24): error TSTL: Unable to convert function with no 'this' parameter to function with 'this'. To fix, wrap in an arrow function, or declare with 'this: any'."
13931393
`;
13941394

13951395
exports[`Invalid function tuple assignment: diagnostics 1`] = `

test/unit/functions/validation/invalidFunctionAssignments.spec.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ test.each(invalidTestFunctionCasts)("Invalid function assignment with cast (%p)"
3838
${testFunction.definition || ""}
3939
let fn: typeof ${testFunction.value};
4040
fn = ${castedFunction};
41-
`.expectDiagnosticsToMatchSnapshot(undefined, true);
41+
`.expectDiagnosticsToMatchSnapshot(
42+
[unsupportedNoSelfFunctionConversion.code, unsupportedSelfFunctionConversion.code],
43+
true
44+
);
4245
});
4346

4447
test.each(invalidTestFunctionAssignments)(
@@ -68,7 +71,10 @@ test.each(invalidTestFunctionCasts)("Invalid function argument with cast (%p)",
6871
${testFunction.definition || ""}
6972
declare function takesFunction(fn: typeof ${testFunction.value});
7073
takesFunction(${castedFunction});
71-
`.expectDiagnosticsToMatchSnapshot(undefined, true);
74+
`.expectDiagnosticsToMatchSnapshot(
75+
[unsupportedNoSelfFunctionConversion.code, unsupportedSelfFunctionConversion.code],
76+
true
77+
);
7278
});
7379

7480
test.each(invalidTestFunctionAssignments)(
@@ -100,14 +106,22 @@ test.each(invalidTestFunctionAssignments)(
100106
}
101107
);
102108

103-
test.each(invalidTestFunctionCasts)("Invalid function return with cast (%p)", (testFunction, castedFunction) => {
104-
util.testModule`
105-
${testFunction.definition || ""}
106-
function returnsFunction(): typeof ${testFunction.value} {
107-
return ${castedFunction};
108-
}
109-
`.expectDiagnosticsToMatchSnapshot(undefined, true);
110-
});
109+
test.each(invalidTestFunctionCasts)(
110+
"Invalid function return with cast (%p)",
111+
(testFunction, castedFunction, isSelfConversion) => {
112+
util.testModule`
113+
${testFunction.definition || ""}
114+
function returnsFunction(): typeof ${testFunction.value} {
115+
return ${castedFunction};
116+
}
117+
`.expectDiagnosticsToMatchSnapshot(
118+
isSelfConversion
119+
? [unsupportedSelfFunctionConversion.code, unsupportedNoSelfFunctionConversion.code]
120+
: [unsupportedNoSelfFunctionConversion.code, unsupportedSelfFunctionConversion.code],
121+
true
122+
);
123+
}
124+
);
111125

112126
test("Invalid function tuple assignment", () => {
113127
util.testModule`

0 commit comments

Comments
 (0)