Skip to content

Commit fe970ab

Browse files
author
Andy Hanson
committed
Change tests to use the full JSON output of the navigation bar.
This reduces the confusion of verify.navigationBarCount() counting duplicate items.
1 parent fe77f54 commit fe970ab

36 files changed

+1660
-452
lines changed

src/harness/fourslash.ts

Lines changed: 38 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,70 +1959,42 @@ namespace FourSlash {
19591959
}
19601960
}
19611961

1962-
public verifyNavigationBarCount(expected: number) {
1963-
const items = this.languageService.getNavigationBarItems(this.activeFile.fileName);
1964-
const actual = this.getNavigationBarItemsCount(items[0]);
1965-
1966-
if (expected !== actual) {
1967-
this.raiseError(`verifyNavigationBarCount failed - found: ${actual} navigation items, expected: ${expected}.`);
1968-
}
1969-
}
1970-
1971-
private getNavigationBarItemsCount(root: ts.NavigationBarItem) {
1972-
ts.Debug.assert(root.kind === ts.ScriptElementKind.moduleElement);
1973-
function recur(item: ts.NavigationBarItem) {
1974-
let count = 1;
1975-
for (const child of item.childItems)
1976-
count += recur(child);
1977-
return count;
1978-
}
1979-
return recur(root);
1980-
}
1981-
1982-
public verifyNavigationBarContains(name: string, kind: string, fileName?: string, parentName?: string, isAdditionalSpan?: boolean, markerPosition?: number) {
1983-
fileName = fileName || this.activeFile.fileName;
1984-
const items = this.languageService.getNavigationBarItems(fileName);
1985-
1986-
if (!items || items.length === 0) {
1987-
this.raiseError("verifyNavigationBarContains failed - found 0 navigation items, expected at least one.");
1988-
}
1989-
1990-
if (this.navigationBarItemsContains(items, name, kind, parentName)) {
1991-
return;
1992-
}
1993-
1994-
const missingItem = { name, kind, parentName };
1995-
this.raiseError(`verifyNavigationBarContains failed - could not find the item: ${JSON.stringify(missingItem, undefined, 2)} in the returned list: (${JSON.stringify(items, undefined, 2)})`);
1996-
}
1997-
1998-
private navigationBarItemsContains(items: ts.NavigationBarItem[], name: string, kind: string, parentName?: string) {
1999-
function recur(items: ts.NavigationBarItem[], curParentName: string) {
2000-
for (let i = 0; i < items.length; i++) {
2001-
const item = items[i];
2002-
if (item && item.text === name && item.kind === kind && (!parentName || curParentName === parentName)) {
2003-
return true;
2004-
}
2005-
if (recur(item.childItems, item.text)) {
2006-
return true;
2007-
}
2008-
}
2009-
return false;
2010-
}
2011-
return recur(items, "");
2012-
}
2013-
2014-
public verifyNavigationBarChildItem(parent: string, name: string, kind: string) {
2015-
const items = this.languageService.getNavigationBarItems(this.activeFile.fileName);
2016-
2017-
for (let i = 0; i < items.length; i++) {
2018-
const item = items[i];
2019-
if (item.text === parent) {
2020-
if (this.navigationBarItemsContains(item.childItems, name, kind))
2021-
return;
2022-
const missingItem = { name, kind };
2023-
this.raiseError(`verifyNavigationBarChildItem failed - could not find the item: ${JSON.stringify(missingItem)} in the children list: (${JSON.stringify(item.childItems, undefined, 2)})`);
2024-
}
2025-
}
1962+
public verifyNavigationBar(json: any) {
1963+
let items = this.languageService.getNavigationBarItems(this.activeFile.fileName);
1964+
items = this.simplifyNavigationBar(items);
1965+
if (JSON.stringify(items) !== JSON.stringify(json)) {
1966+
this.raiseError(`verifyNavigationBar failed - expected: ${JSON.stringify(json, undefined, 2)}, got: ${JSON.stringify(items, undefined, 2)}`)
1967+
}
1968+
}
1969+
1970+
// Remove any properties that tend to all have the same value so that test data is easier to read.
1971+
private simplifyNavigationBar(items: ts.NavigationBarItem[]): any {
1972+
return items.map(item => {
1973+
item = ts.clone(item);
1974+
if (item.kindModifiers === "")
1975+
delete item.kindModifiers;
1976+
delete item.spans;
1977+
item.childItems = item.childItems.map(child => {
1978+
child = ts.clone(child);
1979+
ts.Debug.assert(child.childItems.length === 0);
1980+
ts.Debug.assert(child.indent === 0);
1981+
ts.Debug.assert(child.bolded === false);
1982+
ts.Debug.assert(child.grayed === false);
1983+
delete child.childItems;
1984+
delete child.indent;
1985+
delete child.bolded;
1986+
delete child.grayed;
1987+
delete child.spans;
1988+
if (child.kindModifiers === "")
1989+
delete child.kindModifiers;
1990+
return child;
1991+
});
1992+
if (item.bolded === false)
1993+
delete item.bolded;
1994+
if (item.grayed === false)
1995+
delete item.grayed;
1996+
return item;
1997+
})
20261998
}
20271999

20282000
public printNavigationItems(searchValue: string) {
@@ -3041,23 +3013,8 @@ namespace FourSlashInterface {
30413013
this.DocCommentTemplate(/*expectedText*/ undefined, /*expectedOffset*/ undefined, /*empty*/ true);
30423014
}
30433015

3044-
public navigationBarCount(count: number) {
3045-
this.state.verifyNavigationBarCount(count);
3046-
}
3047-
3048-
// TODO: figure out what to do with the unused arguments.
3049-
public navigationBarContains(
3050-
name: string,
3051-
kind: string,
3052-
fileName?: string,
3053-
parentName?: string,
3054-
isAdditionalSpan?: boolean,
3055-
markerPosition?: number) {
3056-
this.state.verifyNavigationBarContains(name, kind, fileName, parentName, isAdditionalSpan, markerPosition);
3057-
}
3058-
3059-
public navigationBarChildItem(parent: string, name: string, kind: string) {
3060-
this.state.verifyNavigationBarChildItem(parent, name, kind);
3016+
public navigationBar(json: any) {
3017+
this.state.verifyNavigationBar(json);
30613018
}
30623019

30633020
public navigationItemsListCount(count: number, searchValue: string, matchKind?: string) {

tests/cases/fourslash/deleteClassWithEnumPresent.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,35 @@
55

66
goTo.marker();
77
edit.deleteAtCaret('class Bar { }'.length);
8-
verify.navigationBarContains('Foo', 'enum', 'tests/cases/fourslash/deleteClassWithEnumPresent.ts', '');
8+
verify.navigationBar([
9+
{
10+
"text": "<global>",
11+
"kind": "module",
12+
"childItems": [
13+
{
14+
"text": "Foo",
15+
"kind": "enum"
16+
}
17+
],
18+
"indent": 0
19+
},
20+
{
21+
"text": "Foo",
22+
"kind": "enum",
23+
"childItems": [
24+
{
25+
"text": "a",
26+
"kind": "property"
27+
},
28+
{
29+
"text": "b",
30+
"kind": "property"
31+
},
32+
{
33+
"text": "c",
34+
"kind": "property"
35+
}
36+
],
37+
"indent": 1
38+
}
39+
]);

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ declare namespace FourSlashInterface {
175175
DocCommentTemplate(expectedText: string, expectedOffset: number, empty?: boolean): void;
176176
noDocCommentTemplate(): void;
177177

178-
navigationBarCount(count: number): void;
179-
navigationBarContains(name: string, kind: string, fileName?: string, parentName?: string, isAdditionalSpan?: boolean, markerPosition?: number): void;
180-
navigationBarChildItem(parent: string, text: string, kind: string): void;
178+
navigationBar(json: any): void;
181179
navigationItemsListCount(count: number, searchValue: string, matchKind?: string): void;
182180
navigationItemsListContains(name: string, kind: string, searchValue: string, matchKind: string, fileName?: string, parentName?: string): void;
183181
occurrencesAtPositionContains(range: Range, isWriteAccess?: boolean): void;

tests/cases/fourslash/getNavigationBarItems.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,31 @@
55
//// ["bar"]: string;
66
////}
77

8-
verify.navigationBarCount(5);
9-
verify.navigationBarContains("C", "class");
10-
verify.navigationBarChildItem("C", "[\"bar\"]", "property");
11-
verify.navigationBarChildItem("C", "foo", "property");
8+
verify.navigationBar([
9+
{
10+
"text": "<global>",
11+
"kind": "module",
12+
"childItems": [
13+
{
14+
"text": "C",
15+
"kind": "class"
16+
}
17+
],
18+
"indent": 0
19+
},
20+
{
21+
"text": "C",
22+
"kind": "class",
23+
"childItems": [
24+
{
25+
"text": "[\"bar\"]",
26+
"kind": "property"
27+
},
28+
{
29+
"text": "foo",
30+
"kind": "property"
31+
}
32+
],
33+
"indent": 1
34+
}
35+
])
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
/// <reference path="fourslash.ts" />
22

3-
//// {| "itemName": "c", "kind": "const", "parentName": "<global>" |}const c = 0;
3+
//// const c = 0;
44

5-
test.markers().forEach(marker => {
6-
verify.navigationBarContains(
7-
marker.data.itemName,
8-
marker.data.kind,
9-
marker.fileName,
10-
marker.data.parentName,
11-
marker.data.isAdditionalRange,
12-
marker.position);
13-
});
5+
verify.navigationBar([
6+
{
7+
"text": "<global>",
8+
"kind": "module",
9+
"childItems": [
10+
{
11+
"text": "c",
12+
"kind": "const"
13+
}
14+
],
15+
"indent": 0
16+
}
17+
]);

0 commit comments

Comments
 (0)