Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(language-service): remove MockData from MockTypescriptHost #32752

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 32 additions & 35 deletions packages/language-service/test/completions_spec.ts
Expand Up @@ -11,15 +11,16 @@ import * as ts from 'typescript';
import {createLanguageService} from '../src/language_service';
import {TypeScriptServiceHost} from '../src/typescript_host';

import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';

describe('completions', () => {
let mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh);
let mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
let service = ts.createLanguageService(mockHost);
let ngHost = new TypeScriptServiceHost(mockHost, service);
let ngService = createLanguageService(ngHost);

beforeEach(() => { mockHost.reset(); });

it('should be able to get entity completions',
() => { expectContains('/app/test.ng', 'entity-amp', '&', '>', '<', 'ι'); });

Expand All @@ -44,7 +45,6 @@ describe('completions', () => {
const fileName = '/app/test.ng';
mockHost.override(fileName, ' > {{tle<\n {{retl ><bel/beled}}di>\n la</b </d &a ');
expect(() => ngService.getCompletionsAt(fileName, 31)).not.toThrow();
mockHost.override(fileName, undefined !);
});

it('should be able to infer the type of a ngForOf', () => {
Expand All @@ -68,7 +68,7 @@ describe('completions', () => {
street: string
}

@Component({template: '<div *ngFor="let person of people | async">{{person.~{name}name}}</div'})
@Component({template: '<div *ngFor="let person of people | async">{{person.~{name}name}}</div>'})
export class MyComponent {
people: Promise<Person[]>;
}`);
Expand All @@ -92,41 +92,38 @@ describe('completions', () => {
throw e;
}
}
try {
const originalContent = mockHost.getFileContent(fileName) !;

// For each character in the file, add it to the file and request a completion after it.
for (let index = 0, len = originalContent.length; index < len; index++) {
const content = originalContent.substr(0, index);
mockHost.override(fileName, content);
tryCompletionsAt(index);
}

// For the complete file, try to get a completion at every character.
mockHost.override(fileName, originalContent);
for (let index = 0, len = originalContent.length; index < len; index++) {
tryCompletionsAt(index);
}
const originalContent = mockHost.getFileContent(fileName) !;
Copy link
Contributor Author

@kyliau kyliau Sep 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub seems to do a poor job at displaying the diffs - here the try-finally block is removed. Code change is purely due to indentation.


// Delete random characters in the file until we get an empty file.
let content = originalContent;
while (content.length > 0) {
const deleteIndex = Math.floor(Math.random() * content.length);
content = content.slice(0, deleteIndex - 1) + content.slice(deleteIndex + 1);
mockHost.override(fileName, content);
// For each character in the file, add it to the file and request a completion after it.
for (let index = 0, len = originalContent.length; index < len; index++) {
const content = originalContent.substr(0, index);
mockHost.override(fileName, content);
tryCompletionsAt(index);
}

const requestIndex = Math.floor(Math.random() * content.length);
tryCompletionsAt(requestIndex);
}
// For the complete file, try to get a completion at every character.
mockHost.override(fileName, originalContent);
for (let index = 0, len = originalContent.length; index < len; index++) {
tryCompletionsAt(index);
}

// Delete random characters in the file until we get an empty file.
let content = originalContent;
while (content.length > 0) {
const deleteIndex = Math.floor(Math.random() * content.length);
content = content.slice(0, deleteIndex - 1) + content.slice(deleteIndex + 1);
mockHost.override(fileName, content);

// Build up the string from zero asking for a completion after every char
buildUp(originalContent, (text, position) => {
mockHost.override(fileName, text);
tryCompletionsAt(position);
});
} finally {
mockHost.override(fileName, undefined !);
const requestIndex = Math.floor(Math.random() * content.length);
tryCompletionsAt(requestIndex);
}

// Build up the string from zero asking for a completion after every char
buildUp(originalContent, (text, position) => {
mockHost.override(fileName, text);
tryCompletionsAt(position);
});
}).not.toThrow();
});

Expand All @@ -138,7 +135,7 @@ describe('completions', () => {
template: '~{inside-template}'
})
export class MyComponent {

}`);

expectContains(fileName, 'inside-template', 'h1');
Expand Down
3 changes: 1 addition & 2 deletions packages/language-service/test/definitions_spec.ts
Expand Up @@ -12,7 +12,6 @@ import {createLanguageService} from '../src/language_service';
import {LanguageService} from '../src/types';
import {TypeScriptServiceHost} from '../src/typescript_host';

import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';

describe('definitions', () => {
Expand All @@ -23,7 +22,7 @@ describe('definitions', () => {

beforeEach(() => {
// Create a new mockHost every time to reset any files that are overridden.
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh);
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
service = ts.createLanguageService(mockHost);
ngHost = new TypeScriptServiceHost(mockHost, service);
ngService = createLanguageService(ngHost);
Expand Down
3 changes: 1 addition & 2 deletions packages/language-service/test/diagnostics_spec.ts
Expand Up @@ -10,7 +10,6 @@ import * as ts from 'typescript';
import {createLanguageService} from '../src/language_service';
import * as ng from '../src/types';
import {TypeScriptServiceHost} from '../src/typescript_host';
import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';

/**
Expand All @@ -32,7 +31,7 @@ describe('diagnostics', () => {
let ngLS: ng.LanguageService;

beforeEach(() => {
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh);
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
tsLS = ts.createLanguageService(mockHost);
ngHost = new TypeScriptServiceHost(mockHost, tsLS);
ngLS = createLanguageService(ngHost);
Expand Down
3 changes: 1 addition & 2 deletions packages/language-service/test/hover_spec.ts
Expand Up @@ -12,7 +12,6 @@ import {createLanguageService} from '../src/language_service';
import {LanguageService} from '../src/types';
import {TypeScriptServiceHost} from '../src/typescript_host';

import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';

describe('hover', () => {
Expand All @@ -22,7 +21,7 @@ describe('hover', () => {
let ngLS: LanguageService;

beforeEach(() => {
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh);
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
tsLS = ts.createLanguageService(mockHost);
ngLSHost = new TypeScriptServiceHost(mockHost, tsLS);
ngLS = createLanguageService(ngLSHost);
Expand Down
5 changes: 2 additions & 3 deletions packages/language-service/test/language_service_spec.ts
Expand Up @@ -11,11 +11,10 @@ import * as ts from 'typescript';
import {createLanguageService} from '../src/language_service';
import {TypeScriptServiceHost} from '../src/typescript_host';

import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';

describe('service without angular', () => {
let mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh);
let mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
mockHost.forgetAngular();
let service = ts.createLanguageService(mockHost);
let ngHost = new TypeScriptServiceHost(mockHost, service);
Expand All @@ -32,4 +31,4 @@ describe('service without angular', () => {
it('should not crash a get definition',
() => expect(() => ngService.getDefinitionAt(fileName, position)).not.toThrow());
it('should not crash a hover', () => expect(() => ngService.getHoverAt(fileName, position)));
});
});
6 changes: 2 additions & 4 deletions packages/language-service/test/reflector_host_spec.ts
Expand Up @@ -9,11 +9,9 @@
import * as path from 'path';
import * as ts from 'typescript';

import {createLanguageService} from '../src/language_service';
import {ReflectorHost} from '../src/reflector_host';
import {TypeScriptServiceHost} from '../src/typescript_host';

import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';

describe('reflector_host_spec', () => {
Expand All @@ -23,7 +21,7 @@ describe('reflector_host_spec', () => {
const originalJoin = path.join;
const originalPosixJoin = path.posix.join;
const mockHost =
new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh, 'node_modules', {
new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], 'node_modules', {
...path,
join: (...args: string[]) => originalJoin.apply(path, args),
posix:
Expand All @@ -42,7 +40,7 @@ describe('reflector_host_spec', () => {
});

it('should use module resolution cache', () => {
const mockHost = new MockTypescriptHost(['/app/main.ts'], toh);
const mockHost = new MockTypescriptHost(['/app/main.ts']);
// TypeScript relies on `ModuleResolutionHost.fileExists()` to perform
// module resolution, so spy on this method to determine how many times
// it's called.
Expand Down
3 changes: 1 addition & 2 deletions packages/language-service/test/template_references_spec.ts
Expand Up @@ -12,7 +12,6 @@ import {createLanguageService} from '../src/language_service';
import {LanguageService} from '../src/types';
import {TypeScriptServiceHost} from '../src/typescript_host';

import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';

describe('references', () => {
Expand All @@ -23,7 +22,7 @@ describe('references', () => {
let ngService: LanguageService = createLanguageService(undefined !);

beforeEach(() => {
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh);
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
service = ts.createLanguageService(mockHost, documentRegistry);
ngHost = new TypeScriptServiceHost(mockHost, service);
ngService = createLanguageService(ngHost);
Expand Down
3 changes: 1 addition & 2 deletions packages/language-service/test/template_spec.ts
Expand Up @@ -8,7 +8,6 @@

import * as ts from 'typescript';
import {getClassDeclFromDecoratorProp} from '../src/template';
import {toh} from './test_data';
import {MockTypescriptHost} from './test_utils';

describe('getClassDeclFromTemplateNode', () => {
Expand All @@ -35,7 +34,7 @@ describe('getClassDeclFromTemplateNode', () => {


it('should return class declaration for AppComponent', () => {
const host = new MockTypescriptHost(['/app/app.component.ts'], toh);
const host = new MockTypescriptHost(['/app/app.component.ts']);
const tsLS = ts.createLanguageService(host);
const sourceFile = tsLS.getProgram() !.getSourceFile('/app/app.component.ts');
expect(sourceFile).toBeTruthy();
Expand Down