Enable unit tests for DocumentRegistry

This commit is contained in:
Mohamed Hegazy 2014-08-07 23:32:43 -07:00
parent d028c06034
commit 38cacc967f
3 changed files with 24 additions and 159 deletions

View File

@ -693,8 +693,7 @@ module ts {
compilationSettings: CompilerOptions,
scriptSnapshot: TypeScript.IScriptSnapshot,
version: number,
isOpen: boolean,
referencedFiles: string[]): SourceFile;
isOpen: boolean): SourceFile;
updateDocument(
soruceFile: SourceFile,
@ -1350,7 +1349,7 @@ module ts {
sourceFile = documentRegistry.updateDocument(sourceFile, filename, compilationSettings, scriptSnapshot, version, isOpen, textChangeRange);
}
else {
sourceFile = documentRegistry.acquireDocument(filename, compilationSettings, scriptSnapshot, version, isOpen, []);
sourceFile = documentRegistry.acquireDocument(filename, compilationSettings, scriptSnapshot, version, isOpen);
}
// Remeber the new sourceFile

View File

@ -837,8 +837,8 @@ module ts {
public createLanguageServiceShim(host: LanguageServiceShimHost): LanguageServiceShim {
try {
var hostAdapter = new LanguageServiceShimHostAdapter(host);
var pullLanguageService = createLanguageService(hostAdapter, this.documentRegistry);
return new LanguageServiceShimObject(this, host, pullLanguageService);
var languageService = createLanguageService(hostAdapter, this.documentRegistry);
return new LanguageServiceShimObject(this, host, languageService);
}
catch (err) {
logInternalError(host, err);

View File

@ -1,172 +1,38 @@
///<reference path='..\..\..\..\src\compiler\typescript.ts' />
///<reference path='..\..\..\..\src\harness\harness.ts' />
///<reference path='..\..\..\..\src\services\typescriptServices.ts' />
class TestSourceFile {
constructor(
public fileName: string,
public version: number,
public scriptSnapshot: TypeScript.IScriptSnapshot,
public isOpen: boolean,
public byteOrderMark: TypeScript.ByteOrderMark = TypeScript.ByteOrderMark.Utf8) {
}
}
class TestHostSettings {
constructor(
public files: TypeScript.StringHashTable<TestSourceFile>,
public compilationSettings: TypeScript.CompilationSettings = TypeScript.ImmutableCompilationSettings.defaultSettings().toCompilationSettings()) {
}
}
describe("testDocumentRetrievalAndUpdate", () => {
function getHost(settings: TestHostSettings): TypeScript.Services.ILanguageServiceHost {
return {
getCompilationSettings(): TypeScript.CompilationSettings {
return settings.compilationSettings;
},
getScriptFileNames(): string[]{
return settings.files.getAllKeys();
},
getScriptVersion(fileName: string): number {
return settings.files.lookup(fileName).version;
},
getScriptIsOpen(fileName: string): boolean {
return settings.files.lookup(fileName).isOpen;
},
getScriptByteOrderMark(fileName: string): TypeScript.ByteOrderMark {
return settings.files.lookup(fileName).byteOrderMark;
},
getScriptSnapshot(fileName: string): TypeScript.IScriptSnapshot {
return settings.files.lookup(fileName).scriptSnapshot;
},
getDiagnosticsObject(): TypeScript.Services.ILanguageServicesDiagnostics {
throw TypeScript.Errors.notYetImplemented();
},
getLocalizedDiagnosticMessages(): any {
return null;
},
information(): boolean {
return false;
},
debug(): boolean {
return false;
},
warning(): boolean {
return false;
},
error(): boolean {
return false;
},
fatal(): boolean {
return false;
},
log(s: string): void {
},
resolveRelativePath(path: string, directory: string): string {
throw TypeScript.Errors.notYetImplemented();
},
fileExists(path: string): boolean {
throw TypeScript.Errors.notYetImplemented();
},
directoryExists(path: string): boolean {
throw TypeScript.Errors.notYetImplemented();
},
getParentDirectory(path: string): string {
throw TypeScript.Errors.notYetImplemented();
},
getCancellationToken(): TypeScript.ICancellationToken {
return TypeScript.CancellationToken.None;
}
}
}
function getLanguageServiceCompiler(ls: TypeScript.Services.ILanguageService): TypeScript.Services.LanguageServiceCompiler {
return <TypeScript.Services.LanguageServiceCompiler>(<any>ls).compiler
}
describe("DocumentRegistry", () => {
it("documents are shared between projects", () => {
function ensureDocumentIsShared(prefix: string, ls1: TypeScript.Services.ILanguageService, ls2: TypeScript.Services.ILanguageService, fileName: string): void {
var c1 = getLanguageServiceCompiler(ls1);
var c2 = getLanguageServiceCompiler(ls2);
// getDocument synchronized its internal state with host
var doc1 = c1.getDocument(fileName);
var doc2 = c2.getDocument(fileName);
if (doc1 !== doc2) {
throw new Error(prefix + ":document should be shared between language services");
}
}
var files = new TypeScript.StringHashTable<TestSourceFile>();
var f1 = new TestSourceFile("file1.ts", 1, TypeScript.ScriptSnapshot.fromString("var x = 1;"), false);
files.add(f1.fileName, f1);
var factory = new TypeScript.Services.TypeScriptServicesFactory();
var documentRegistry = ts.createDocumentRegistry();
var defaultCompilerOptions = ts.getDefaultCompilerOptions();
var hostSettings = new TestHostSettings(files);
var ls1 = factory.createPullLanguageService(getHost(hostSettings));
var ls2 = factory.createPullLanguageService(getHost(hostSettings));
var f1 = documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
var f2 = documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
ensureDocumentIsShared("==1==", ls1, ls2, f1.fileName);
f1.version = 2;
f1.scriptSnapshot = TypeScript.ScriptSnapshot.fromString("var x = 2;");
ensureDocumentIsShared("==2==", ls1, ls2, f1.fileName);
assert(f1 === f2, "DocumentRegistry should return the same document for the same name");
});
it("documents are refreshed when settings in compilation settings affect syntax", () => {
var files = new TypeScript.StringHashTable<TestSourceFile>();
var f1 = new TestSourceFile("file1.ts", 1, TypeScript.ScriptSnapshot.fromString("var x = 1;"), false);
files.add(f1.fileName, f1);
var factory = new TypeScript.Services.TypeScriptServicesFactory();
var hostSettings = new TestHostSettings(files);
var factory = new TypeScript.Services.TypeScriptServicesFactory();
var ls = factory.createPullLanguageService(getHost(hostSettings));
var compiler = getLanguageServiceCompiler(ls);
var d1 = compiler.getDocument(f1.fileName);
var documentRegistry = ts.createDocumentRegistry();
var compilerOptions: ts.CompilerOptions = { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.AMD };
// change compilation setting that doesn't affect parsing - should have the same document
hostSettings.compilationSettings.generateDeclarationFiles = !hostSettings.compilationSettings.generateDeclarationFiles;
var d2 = compiler.getDocument(f1.fileName);
compilerOptions.declaration = true;
var f1 = documentRegistry.acquireDocument("file1.ts", compilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
compilerOptions.declaration = false;
var f2 = documentRegistry.acquireDocument("file1.ts", compilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
assert(f1 === f2, "Expected to have the same document instance");
if (d1 !== d2) {
throw new Error("Expected to have the same document instance");
}
// change value of compilation setting that is used during production of AST - new document is required
hostSettings.compilationSettings.codeGenTarget = TypeScript.LanguageVersion.EcmaScript5;
var d3 = compiler.getDocument(f1.fileName);
if (d2 === d3) {
throw new Error("Changed codeGenTarget: Expected to have different instances of document");
}
compilerOptions.target = ts.ScriptTarget.ES3;
var f3 = documentRegistry.acquireDocument("file1.ts", compilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
hostSettings.compilationSettings.propagateEnumConstants = !hostSettings.compilationSettings.propagateEnumConstants;
var d4 = compiler.getDocument(f1.fileName);
if (d3 === d4) {
throw new Error("Changed propagateEnumConstants: Expected to have different instances of document");
}
assert(f1 !== f3, "Changed target: Expected to have different instances of document");
hostSettings.compilationSettings.allowAutomaticSemicolonInsertion = !hostSettings.compilationSettings.allowAutomaticSemicolonInsertion;
var d5 = compiler.getDocument(f1.fileName);
if (d4 === d5) {
throw new Error("Changed allowAutomaticSemicolonInsertion: Expected to have different instances of document");
}
compilerOptions.module = ts.ModuleKind.CommonJS;
var f4 = documentRegistry.acquireDocument("file1.ts", compilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
assert(f1 !== f4, "Changed module: Expected to have different instances of document");
});
});