Skip to content

Commit 38cacc9

Browse files
committed
Enable unit tests for DocumentRegistry
1 parent d028c06 commit 38cacc9

File tree

3 files changed

+24
-159
lines changed

3 files changed

+24
-159
lines changed

src/services/services.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,7 @@ module ts {
693693
compilationSettings: CompilerOptions,
694694
scriptSnapshot: TypeScript.IScriptSnapshot,
695695
version: number,
696-
isOpen: boolean,
697-
referencedFiles: string[]): SourceFile;
696+
isOpen: boolean): SourceFile;
698697

699698
updateDocument(
700699
soruceFile: SourceFile,
@@ -1350,7 +1349,7 @@ module ts {
13501349
sourceFile = documentRegistry.updateDocument(sourceFile, filename, compilationSettings, scriptSnapshot, version, isOpen, textChangeRange);
13511350
}
13521351
else {
1353-
sourceFile = documentRegistry.acquireDocument(filename, compilationSettings, scriptSnapshot, version, isOpen, []);
1352+
sourceFile = documentRegistry.acquireDocument(filename, compilationSettings, scriptSnapshot, version, isOpen);
13541353
}
13551354

13561355
// Remeber the new sourceFile

src/services/shims.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,8 +837,8 @@ module ts {
837837
public createLanguageServiceShim(host: LanguageServiceShimHost): LanguageServiceShim {
838838
try {
839839
var hostAdapter = new LanguageServiceShimHostAdapter(host);
840-
var pullLanguageService = createLanguageService(hostAdapter, this.documentRegistry);
841-
return new LanguageServiceShimObject(this, host, pullLanguageService);
840+
var languageService = createLanguageService(hostAdapter, this.documentRegistry);
841+
return new LanguageServiceShimObject(this, host, languageService);
842842
}
843843
catch (err) {
844844
logInternalError(host, err);
Lines changed: 20 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,38 @@
1-
///<reference path='..\..\..\..\src\compiler\typescript.ts' />
21
///<reference path='..\..\..\..\src\harness\harness.ts' />
3-
///<reference path='..\..\..\..\src\services\typescriptServices.ts' />
4-
5-
class TestSourceFile {
6-
constructor(
7-
public fileName: string,
8-
public version: number,
9-
public scriptSnapshot: TypeScript.IScriptSnapshot,
10-
public isOpen: boolean,
11-
public byteOrderMark: TypeScript.ByteOrderMark = TypeScript.ByteOrderMark.Utf8) {
12-
}
13-
}
14-
15-
class TestHostSettings {
16-
constructor(
17-
public files: TypeScript.StringHashTable<TestSourceFile>,
18-
public compilationSettings: TypeScript.CompilationSettings = TypeScript.ImmutableCompilationSettings.defaultSettings().toCompilationSettings()) {
19-
}
20-
}
21-
22-
describe("testDocumentRetrievalAndUpdate", () => {
23-
function getHost(settings: TestHostSettings): TypeScript.Services.ILanguageServiceHost {
24-
return {
25-
getCompilationSettings(): TypeScript.CompilationSettings {
26-
return settings.compilationSettings;
27-
},
28-
29-
getScriptFileNames(): string[]{
30-
return settings.files.getAllKeys();
31-
},
32-
33-
getScriptVersion(fileName: string): number {
34-
return settings.files.lookup(fileName).version;
35-
},
36-
37-
getScriptIsOpen(fileName: string): boolean {
38-
return settings.files.lookup(fileName).isOpen;
39-
},
40-
41-
getScriptByteOrderMark(fileName: string): TypeScript.ByteOrderMark {
42-
return settings.files.lookup(fileName).byteOrderMark;
43-
},
44-
45-
getScriptSnapshot(fileName: string): TypeScript.IScriptSnapshot {
46-
return settings.files.lookup(fileName).scriptSnapshot;
47-
},
48-
49-
getDiagnosticsObject(): TypeScript.Services.ILanguageServicesDiagnostics {
50-
throw TypeScript.Errors.notYetImplemented();
51-
},
52-
53-
getLocalizedDiagnosticMessages(): any {
54-
return null;
55-
},
56-
57-
information(): boolean {
58-
return false;
59-
},
60-
61-
debug(): boolean {
62-
return false;
63-
},
64-
65-
warning(): boolean {
66-
return false;
67-
},
68-
69-
error(): boolean {
70-
return false;
71-
},
72-
73-
fatal(): boolean {
74-
return false;
75-
},
76-
77-
log(s: string): void {
78-
},
79-
80-
resolveRelativePath(path: string, directory: string): string {
81-
throw TypeScript.Errors.notYetImplemented();
82-
},
83-
fileExists(path: string): boolean {
84-
throw TypeScript.Errors.notYetImplemented();
85-
},
86-
directoryExists(path: string): boolean {
87-
throw TypeScript.Errors.notYetImplemented();
88-
},
89-
getParentDirectory(path: string): string {
90-
throw TypeScript.Errors.notYetImplemented();
91-
},
92-
getCancellationToken(): TypeScript.ICancellationToken {
93-
return TypeScript.CancellationToken.None;
94-
}
95-
}
96-
}
97-
98-
function getLanguageServiceCompiler(ls: TypeScript.Services.ILanguageService): TypeScript.Services.LanguageServiceCompiler {
99-
return <TypeScript.Services.LanguageServiceCompiler>(<any>ls).compiler
100-
}
1012

3+
describe("DocumentRegistry", () => {
1024
it("documents are shared between projects", () => {
103-
function ensureDocumentIsShared(prefix: string, ls1: TypeScript.Services.ILanguageService, ls2: TypeScript.Services.ILanguageService, fileName: string): void {
104-
var c1 = getLanguageServiceCompiler(ls1);
105-
var c2 = getLanguageServiceCompiler(ls2);
106-
// getDocument synchronized its internal state with host
107-
var doc1 = c1.getDocument(fileName);
108-
var doc2 = c2.getDocument(fileName);
109-
if (doc1 !== doc2) {
110-
throw new Error(prefix + ":document should be shared between language services");
111-
}
112-
}
113-
var files = new TypeScript.StringHashTable<TestSourceFile>();
114-
var f1 = new TestSourceFile("file1.ts", 1, TypeScript.ScriptSnapshot.fromString("var x = 1;"), false);
115-
files.add(f1.fileName, f1);
116-
var factory = new TypeScript.Services.TypeScriptServicesFactory();
5+
var documentRegistry = ts.createDocumentRegistry();
6+
var defaultCompilerOptions = ts.getDefaultCompilerOptions();
1177

118-
var hostSettings = new TestHostSettings(files);
119-
var ls1 = factory.createPullLanguageService(getHost(hostSettings));
120-
var ls2 = factory.createPullLanguageService(getHost(hostSettings));
8+
var f1 = documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
9+
var f2 = documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
12110

122-
ensureDocumentIsShared("==1==", ls1, ls2, f1.fileName);
123-
124-
f1.version = 2;
125-
f1.scriptSnapshot = TypeScript.ScriptSnapshot.fromString("var x = 2;");
126-
127-
ensureDocumentIsShared("==2==", ls1, ls2, f1.fileName);
11+
assert(f1 === f2, "DocumentRegistry should return the same document for the same name");
12812
});
12913

13014
it("documents are refreshed when settings in compilation settings affect syntax", () => {
131-
var files = new TypeScript.StringHashTable<TestSourceFile>();
132-
var f1 = new TestSourceFile("file1.ts", 1, TypeScript.ScriptSnapshot.fromString("var x = 1;"), false);
133-
files.add(f1.fileName, f1);
134-
var factory = new TypeScript.Services.TypeScriptServicesFactory();
135-
136-
var hostSettings = new TestHostSettings(files);
137-
138-
var factory = new TypeScript.Services.TypeScriptServicesFactory();
139-
var ls = factory.createPullLanguageService(getHost(hostSettings));
140-
var compiler = getLanguageServiceCompiler(ls);
141-
142-
var d1 = compiler.getDocument(f1.fileName);
15+
var documentRegistry = ts.createDocumentRegistry();
16+
var compilerOptions: ts.CompilerOptions = { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.AMD };
14317

14418
// change compilation setting that doesn't affect parsing - should have the same document
145-
hostSettings.compilationSettings.generateDeclarationFiles = !hostSettings.compilationSettings.generateDeclarationFiles;
146-
var d2 = compiler.getDocument(f1.fileName);
19+
compilerOptions.declaration = true;
20+
var f1 = documentRegistry.acquireDocument("file1.ts", compilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
21+
compilerOptions.declaration = false;
22+
var f2 = documentRegistry.acquireDocument("file1.ts", compilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
23+
24+
assert(f1 === f2, "Expected to have the same document instance");
14725

148-
if (d1 !== d2) {
149-
throw new Error("Expected to have the same document instance");
150-
}
15126

15227
// change value of compilation setting that is used during production of AST - new document is required
153-
hostSettings.compilationSettings.codeGenTarget = TypeScript.LanguageVersion.EcmaScript5;
154-
var d3 = compiler.getDocument(f1.fileName);
155-
if (d2 === d3) {
156-
throw new Error("Changed codeGenTarget: Expected to have different instances of document");
157-
}
28+
compilerOptions.target = ts.ScriptTarget.ES3;
29+
var f3 = documentRegistry.acquireDocument("file1.ts", compilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
15830

159-
hostSettings.compilationSettings.propagateEnumConstants = !hostSettings.compilationSettings.propagateEnumConstants;
160-
var d4 = compiler.getDocument(f1.fileName);
161-
if (d3 === d4) {
162-
throw new Error("Changed propagateEnumConstants: Expected to have different instances of document");
163-
}
31+
assert(f1 !== f3, "Changed target: Expected to have different instances of document");
16432

165-
hostSettings.compilationSettings.allowAutomaticSemicolonInsertion = !hostSettings.compilationSettings.allowAutomaticSemicolonInsertion;
166-
var d5 = compiler.getDocument(f1.fileName);
167-
if (d4 === d5) {
168-
throw new Error("Changed allowAutomaticSemicolonInsertion: Expected to have different instances of document");
169-
}
33+
compilerOptions.module = ts.ModuleKind.CommonJS;
34+
var f4 = documentRegistry.acquireDocument("file1.ts", compilerOptions, TypeScript.ScriptSnapshot.fromString("var x = 1;"), 1, false);
17035

36+
assert(f1 !== f4, "Changed module: Expected to have different instances of document");
17137
});
17238
});

0 commit comments

Comments
 (0)