Skip to content

Commit

Permalink
Add test suits and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingwl committed Dec 27, 2020
1 parent 446bee4 commit d2fbd1e
Show file tree
Hide file tree
Showing 35 changed files with 608 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,18 @@ namespace FourSlash {
});
}

public verifyInlineHints(expected: readonly FourSlashInterface.VerifyInlineHintsOptions[], span: ts.TextSpan = { start: 0, length: this.activeFile.content.length }, preference?: ts.InlineHintsOptions) {
const hints = this.languageService.provideInlineHints(this.activeFile.fileName, span, preference);
assert.equal(hints.length, expected.length, "Number of hints");
const sortHints = (a: ts.InlineHint, b: ts.InlineHint) => a.position - b.position;
ts.zipWith(hints.sort(sortHints), [...expected].sort(sortHints), (actual, expected) => {
assert.equal(actual.text, expected.text, "Text");
assert.equal(actual.position, expected.position, "Position");
assert.equal(actual.whitespaceBefore, expected.whitespaceBefore, "whitespaceBefore");
assert.equal(actual.whitespaceAfter, expected.whitespaceAfter, "whitespaceAfter");
});
}

public verifyCompletions(options: FourSlashInterface.VerifyCompletionsOptions) {
if (options.marker === undefined) {
this.verifyCompletionsWorker(options);
Expand Down
11 changes: 11 additions & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ namespace FourSlashInterface {
}
}

public getInlineHints(expected: readonly VerifyInlineHintsOptions[], span: ts.TextSpan, preference?: ts.InlineHintsOptions) {
this.state.verifyInlineHints(expected, span, preference);
}

public quickInfoIs(expectedText: string, expectedDocumentation?: string) {
this.state.verifyQuickInfoString(expectedText, expectedDocumentation);
}
Expand Down Expand Up @@ -1645,6 +1649,13 @@ namespace FourSlashInterface {
readonly containerKind?: ts.ScriptElementKind;
}

export interface VerifyInlineHintsOptions {
text: string;
position: number
whitespaceBefore?: boolean;
whitespaceAfter?: boolean;
}

export type ArrayOrSingle<T> = T | readonly T[];

export interface VerifyCompletionListContainsOptions extends ts.UserPreferences {
Expand Down
2 changes: 1 addition & 1 deletion src/services/inlineHints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ts.InlineHints {
whitespaceAfter?: boolean;
}

const maxHintsLength = 20;
const maxHintsLength = 30;

export function provideInlineHints(context: InlineHintsContext): HintInfo[] {
const { file, program, span, cancellationToken, preferences } = context;
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ declare namespace FourSlashInterface {
start: number;
length: number;
}, displayParts: ts.SymbolDisplayPart[], documentation: ts.SymbolDisplayPart[], tags: { name: string, text?: string }[] | undefined): void;
getInlineHints(expected: readonly VerifyInlineHintsOptions[], span?: TextSpan, preference?: InlineHintsOptions);
getSyntacticDiagnostics(expected: ReadonlyArray<Diagnostic>): void;
getSemanticDiagnostics(expected: ReadonlyArray<Diagnostic>): void;
getSuggestionDiagnostics(expected: ReadonlyArray<Diagnostic>): void;
Expand Down Expand Up @@ -614,6 +615,11 @@ declare namespace FourSlashInterface {
readonly importModuleSpecifierPreference?: "shortest" | "project-relative" | "relative" | "non-relative";
readonly importModuleSpecifierEnding?: "minimal" | "index" | "js";
}
interface InlineHintsOptions extends UserPreferences {
readonly includeInlineParameterName?: boolean;
readonly includeInlineFunctionParameterType?: boolean;
readonly includeInlineVariableType?: boolean;
}
interface CompletionsOptions {
readonly marker?: ArrayOrSingle<string | Marker>;
readonly isNewIdentifierLocation?: boolean;
Expand Down Expand Up @@ -711,6 +717,14 @@ declare namespace FourSlashInterface {
readonly commands?: ReadonlyArray<{}>;
}

export interface VerifyInlineHintsOptions {
text: string;
position: number
whitespaceBefore?: boolean;
whitespaceAfter?: boolean;
}


interface VerifyNavigateToOptions {
readonly pattern: string;
readonly fileName?: string;
Expand Down
20 changes: 20 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path="fourslash.ts" />

//// function foo (a: number, b: number) {}
//// foo(/*a*/1, /*b*/2);

const markers = test.markers();
verify.getInlineHints([
{
text: 'a:',
position: markers[0].position,
whitespaceAfter: true
},
{
text: 'b:',
position: markers[1].position,
whitespaceAfter: true
}
], undefined, {
includeInlineParameterName: true
});
9 changes: 9 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path="fourslash.ts" />

//// declare const unknownCall: any;
//// unknownCall();

const markers = test.markers();
verify.getInlineHints([], undefined, {
includeInlineParameterName: true
});
24 changes: 24 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference path="fourslash.ts" />

//// function foo(a: number) {
//// return (b: number) => {
//// return a + b
//// }
//// }
//// foo(/*a*/1)(/*b*/2);

const markers = test.markers();
verify.getInlineHints([
{
text: 'a:',
position: markers[0].position,
whitespaceAfter: true
},
{
text: 'b:',
position: markers[1].position,
whitespaceAfter: true
},
], undefined, {
includeInlineParameterName: true
});
23 changes: 23 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork12.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path="fourslash.ts" />

//// function foo(a: (b: number) => number) {
//// return a(/*a*/1) + 2
//// }

//// foo(/*b*/(c: number) => c + 1);

const markers = test.markers();
verify.getInlineHints([
{
text: 'b:',
position: markers[0].position,
whitespaceAfter: true
},
{
text: 'a:',
position: markers[1].position,
whitespaceAfter: true
},
], undefined, {
includeInlineParameterName: true
});
16 changes: 16 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path="fourslash.ts" />

//// function foo (a: number, b: number) {}
//// declare const a: 1;
//// foo(a, /*b*/2);

const markers = test.markers();
verify.getInlineHints([
{
text: 'b:',
position: markers[0].position,
whitespaceAfter: true
},
], undefined, {
includeInlineParameterName: true
});
8 changes: 8 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork14.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />

//// function foo (a: number, b: number) {}
//// foo(1, 2);

verify.getInlineHints([], undefined, {
includeInlineParameterName: false
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork15.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

//// const a/*a*/ = 123;

const markers = test.markers();
verify.getInlineHints([
{
text: ':123',
position: markers[0].position,
whitespaceBefore: true
},
], undefined, {
includeInlineVariableType: true
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork16.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

//// const a/*a*/ = 123;

const markers = test.markers();
verify.getInlineHints([
{
text: ':123',
position: markers[0].position,
whitespaceBefore: true
},
], undefined, {
includeInlineVariableType: true
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork17.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

//// const a/*a*/ = { a: 123 };

const markers = test.markers();
verify.getInlineHints([
{
text: ':{ a: number; }',
position: markers[0].position,
whitespaceBefore: true
},
], undefined, {
includeInlineVariableType: true
});
15 changes: 15 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork18.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

//// class Class {}
//// const a/*a*/ = new Class();

const markers = test.markers();
verify.getInlineHints([
{
text: ':Class',
position: markers[0].position,
whitespaceBefore: true
},
], undefined, {
includeInlineVariableType: true
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork19.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

//// const a/*a*/ = () => 123;

const markers = test.markers();
verify.getInlineHints([
{
text: ':() => number',
position: markers[0].position,
whitespaceBefore: true
},
], undefined, {
includeInlineVariableType: true
});
15 changes: 15 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

//// function foo (a: number, { c }: any) {}
//// foo(/*a*/1, { c: 1});

const markers = test.markers();
verify.getInlineHints([
{
text: 'a:',
position: markers[0].position,
whitespaceAfter: true
}
], undefined, {
includeInlineParameterName: true
});
7 changes: 7 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

//// const a = 123;

verify.getInlineHints([], undefined, {
includeInlineVariableType: false
});
7 changes: 7 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork21.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

//// const a;

verify.getInlineHints([], undefined, {
includeInlineVariableType: true
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork22.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

//// const a/*a*/ = "I'm very very very very very very very very very long";

const markers = test.markers();
verify.getInlineHints([
{
text: `:"I'm very very very very ve...`,
position: markers[0].position,
whitespaceBefore: true
},
], undefined, {
includeInlineVariableType: true
});
15 changes: 15 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork23.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

//// function foo (Im_very_very_very_very_very_very_very_long: number) {}
//// foo(/*a*/1);

const markers = test.markers();
verify.getInlineHints([
{
text: 'Im_very_very_very_very_very...:',
position: markers[0].position,
whitespaceAfter: true
}
], undefined, {
includeInlineParameterName: true
});
20 changes: 20 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork24.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path="fourslash.ts" />

//// type F = (a: string, b: number) => void
//// const f: F = (a/*a*/, b/*b*/) => { }

const markers = test.markers();
verify.getInlineHints([
{
text: ':string',
position: markers[0].position,
whitespaceBefore: true
},
{
text: ':number',
position: markers[1].position,
whitespaceBefore: true
}
], undefined, {
includeInlineFunctionParameterType: true
});
15 changes: 15 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork25.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

//// function foo (cb: (a: string) => void) {}
//// foo((a/*a*/) => { })

const markers = test.markers();
verify.getInlineHints([
{
text: ':string',
position: markers[0].position,
whitespaceBefore: true
}
], undefined, {
includeInlineFunctionParameterType: true
});
15 changes: 15 additions & 0 deletions tests/cases/fourslash/inlineHintsShouldWork26.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />

//// function foo (cb: (a: Exclude<1 | 2 | 3, 1>) => void) {}
//// foo((a/*a*/) => { })

const markers = test.markers();
verify.getInlineHints([
{
text: ':Exclude<1 | 2 | 3, 1>',
position: markers[0].position,
whitespaceBefore: true
}
], undefined, {
includeInlineFunctionParameterType: true
});
Loading

0 comments on commit d2fbd1e

Please sign in to comment.