Skip to content

Commit

Permalink
refactor(language-service): cleanup tests for Hover (angular#32378)
Browse files Browse the repository at this point in the history
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 angular#32378
  • Loading branch information
Keen Yee Liau authored and mhevery committed Aug 29, 2019
1 parent 852afb3 commit 18ce58c
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 198 deletions.
149 changes: 28 additions & 121 deletions packages/language-service/test/definitions_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ describe('definitions', () => {
});

it('should be able to find field in an interpolation', () => {
const fileName = addCode(`
const fileName = mockHost.addCode(`
@Component({
template: '{{«name»}}'
})
export class MyComponent {
«ᐱnameᐱ: string;»
}`);

const marker = getReferenceMarkerFor(fileName, 'name');
const marker = mockHost.getReferenceMarkerFor(fileName, 'name');
const result = ngService.getDefinitionAt(fileName, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;
Expand All @@ -51,19 +51,19 @@ describe('definitions', () => {
expect(def.fileName).toBe(fileName);
expect(def.name).toBe('name');
expect(def.kind).toBe('property');
expect(def.textSpan).toEqual(getDefinitionMarkerFor(fileName, 'name'));
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(fileName, 'name'));
});

it('should be able to find a field in a attribute reference', () => {
const fileName = addCode(`
const fileName = mockHost.addCode(`
@Component({
template: '<input [(ngModel)]="«name»">'
})
export class MyComponent {
«ᐱnameᐱ: string;»
}`);

const marker = getReferenceMarkerFor(fileName, 'name');
const marker = mockHost.getReferenceMarkerFor(fileName, 'name');
const result = ngService.getDefinitionAt(fileName, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;
Expand All @@ -76,44 +76,44 @@ describe('definitions', () => {
expect(def.fileName).toBe(fileName);
expect(def.name).toBe('name');
expect(def.kind).toBe('property');
expect(def.textSpan).toEqual(getDefinitionMarkerFor(fileName, 'name'));
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(fileName, 'name'));
});

it('should be able to find a method from a call', () => {
const fileName = addCode(`
const fileName = mockHost.addCode(`
@Component({
template: '<div (click)="~{start-my}«myClick»()~{end-my};"></div>'
})
export class MyComponent {
«ᐱmyClickᐱ() { }»
}`);

const marker = getReferenceMarkerFor(fileName, 'myClick');
const marker = mockHost.getReferenceMarkerFor(fileName, 'myClick');
const result = ngService.getDefinitionAt(fileName, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;

expect(textSpan).toEqual(getLocationMarkerFor(fileName, 'my'));
expect(textSpan).toEqual(mockHost.getLocationMarkerFor(fileName, 'my'));
expect(definitions).toBeDefined();
expect(definitions !.length).toBe(1);
const def = definitions ![0];

expect(def.fileName).toBe(fileName);
expect(def.name).toBe('myClick');
expect(def.kind).toBe('method');
expect(def.textSpan).toEqual(getDefinitionMarkerFor(fileName, 'myClick'));
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(fileName, 'myClick'));
});

it('should be able to find a field reference in an *ngIf', () => {
const fileName = addCode(`
const fileName = mockHost.addCode(`
@Component({
template: '<div *ngIf="«include»"></div>'
})
export class MyComponent {
«ᐱincludeᐱ = true;»
}`);

const marker = getReferenceMarkerFor(fileName, 'include');
const marker = mockHost.getReferenceMarkerFor(fileName, 'include');
const result = ngService.getDefinitionAt(fileName, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;
Expand All @@ -126,25 +126,25 @@ describe('definitions', () => {
expect(def.fileName).toBe(fileName);
expect(def.name).toBe('include');
expect(def.kind).toBe('property');
expect(def.textSpan).toEqual(getDefinitionMarkerFor(fileName, 'include'));
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(fileName, 'include'));
});

it('should be able to find a reference to a component', () => {
const fileName = addCode(`
const fileName = mockHost.addCode(`
@Component({
template: '~{start-my}<«test-comp»></test-comp>~{end-my}'
})
export class MyComponent { }`);

// Get the marker for «test-comp» in the code added above.
const marker = getReferenceMarkerFor(fileName, 'test-comp');
const marker = mockHost.getReferenceMarkerFor(fileName, 'test-comp');

const result = ngService.getDefinitionAt(fileName, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;

// Get the marker for bounded text in the code added above.
const boundedText = getLocationMarkerFor(fileName, 'my');
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
expect(textSpan).toEqual(boundedText);

// There should be exactly 1 definition
Expand All @@ -156,25 +156,25 @@ describe('definitions', () => {
expect(def.fileName).toBe(refFileName);
expect(def.name).toBe('TestComponent');
expect(def.kind).toBe('component');
expect(def.textSpan).toEqual(getLocationMarkerFor(refFileName, 'test-comp'));
expect(def.textSpan).toEqual(mockHost.getLocationMarkerFor(refFileName, 'test-comp'));
});

it('should be able to find an event provider', () => {
const fileName = addCode(`
const fileName = mockHost.addCode(`
@Component({
template: '<test-comp ~{start-my}(«test»)="myHandler()"~{end-my}></div>'
})
export class MyComponent { myHandler() {} }`);

// Get the marker for «test» in the code added above.
const marker = getReferenceMarkerFor(fileName, 'test');
const marker = mockHost.getReferenceMarkerFor(fileName, 'test');

const result = ngService.getDefinitionAt(fileName, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;

// Get the marker for bounded text in the code added above
const boundedText = getLocationMarkerFor(fileName, 'my');
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
expect(textSpan).toEqual(boundedText);

// There should be exactly 1 definition
Expand All @@ -186,12 +186,12 @@ describe('definitions', () => {
expect(def.fileName).toBe(refFileName);
expect(def.name).toBe('testEvent');
expect(def.kind).toBe('event');
expect(def.textSpan).toEqual(getDefinitionMarkerFor(refFileName, 'test'));
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(refFileName, 'test'));
});

it('should be able to find an input provider', () => {
// '/app/parsing-cases.ts', 'tcName',
const fileName = addCode(`
const fileName = mockHost.addCode(`
@Component({
template: '<test-comp ~{start-my}[«tcName»]="name"~{end-my}></div>'
})
Expand All @@ -200,14 +200,14 @@ describe('definitions', () => {
}`);

// Get the marker for «test» in the code added above.
const marker = getReferenceMarkerFor(fileName, 'tcName');
const marker = mockHost.getReferenceMarkerFor(fileName, 'tcName');

const result = ngService.getDefinitionAt(fileName, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;

// Get the marker for bounded text in the code added above
const boundedText = getLocationMarkerFor(fileName, 'my');
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
expect(textSpan).toEqual(boundedText);

// There should be exactly 1 definition
Expand All @@ -219,11 +219,11 @@ describe('definitions', () => {
expect(def.fileName).toBe(refFileName);
expect(def.name).toBe('name');
expect(def.kind).toBe('property');
expect(def.textSpan).toEqual(getDefinitionMarkerFor(refFileName, 'tcName'));
expect(def.textSpan).toEqual(mockHost.getDefinitionMarkerFor(refFileName, 'tcName'));
});

it('should be able to find a pipe', () => {
const fileName = addCode(`
const fileName = mockHost.addCode(`
@Component({
template: '<div *ngIf="~{start-my}input | «async»~{end-my}"></div>'
})
Expand All @@ -232,14 +232,14 @@ describe('definitions', () => {
}`);

// Get the marker for «test» in the code added above.
const marker = getReferenceMarkerFor(fileName, 'async');
const marker = mockHost.getReferenceMarkerFor(fileName, 'async');

const result = ngService.getDefinitionAt(fileName, marker.start);
expect(result).toBeDefined();
const {textSpan, definitions} = result !;

// Get the marker for bounded text in the code added above
const boundedText = getLocationMarkerFor(fileName, 'my');
const boundedText = mockHost.getLocationMarkerFor(fileName, 'my');
expect(textSpan).toEqual(boundedText);

expect(definitions).toBeDefined();
Expand All @@ -253,97 +253,4 @@ describe('definitions', () => {
// Not asserting the textSpan of definition because it's external file
}
});

it('should be able to find a template from a url', () => {
const fileName = addCode(`
@Component({
templateUrl: './«test».ng',
})
export class MyComponent {}`);

const marker = getReferenceMarkerFor(fileName, 'test');
const result = ngService.getDefinitionAt(fileName, marker.start);

expect(result).toBeDefined();
const {textSpan, definitions} = result !;

expect(textSpan).toEqual({start: marker.start - 2, length: 9});

expect(definitions).toBeDefined();
expect(definitions !.length).toBe(1);
const [def] = definitions !;
expect(def.fileName).toBe('/app/test.ng');
expect(def.textSpan).toEqual({start: 0, length: 0});
});

/**
* 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
*/
function addCode(code: string) {
const fileName = '/app/app.component.ts';
const originalContent = mockHost.getFileContent(fileName);
const newContent = originalContent + code;
mockHost.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
*/
function getDefinitionMarkerFor(fileName: string, selector: string): ts.TextSpan {
const markers = mockHost.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
*/
function getReferenceMarkerFor(fileName: string, selector: string): ts.TextSpan {
const markers = mockHost.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
*/
function getLocationMarkerFor(fileName: string, selector: string): ts.TextSpan {
const markers = mockHost.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,
};
}
});
Loading

0 comments on commit 18ce58c

Please sign in to comment.