Skip to content

Commit bfef4a0

Browse files
committed
Add new tests for shims
1 parent c5006ca commit bfef4a0

28 files changed

Lines changed: 776 additions & 28 deletions

src/harness/fourslash.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,26 @@ module FourSlash {
275275
}
276276
}
277277

278-
constructor(public testData: FourSlashData) {
278+
private getLanguageServiceAdaptor(testType: FourSlashTestType):
279+
{ new (cancellationToken?: ts.CancellationToken, options?: ts.CompilerOptions): Harness.LanguageService.LanguageServiceAdaptor } {
280+
switch (testType) {
281+
case FourSlashTestType.Native:
282+
return Harness.LanguageService.NativeLanugageServiceAdaptor;
283+
break;
284+
case FourSlashTestType.Shims:
285+
return Harness.LanguageService.ShimLanugageServiceAdaptor;
286+
break;
287+
default:
288+
throw new Error("Unknown FourSlash test type: ");
289+
}
290+
}
291+
292+
constructor(private basePath: string, private testType: FourSlashTestType, public testData: FourSlashData) {
279293
// Create a new Services Adaptor
280294
this.cancellationToken = new TestCancellationToken();
281295
var compilationOptions = convertGlobalOptionsToCompilerOptions(this.testData.globalOptions);
282-
var languageServiceAdaptor = new Harness.LanguageService.NativeLanugageServiceAdaptor(this.cancellationToken, compilationOptions);
296+
var languageserviceAdaptorFactory = this.getLanguageServiceAdaptor(testType);
297+
var languageServiceAdaptor = new languageserviceAdaptorFactory(this.cancellationToken, compilationOptions);
283298
this.languageServiceAdaptorHost = languageServiceAdaptor.getHost();
284299
this.languageService = languageServiceAdaptor.getLanguageService();
285300

@@ -308,15 +323,15 @@ module FourSlash {
308323
// Add triple reference files into language-service host
309324
ts.forEach(referencedFiles, referenceFile => {
310325
// Fourslash insert tests/cases/fourslash into inputFile.unitName so we will properly append the same base directory to refFile path
311-
var referenceFilePath = "tests/cases/fourslash/" + referenceFile.filename;
326+
var referenceFilePath = this.basePath+ '/' + referenceFile.filename;
312327
this.addMatchedInputFile(referenceFilePath);
313328
});
314329

315330
// Add import files into language-service host
316331
ts.forEach(importedFiles, importedFile => {
317332
// Fourslash insert tests/cases/fourslash into inputFile.unitName and import statement doesn't require ".ts"
318333
// so convert them before making appropriate comparison
319-
var importedFilePath = "tests/cases/fourslash/" + importedFile.filename + ".ts";
334+
var importedFilePath = this.basePath + '/' + importedFile.filename + ".ts";
320335
this.addMatchedInputFile(importedFilePath);
321336
});
322337

@@ -1382,6 +1397,12 @@ module FourSlash {
13821397
}
13831398

13841399
private checkPostEditInvariants() {
1400+
if (this.testType !== FourSlashTestType.Native) {
1401+
// getSourcefile() results can not be serialized. Only perform these verifications
1402+
// if running against a native LS object.
1403+
return;
1404+
}
1405+
13851406
var incrementalSourceFile = this.languageService.getSourceFile(this.activeFile.fileName);
13861407
Utils.assertInvariants(incrementalSourceFile, /*parent:*/ undefined);
13871408

@@ -2052,7 +2073,7 @@ module FourSlash {
20522073
} else if (typeof indexOrName === 'string') {
20532074
var name = <string>indexOrName;
20542075
// names are stored in the compiler with this relative path, this allows people to use goTo.file on just the filename
2055-
name = name.indexOf('/') === -1 ? 'tests/cases/fourslash/' + name : name;
2076+
name = name.indexOf('/') === -1 ? (this.basePath + '/' + name) : name;
20562077
var availableNames: string[] = [];
20572078
var foundIt = false;
20582079
for (var i = 0; i < this.testData.files.length; i++) {
@@ -2118,17 +2139,17 @@ module FourSlash {
21182139
var fsOutput = new Harness.Compiler.WriterAggregator();
21192140
var fsErrors = new Harness.Compiler.WriterAggregator();
21202141
export var xmlData: TestXmlData[] = [];
2121-
export function runFourSlashTest(fileName: string) {
2142+
export function runFourSlashTest(basePath: string, testType: FourSlashTestType, fileName: string) {
21222143
var content = Harness.IO.readFile(fileName);
2123-
var xml = runFourSlashTestContent(content, fileName);
2144+
var xml = runFourSlashTestContent(basePath, testType, content, fileName);
21242145
xmlData.push(xml);
21252146
}
21262147

2127-
export function runFourSlashTestContent(content: string, fileName: string): TestXmlData {
2148+
export function runFourSlashTestContent(basePath: string, testType: FourSlashTestType, content: string, fileName: string): TestXmlData {
21282149
// Parse out the files and their metadata
2129-
var testData = parseTestData(content, fileName);
2150+
var testData = parseTestData(basePath, content, fileName);
21302151

2131-
currentTestState = new TestState(testData);
2152+
currentTestState = new TestState(basePath, testType, testData);
21322153

21332154
var result = '';
21342155
var host = Harness.Compiler.createCompilerHost([{ unitName: Harness.Compiler.fourslashFilename, content: undefined },
@@ -2171,7 +2192,7 @@ module FourSlash {
21712192
return lines.map(s => s.substr(1)).join('\n');
21722193
}
21732194

2174-
function parseTestData(contents: string, fileName: string): FourSlashData {
2195+
function parseTestData(basePath: string, contents: string, fileName: string): FourSlashData {
21752196
// Regex for parsing options in the format "@Alpha: Value of any sort"
21762197
var optionRegex = /^\s*@(\w+): (.*)\s*/;
21772198

@@ -2239,7 +2260,7 @@ module FourSlash {
22392260
currentFileName = fileName;
22402261
}
22412262

2242-
currentFileName = 'tests/cases/fourslash/' + match[2];
2263+
currentFileName = basePath + '/' + match[2];
22432264
currentFileOptions[match[1]] = match[2];
22442265
} else {
22452266
// Add other fileMetadata flag

src/harness/fourslashRunner.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@
22
///<reference path='harness.ts'/>
33
///<reference path='runnerbase.ts' />
44

5-
class FourslashRunner extends RunnerBase {
6-
public basePath = 'tests/cases/fourslash';
5+
const enum FourSlashTestType {
6+
Native,
7+
Shims
8+
}
9+
10+
class FourSlashRunner extends RunnerBase {
11+
protected basePath: string;
12+
protected testSuiteName: string;
713

8-
constructor() {
14+
constructor(private testType: FourSlashTestType) {
915
super();
16+
switch (testType) {
17+
case FourSlashTestType.Native:
18+
this.basePath = 'tests/cases/fourslash';
19+
this.testSuiteName = 'fourslash';
20+
break;
21+
case FourSlashTestType.Shims:
22+
this.basePath = 'tests/cases/fourslash/shims';
23+
this.testSuiteName = 'fourslash-shims';
24+
break;
25+
}
1026
}
1127

1228
public initializeTests() {
1329
if (this.tests.length === 0) {
14-
this.tests = this.enumerateFiles(this.basePath, /\.ts/i);
30+
this.tests = this.enumerateFiles(this.basePath, /\.ts/i, { recursive: false });
1531
}
1632

17-
describe("fourslash tests", () => {
33+
describe(this.testSuiteName, () => {
1834
this.tests.forEach((fn: string) => {
1935
fn = ts.normalizeSlashes(fn);
2036
var justName = fn.replace(/^.*[\\\/]/, '');
@@ -24,8 +40,8 @@ class FourslashRunner extends RunnerBase {
2440
if (testIndex >= 0) fn = fn.substr(testIndex);
2541

2642
if (justName && !justName.match(/fourslash\.ts$/i) && !justName.match(/\.d\.ts$/i)) {
27-
it('FourSlash test ' + justName + ' runs correctly', function () {
28-
FourSlash.runFourSlashTest(fn);
43+
it(this.testSuiteName + ' test ' + justName + ' runs correctly',() => {
44+
FourSlash.runFourSlashTest(this.basePath, this.testType, fn);
2945
});
3046
}
3147
});
@@ -82,9 +98,9 @@ class FourslashRunner extends RunnerBase {
8298
}
8399
}
84100

85-
class GeneratedFourslashRunner extends FourslashRunner {
86-
constructor() {
87-
super();
101+
class GeneratedFourslashRunner extends FourSlashRunner {
102+
constructor(testType: FourSlashTestType) {
103+
super(testType);
88104
this.basePath += '/generated/';
89105
}
90106
}

src/harness/harnessLanguageService.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ module Harness.LanguageService {
5656
}
5757

5858
class ScriptSnapshot implements ts.IScriptSnapshot {
59-
public textSnapshot: string; public version: number;
59+
public textSnapshot: string;
60+
public version: number;
61+
6062
constructor(public scriptInfo: ScriptInfo) {
61-
this.textSnapshot = scriptInfo.content; this.version = scriptInfo.version; }
63+
this.textSnapshot = scriptInfo.content;
64+
this.version = scriptInfo.version;
65+
}
6266

6367
public getText(start: number, end: number): string {
6468
return this.textSnapshot.substring(start, end);

src/harness/runner.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ if (testConfigFile !== '') {
6161
runners.push(new ProjectRunner());
6262
break;
6363
case 'fourslash':
64-
runners.push(new FourslashRunner());
64+
runners.push(new FourSlashRunner(FourSlashTestType.Native));
65+
break;
66+
case 'fourslash-shims':
67+
runners.push(new FourSlashRunner(FourSlashTestType.Shims));
6568
break;
6669
case 'fourslash-generated':
67-
runners.push(new GeneratedFourslashRunner());
70+
runners.push(new GeneratedFourslashRunner(FourSlashTestType.Native));
6871
break;
6972
case 'rwc':
7073
runners.push(new RWCRunner());
@@ -90,7 +93,8 @@ if (runners.length === 0) {
9093
}
9194

9295
// language services
93-
runners.push(new FourslashRunner());
96+
runners.push(new FourSlashRunner(FourSlashTestType.Native));
97+
runners.push(new FourSlashRunner(FourSlashTestType.Shims));
9498
//runners.push(new GeneratedFourslashRunner());
9599
}
96100

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
1 >while (true) {
3+
4+
~~~~~~~~~~~~~~~ => Pos: (0 to 14) SpanInfo: {"start":0,"length":12}
5+
>while (true)
6+
>:=> (line 1, col 0) to (line 1, col 12)
7+
--------------------------------
8+
2 > break;
9+
10+
~~~~~~~~~~~ => Pos: (15 to 25) SpanInfo: {"start":19,"length":5}
11+
>break
12+
>:=> (line 2, col 4) to (line 2, col 9)
13+
--------------------------------
14+
3 >}
15+
16+
~~ => Pos: (26 to 27) SpanInfo: {"start":19,"length":5}
17+
>break
18+
>:=> (line 2, col 4) to (line 2, col 9)
19+
--------------------------------
20+
4 >y: while (true) {
21+
22+
~~~~~~~~~~~~~~~~~~ => Pos: (28 to 45) SpanInfo: {"start":31,"length":12}
23+
>while (true)
24+
>:=> (line 4, col 3) to (line 4, col 15)
25+
--------------------------------
26+
5 > break y;
27+
28+
~~~~~~~~~~~~~ => Pos: (46 to 58) SpanInfo: {"start":50,"length":7}
29+
>break y
30+
>:=> (line 5, col 4) to (line 5, col 11)
31+
--------------------------------
32+
6 >}
33+
34+
~~ => Pos: (59 to 60) SpanInfo: {"start":50,"length":7}
35+
>break y
36+
>:=> (line 5, col 4) to (line 5, col 11)
37+
--------------------------------
38+
7 >while (true) {
39+
40+
~~~~~~~~~~~~~~~ => Pos: (61 to 75) SpanInfo: {"start":61,"length":12}
41+
>while (true)
42+
>:=> (line 7, col 0) to (line 7, col 12)
43+
--------------------------------
44+
8 > continue;
45+
46+
~~~~~~~~~~~~~~ => Pos: (76 to 89) SpanInfo: {"start":80,"length":8}
47+
>continue
48+
>:=> (line 8, col 4) to (line 8, col 12)
49+
--------------------------------
50+
9 >}
51+
52+
~~ => Pos: (90 to 91) SpanInfo: {"start":80,"length":8}
53+
>continue
54+
>:=> (line 8, col 4) to (line 8, col 12)
55+
--------------------------------
56+
10 >z: while (true) {
57+
58+
~~~~~~~~~~~~~~~~~~ => Pos: (92 to 109) SpanInfo: {"start":95,"length":12}
59+
>while (true)
60+
>:=> (line 10, col 3) to (line 10, col 15)
61+
--------------------------------
62+
11 > continue z;
63+
64+
~~~~~~~~~~~~~~~~ => Pos: (110 to 125) SpanInfo: {"start":114,"length":10}
65+
>continue z
66+
>:=> (line 11, col 4) to (line 11, col 14)
67+
--------------------------------
68+
12 >}
69+
~ => Pos: (126 to 126) SpanInfo: {"start":114,"length":10}
70+
>continue z
71+
>:=> (line 11, col 4) to (line 11, col 14)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
EmitOutputStatus : Succeeded
2+
Filename : tests/cases/fourslash/shims/inputFile1.js
3+
var x = 5;
4+
var Bar = (function () {
5+
function Bar() {
6+
}
7+
return Bar;
8+
})();
9+
Filename : tests/cases/fourslash/shims/inputFile1.d.ts
10+
declare var x: number;
11+
declare class Bar {
12+
x: string;
13+
y: number;
14+
}
15+
16+
EmitOutputStatus : Succeeded
17+
Filename : tests/cases/fourslash/shims/inputFile2.js
18+
var x1 = "hello world";
19+
var Foo = (function () {
20+
function Foo() {
21+
}
22+
return Foo;
23+
})();
24+
Filename : tests/cases/fourslash/shims/inputFile2.d.ts
25+
declare var x1: string;
26+
declare class Foo {
27+
x: string;
28+
y: number;
29+
}
30+

tests/cases/fourslash/indentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,5 @@
179179
////{| "indent": 0 |}
180180

181181
test.markers().forEach((marker) => {
182-
verify.indentationAtPositionIs('tests/cases/fourslash/indentation.ts', marker.position, marker.data.indent);
182+
verify.indentationAtPositionIs(marker.fileName, marker.position, marker.data.indent);
183183
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//@Filename: findAllRefsOnDefinition-import.ts
4+
////export class Test{
5+
////
6+
//// constructor(){
7+
////
8+
//// }
9+
////
10+
//// public /*1*/start(){
11+
//// return this;
12+
//// }
13+
////
14+
//// public stop(){
15+
//// return this;
16+
//// }
17+
////}
18+
19+
//@Filename: findAllRefsOnDefinition.ts
20+
////import Second = require("findAllRefsOnDefinition-import");
21+
////
22+
////var second = new Second.Test()
23+
////second.start();
24+
////second.stop();
25+
26+
goTo.file("findAllRefsOnDefinition-import.ts");
27+
goTo.marker("1");
28+
29+
verify.referencesCountIs(2);
30+
31+
cancellation.setCancelled();
32+
goTo.marker("1");
33+
verifyOperationIsCancelled(() => verify.referencesCountIs(0) );
34+
35+
// verify that internal state is still correct
36+
cancellation.resetCancelled();
37+
goTo.marker("1");
38+
verify.referencesCountIs(2);

0 commit comments

Comments
 (0)