Skip to content

Commit 0b1e217

Browse files
author
Andy
authored
fourslash diagnostics tests: use objects instead of strings (#22193)
1 parent d15b098 commit 0b1e217

29 files changed

+226
-212
lines changed

src/harness/fourslash.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1236,20 +1236,18 @@ Actual: ${stringify(fullActual)}`);
12361236
return this.languageService.findReferences(this.activeFile.fileName, this.currentCaretPosition);
12371237
}
12381238

1239-
public getSyntacticDiagnostics(expected: string) {
1239+
public getSyntacticDiagnostics(expected: ReadonlyArray<ts.RealizedDiagnostic>) {
12401240
const diagnostics = this.languageService.getSyntacticDiagnostics(this.activeFile.fileName);
12411241
this.testDiagnostics(expected, diagnostics);
12421242
}
12431243

1244-
public getSemanticDiagnostics(expected: string) {
1244+
public getSemanticDiagnostics(expected: ReadonlyArray<ts.RealizedDiagnostic>) {
12451245
const diagnostics = this.languageService.getSemanticDiagnostics(this.activeFile.fileName);
12461246
this.testDiagnostics(expected, diagnostics);
12471247
}
12481248

1249-
private testDiagnostics(expected: string, diagnostics: ReadonlyArray<ts.Diagnostic>) {
1250-
const realized = ts.realizeDiagnostics(diagnostics, "\r\n");
1251-
const actual = stringify(realized);
1252-
assert.equal(actual, expected);
1249+
private testDiagnostics(expected: ReadonlyArray<ts.RealizedDiagnostic>, diagnostics: ReadonlyArray<ts.Diagnostic>) {
1250+
assert.deepEqual(ts.realizeDiagnostics(diagnostics, ts.newLineCharacter), expected);
12531251
}
12541252

12551253
public verifyQuickInfoAt(markerName: string, expectedText: string, expectedDocumentation?: string) {
@@ -4321,11 +4319,11 @@ namespace FourSlashInterface {
43214319
this.state.verifyQuickInfoDisplayParts(kind, kindModifiers, textSpan, displayParts, documentation, tags);
43224320
}
43234321

4324-
public getSyntacticDiagnostics(expected: string) {
4322+
public getSyntacticDiagnostics(expected: ReadonlyArray<ts.RealizedDiagnostic>) {
43254323
this.state.getSyntacticDiagnostics(expected);
43264324
}
43274325

4328-
public getSemanticDiagnostics(expected: string) {
4326+
public getSemanticDiagnostics(expected: ReadonlyArray<ts.RealizedDiagnostic>) {
43294327
this.state.getSemanticDiagnostics(expected);
43304328
}
43314329

src/services/shims.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ namespace ts {
581581
}
582582
}
583583

584-
interface RealizedDiagnostic {
584+
export interface RealizedDiagnostic {
585585
message: string;
586586
start: number;
587587
length: number;

tests/cases/fourslash/fourslash.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ declare namespace FourSlashInterface {
347347
start: number;
348348
length: number;
349349
}, displayParts: ts.SymbolDisplayPart[], documentation: ts.SymbolDisplayPart[], tags: ts.JSDocTagInfo[]): void;
350-
getSyntacticDiagnostics(expected: string): void;
351-
getSemanticDiagnostics(expected: string): void;
350+
getSyntacticDiagnostics(expected: ReadonlyArray<RealizedDiagnostic>): void;
351+
getSemanticDiagnostics(expected: ReadonlyArray<RealizedDiagnostic>): void;
352352
ProjectInfo(expected: string[]): void;
353353
allRangesAppearInImplementationList(markerName: string): void;
354354
}
@@ -520,6 +520,13 @@ declare namespace FourSlashInterface {
520520
text: string;
521521
range: Range;
522522
}
523+
interface RealizedDiagnostic {
524+
message: string;
525+
start: number;
526+
length: number;
527+
category: string;
528+
code: number;
529+
}
523530
}
524531
declare function verifyOperationIsCancelled(f: any): void;
525532
declare var test: FourSlashInterface.test_;

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics1.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// import a = b;
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'import ... =' can only be used in a .ts file.",
10-
"start": 0,
11-
"length": 13,
12-
"category": "error",
13-
"code": 8002
9+
message: "'import ... =' can only be used in a .ts file.",
10+
start: 0,
11+
length: 13,
12+
category: "error",
13+
code: 8002
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics10.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// function F<T>() { }
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'type parameter declarations' can only be used in a .ts file.",
10-
"start": 11,
11-
"length": 1,
12-
"category": "error",
13-
"code": 8004
9+
message: "'type parameter declarations' can only be used in a .ts file.",
10+
start: 11,
11+
length: 1,
12+
category: "error",
13+
code: 8004
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics11.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// function F(): number { }
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'types' can only be used in a .ts file.",
10-
"start": 14,
11-
"length": 6,
12-
"category": "error",
13-
"code": 8010
9+
message: "'types' can only be used in a .ts file.",
10+
start: 14,
11+
length: 6,
12+
category: "error",
13+
code: 8010
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics12.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// declare var v;
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'declare' can only be used in a .ts file.",
10-
"start": 0,
11-
"length": 7,
12-
"category": "error",
13-
"code": 8009
9+
message: "'declare' can only be used in a .ts file.",
10+
start: 0,
11+
length: 7,
12+
category: "error",
13+
code: 8009
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics13.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// var v: () => number;
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'types' can only be used in a .ts file.",
10-
"start": 7,
11-
"length": 12,
12-
"category": "error",
13-
"code": 8010
9+
message: "'types' can only be used in a .ts file.",
10+
start: 7,
11+
length: 12,
12+
category: "error",
13+
code: 8010
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics14.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// Foo<number>();
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'type arguments' can only be used in a .ts file.",
10-
"start": 4,
11-
"length": 6,
12-
"category": "error",
13-
"code": 8011
9+
message: "'type arguments' can only be used in a .ts file.",
10+
start: 4,
11+
length: 6,
12+
category: "error",
13+
code: 8011
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics15.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// function F(public p) { }
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'parameter modifiers' can only be used in a .ts file.",
10-
"start": 11,
11-
"length": 6,
12-
"category": "error",
13-
"code": 8012
9+
message: "'parameter modifiers' can only be used in a .ts file.",
10+
start: 11,
11+
length: 6,
12+
category: "error",
13+
code: 8012
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics16.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// function F(p?) { }
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'?' can only be used in a .ts file.",
10-
"start": 12,
11-
"length": 1,
12-
"category": "error",
13-
"code": 8009
9+
message: "'?' can only be used in a .ts file.",
10+
start: 12,
11+
length: 1,
12+
category: "error",
13+
code: 8009
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics17.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// function F(a: number) { }
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'types' can only be used in a .ts file.",
10-
"start": 14,
11-
"length": 6,
12-
"category": "error",
13-
"code": 8010
9+
message: "'types' can only be used in a .ts file.",
10+
start: 14,
11+
length: 6,
12+
category: "error",
13+
code: 8010
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics18.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@
99
////}
1010

1111
goTo.file("a.js");
12-
verify.getSyntacticDiagnostics(`[
12+
verify.getSyntacticDiagnostics([
1313
{
14-
"message": "\'public\' can only be used in a .ts file.",
15-
"start": 93,
16-
"length": 6,
17-
"category": "error",
18-
"code": 8009
14+
message: "\'public\' can only be used in a .ts file.",
15+
start: 93,
16+
length: 6,
17+
category: "error",
18+
code: 8009
1919
}
20-
]`);
20+
]);
2121

2222
// @Filename: b.js
2323
////class C {
2424
//// x: number; // Types not allowed
2525
////}
2626

2727
goTo.file("b.js");
28-
verify.getSyntacticDiagnostics(`[
28+
verify.getSyntacticDiagnostics([
2929
{
30-
"message": "'types' can only be used in a .ts file.",
31-
"start": 17,
32-
"length": 6,
33-
"category": "error",
34-
"code": 8010
30+
message: "'types' can only be used in a .ts file.",
31+
start: 17,
32+
length: 6,
33+
category: "error",
34+
code: 8010
3535
}
36-
]`);
36+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics19.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// enum E { }
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'enum declarations' can only be used in a .ts file.",
10-
"start": 5,
11-
"length": 1,
12-
"category": "error",
13-
"code": 8015
9+
message: "'enum declarations' can only be used in a .ts file.",
10+
start: 5,
11+
length: 1,
12+
category: "error",
13+
code: 8015
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics2.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// export = b;
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'export=' can only be used in a .ts file.",
10-
"start": 0,
11-
"length": 11,
12-
"category": "error",
13-
"code": 8003
9+
message: "'export=' can only be used in a .ts file.",
10+
start: 0,
11+
length: 11,
12+
category: "error",
13+
code: 8003
1414
}
15-
]`);
15+
]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics21.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
// @Filename: a.js
66
//// @internal class C {}
77

8-
verify.getSemanticDiagnostics(`[]`);
8+
verify.getSemanticDiagnostics([]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics22.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
// @Filename: a.js
55
//// function foo(...a) {}
66

7-
verify.getSemanticDiagnostics(`[]`);
7+
verify.getSemanticDiagnostics([]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics23.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
//// }
1111
//// }
1212

13-
verify.getSyntacticDiagnostics(`[]`);
14-
verify.getSemanticDiagnostics(`[]`);
13+
verify.getSyntacticDiagnostics([]);
14+
verify.getSemanticDiagnostics([]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics3.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// @Filename: a.js
55
//// class C<T> { }
66

7-
verify.getSyntacticDiagnostics(`[
7+
verify.getSyntacticDiagnostics([
88
{
9-
"message": "'type parameter declarations' can only be used in a .ts file.",
10-
"start": 8,
11-
"length": 1,
12-
"category": "error",
13-
"code": 8004
9+
message: "'type parameter declarations' can only be used in a .ts file.",
10+
start: 8,
11+
length: 1,
12+
category: "error",
13+
code: 8004
1414
}
15-
]`);
15+
]);

0 commit comments

Comments
 (0)