refactor(language-service): cleanup tests for Hover (#32378)

Move generic test methods to `MockTypescriptHost` so they could be
shared across all tests.

This is in preparation for adding more tests to Hover when new features
get added.

PR Close #32378
This commit is contained in:
Keen Yee Liau
2019-08-28 12:55:12 -07:00
committed by Miško Hevery
parent 852afb312a
commit 18ce58c2bc
3 changed files with 213 additions and 197 deletions

View File

@ -239,6 +239,78 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
}
return name;
}
/**
* Append a snippet of code to `app.component.ts` and return the file name.
* There must not be any name collision with existing code.
* @param code Snippet of code
*/
addCode(code: string) {
const fileName = '/app/app.component.ts';
const originalContent = this.getFileContent(fileName);
const newContent = originalContent + code;
this.override(fileName, newContent);
return fileName;
}
/**
* Returns the definition marker ᐱselectorᐱ for the specified 'selector'.
* Asserts that marker exists.
* @param fileName name of the file
* @param selector name of the marker
*/
getDefinitionMarkerFor(fileName: string, selector: string): ts.TextSpan {
const markers = this.getReferenceMarkers(fileName);
expect(markers).toBeDefined();
expect(Object.keys(markers !.definitions)).toContain(selector);
expect(markers !.definitions[selector].length).toBe(1);
const marker = markers !.definitions[selector][0];
expect(marker.start).toBeLessThanOrEqual(marker.end);
return {
start: marker.start,
length: marker.end - marker.start,
};
}
/**
* Returns the reference marker «selector» for the specified 'selector'.
* Asserts that marker exists.
* @param fileName name of the file
* @param selector name of the marker
*/
getReferenceMarkerFor(fileName: string, selector: string): ts.TextSpan {
const markers = this.getReferenceMarkers(fileName);
expect(markers).toBeDefined();
expect(Object.keys(markers !.references)).toContain(selector);
expect(markers !.references[selector].length).toBe(1);
const marker = markers !.references[selector][0];
expect(marker.start).toBeLessThanOrEqual(marker.end);
return {
start: marker.start,
length: marker.end - marker.start,
};
}
/**
* Returns the location marker ~{selector} for the specified 'selector'.
* Asserts that marker exists.
* @param fileName name of the file
* @param selector name of the marker
*/
getLocationMarkerFor(fileName: string, selector: string): ts.TextSpan {
const markers = this.getMarkerLocations(fileName);
expect(markers).toBeDefined();
const start = markers ![`start-${selector}`];
expect(start).toBeDefined();
const end = markers ![`end-${selector}`];
expect(end).toBeDefined();
expect(start).toBeLessThanOrEqual(end);
return {
start: start,
length: end - start,
};
}
}
function iterableToArray<T>(iterator: IterableIterator<T>) {