|
| 1 | +/// <reference path="..\..\..\src\harness\harness.ts" /> |
| 2 | + |
| 3 | +module ts { |
| 4 | + describe("Transpile", () => { |
| 5 | + |
| 6 | + function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void { |
| 7 | + let diagnostics: Diagnostic[] = []; |
| 8 | + let result = transpile(input, compilerOptions, "file.ts", diagnostics); |
| 9 | + |
| 10 | + assert.equal(diagnostics.length, expectedDiagnosticCodes.length); |
| 11 | + for (let diagnostic of diagnostics) { |
| 12 | + assert.isTrue(expectedDiagnosticCodes.indexOf(diagnostic.code) >= 0, `Found an unexpected diagnostic: ${ diagnostic.code }`); |
| 13 | + } |
| 14 | + |
| 15 | + if (expectedOutput !== undefined) { |
| 16 | + assert.equal(result, expectedOutput); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + it("Generates correct compilerOptions diagnostics", () => { |
| 21 | + // Expecting 5047: "Option 'isolatedModules' can only be used when either option'--module' is provided or option 'target' is 'ES6' or higher." |
| 22 | + runTest(`var x = 0;`, {}, /*expectedOutput*/ undefined, [5047]); |
| 23 | + }); |
| 24 | + |
| 25 | + it("Generates no diagnostics with valid inputs", () => { |
| 26 | + // No errors |
| 27 | + runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []); |
| 28 | + }); |
| 29 | + |
| 30 | + it("Generates no diagnostics for missing file references", () => { |
| 31 | + runTest(`/// <reference path="file2.ts" /> |
| 32 | +var x = 0;`, |
| 33 | + { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []); |
| 34 | + }); |
| 35 | + |
| 36 | + it("Generates no diagnostics for missing module imports", () => { |
| 37 | + runTest(`import {a} from "module2";`, |
| 38 | + { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []); |
| 39 | + }); |
| 40 | + |
| 41 | + it("Generates expected syntactic diagnostics", () => { |
| 42 | + runTest(`a b`, |
| 43 | + { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, [1005]); /// 1005: ';' Expected |
| 44 | + }); |
| 45 | + |
| 46 | + it("Does not generate semantic diagnostics", () => { |
| 47 | + runTest(`var x: string = 0;`, |
| 48 | + { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []); |
| 49 | + }); |
| 50 | + |
| 51 | + it("Generates module output", () => { |
| 52 | + runTest(`var x = 0;`, { module: ModuleKind.AMD }, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`); |
| 53 | + }); |
| 54 | + |
| 55 | + it("Uses correct newLine character", () => { |
| 56 | + runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, `var x = 0;\n`); |
| 57 | + }); |
| 58 | + }); |
| 59 | +} |
0 commit comments