Files
angular-docs-cn/modules/@angular/compiler-cli/test/mocks.ts
T

107 lines
2.8 KiB
TypeScript
Raw Normal View History

import * as ts from 'typescript';
import {ReflectorHost, ReflectorHostContext} from '../src/reflector_host';
export type Entry = string | Directory;
2016-06-08 16:38:52 -07:00
export interface Directory { [name: string]: Entry; }
export class MockContext implements ReflectorHostContext {
2016-06-08 16:38:52 -07:00
constructor(public currentDirectory: string, private files: Entry) {}
2016-06-08 16:38:52 -07:00
exists(fileName: string): boolean { return this.getEntry(fileName) !== undefined; }
2016-06-08 16:38:52 -07:00
read(fileName: string): string|undefined {
let data = this.getEntry(fileName);
2016-06-08 16:38:52 -07:00
if (typeof data === 'string') {
return data;
}
return undefined;
}
write(fileName: string, data: string): void {
let parts = fileName.split('/');
let name = parts.pop();
let entry = this.getEntry(parts);
2016-06-08 16:38:52 -07:00
if (entry && typeof entry !== 'string') {
entry[name] = data;
}
}
2016-06-08 16:38:52 -07:00
getEntry(fileName: string|string[]): Entry|undefined {
let parts = typeof fileName === 'string' ? fileName.split('/') : fileName;
if (parts[0]) {
parts = this.currentDirectory.split('/').concat(parts);
}
parts.shift();
parts = normalize(parts);
let current = this.files;
while (parts.length) {
let part = parts.shift();
2016-06-08 16:38:52 -07:00
if (typeof current === 'string') {
return undefined;
}
let next = (<Directory>current)[part];
if (next === undefined) {
return undefined;
}
current = next;
}
return current;
}
}
function normalize(parts: string[]): string[] {
let result: string[] = [];
while (parts.length) {
let part = parts.shift();
switch (part) {
2016-06-08 16:38:52 -07:00
case '.':
break;
case '..':
result.pop();
break;
default:
result.push(part);
}
}
return result;
}
export class MockCompilerHost implements ts.CompilerHost {
2016-06-08 16:38:52 -07:00
constructor(private context: MockContext) {}
2016-06-08 16:38:52 -07:00
fileExists(fileName: string): boolean { return this.context.exists(fileName); }
2016-06-08 16:38:52 -07:00
readFile(fileName: string): string { return this.context.read(fileName); }
2016-06-08 16:38:52 -07:00
directoryExists(directoryName: string): boolean { return this.context.exists(directoryName); }
2016-06-08 16:38:52 -07:00
getSourceFile(
fileName: string, languageVersion: ts.ScriptTarget,
onError?: (message: string) => void): ts.SourceFile {
let sourceText = this.context.read(fileName);
if (sourceText) {
return ts.createSourceFile(fileName, sourceText, languageVersion);
} else {
return undefined;
}
}
getDefaultLibFileName(options: ts.CompilerOptions): string {
return ts.getDefaultLibFileName(options);
}
2016-06-08 16:38:52 -07:00
writeFile: ts.WriteFileCallback = (fileName, text) => { this.context.write(fileName, text); }
getCurrentDirectory(): string {
return this.context.currentDirectory;
}
2016-06-08 16:38:52 -07:00
getCanonicalFileName(fileName: string): string { return fileName; }
2016-06-08 16:38:52 -07:00
useCaseSensitiveFileNames(): boolean { return false; }
2016-06-08 16:38:52 -07:00
getNewLine(): string { return '\n'; }
}