|
| 1 | +/// <reference path="..\..\..\src\harness\harness.ts" /> |
| 2 | +/// <reference path="..\..\..\src\compiler\commandLineParser.ts" /> |
| 3 | + |
| 4 | +namespace ts { |
| 5 | + describe('parseCommandLine', () => { |
| 6 | + |
| 7 | + function assertParseResult(commandLine: string[], expectedParsedCommandLine: ts.ParsedCommandLine) { |
| 8 | + const parsed = ts.parseCommandLine(commandLine); |
| 9 | + const parsedCompilerOptions = JSON.stringify(parsed.options); |
| 10 | + const expectedCompilerOptions = JSON.stringify(expectedParsedCommandLine.options); |
| 11 | + assert.equal(parsedCompilerOptions, expectedCompilerOptions); |
| 12 | + |
| 13 | + const parsedErrors = parsed.errors; |
| 14 | + const expectedErrors = expectedParsedCommandLine.errors; |
| 15 | + assert.isTrue(parsedErrors.length === expectedErrors.length, `Expected error: ${JSON.stringify(expectedErrors)}. Actual error: ${JSON.stringify(parsedErrors)}.`); |
| 16 | + for (let i = 0; i < parsedErrors.length; ++i) { |
| 17 | + const parsedError = parsedErrors[i]; |
| 18 | + const expectedError = expectedErrors[i]; |
| 19 | + assert.equal(parsedError.code, expectedError.code); |
| 20 | + assert.equal(parsedError.category, expectedError.category); |
| 21 | + assert.equal(parsedError.messageText, expectedError.messageText); |
| 22 | + } |
| 23 | + |
| 24 | + const parsedFileNames = parsed.fileNames; |
| 25 | + const expectedFileNames = expectedParsedCommandLine.fileNames; |
| 26 | + assert.isTrue(parsedFileNames.length === expectedFileNames.length, `Expected fileNames: [${JSON.stringify(expectedFileNames)}]. Actual fileNames: [${JSON.stringify(parsedFileNames)}].`); |
| 27 | + for (let i = 0; i < parsedFileNames.length; ++i) { |
| 28 | + const parsedFileName = parsedFileNames[i]; |
| 29 | + const expectedFileName = expectedFileNames[i]; |
| 30 | + assert.equal(parsedFileName, expectedFileName); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + it("Parse empty options of --jsx ", () => { |
| 35 | + // 0.ts --jsx |
| 36 | + assertParseResult(["0.ts", "--jsx"], |
| 37 | + { |
| 38 | + errors: [{ |
| 39 | + messageText: "Compiler option 'jsx' expects an argument.", |
| 40 | + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, |
| 41 | + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, |
| 42 | + |
| 43 | + file: undefined, |
| 44 | + start: undefined, |
| 45 | + length: undefined, |
| 46 | + }, { |
| 47 | + messageText: "Argument for '--jsx' option must be: 'preserve', 'react'", |
| 48 | + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, |
| 49 | + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, |
| 50 | + |
| 51 | + file: undefined, |
| 52 | + start: undefined, |
| 53 | + length: undefined, |
| 54 | + }], |
| 55 | + fileNames: ["0.ts"], |
| 56 | + options: {} |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("Parse empty options of --module ", () => { |
| 61 | + // 0.ts -- |
| 62 | + assertParseResult(["0.ts", "--module"], |
| 63 | + { |
| 64 | + errors: [{ |
| 65 | + messageText: "Compiler option 'module' expects an argument.", |
| 66 | + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, |
| 67 | + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, |
| 68 | + |
| 69 | + file: undefined, |
| 70 | + start: undefined, |
| 71 | + length: undefined, |
| 72 | + }, { |
| 73 | + messageText: "Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015'", |
| 74 | + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, |
| 75 | + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, |
| 76 | + |
| 77 | + file: undefined, |
| 78 | + start: undefined, |
| 79 | + length: undefined, |
| 80 | + }], |
| 81 | + fileNames: ["0.ts"], |
| 82 | + options: {} |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("Parse empty options of --newLine ", () => { |
| 87 | + // 0.ts --newLine |
| 88 | + assertParseResult(["0.ts", "--newLine"], |
| 89 | + { |
| 90 | + errors: [{ |
| 91 | + messageText: "Compiler option 'newLine' expects an argument.", |
| 92 | + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, |
| 93 | + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, |
| 94 | + |
| 95 | + file: undefined, |
| 96 | + start: undefined, |
| 97 | + length: undefined, |
| 98 | + }, { |
| 99 | + messageText: "Argument for '--newLine' option must be: 'crlf', 'lf'", |
| 100 | + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, |
| 101 | + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, |
| 102 | + |
| 103 | + file: undefined, |
| 104 | + start: undefined, |
| 105 | + length: undefined, |
| 106 | + }], |
| 107 | + fileNames: ["0.ts"], |
| 108 | + options: {} |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it("Parse empty options of --target ", () => { |
| 113 | + // 0.ts --target |
| 114 | + assertParseResult(["0.ts", "--target"], |
| 115 | + { |
| 116 | + errors: [{ |
| 117 | + messageText: "Compiler option 'target' expects an argument.", |
| 118 | + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, |
| 119 | + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, |
| 120 | + |
| 121 | + file: undefined, |
| 122 | + start: undefined, |
| 123 | + length: undefined, |
| 124 | + }, { |
| 125 | + messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015'", |
| 126 | + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, |
| 127 | + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, |
| 128 | + |
| 129 | + file: undefined, |
| 130 | + start: undefined, |
| 131 | + length: undefined, |
| 132 | + }], |
| 133 | + fileNames: ["0.ts"], |
| 134 | + options: {} |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + it("Parse empty options of --moduleResolution ", () => { |
| 139 | + // 0.ts --moduleResolution |
| 140 | + assertParseResult(["0.ts", "--moduleResolution"], |
| 141 | + { |
| 142 | + errors: [{ |
| 143 | + messageText: "Compiler option 'moduleResolution' expects an argument.", |
| 144 | + category: ts.Diagnostics.Compiler_option_0_expects_an_argument.category, |
| 145 | + code: ts.Diagnostics.Compiler_option_0_expects_an_argument.code, |
| 146 | + |
| 147 | + file: undefined, |
| 148 | + start: undefined, |
| 149 | + length: undefined, |
| 150 | + }, { |
| 151 | + messageText: "Argument for '--moduleResolution' option must be: 'node', 'classic'", |
| 152 | + category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, |
| 153 | + code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, |
| 154 | + |
| 155 | + file: undefined, |
| 156 | + start: undefined, |
| 157 | + length: undefined, |
| 158 | + }], |
| 159 | + fileNames: ["0.ts"], |
| 160 | + options: {} |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + it("Parse multiple compiler flags with input files at the end", () => { |
| 165 | + // --module commonjs --target es5 0.ts |
| 166 | + assertParseResult(["--module", "commonjs", "--target", "es5", "0.ts"], |
| 167 | + { |
| 168 | + errors: [], |
| 169 | + fileNames: ["0.ts"], |
| 170 | + options: { |
| 171 | + module: ts.ModuleKind.CommonJS, |
| 172 | + target: ts.ScriptTarget.ES5, |
| 173 | + } |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it("Parse multiple compiler flags with input files in the middle", () => { |
| 178 | + // --module commonjs --target es5 0.ts --noImplicitAny |
| 179 | + assertParseResult(["--module", "commonjs", "--target", "es5", "0.ts", "--noImplicitAny"], |
| 180 | + { |
| 181 | + errors: [], |
| 182 | + fileNames: ["0.ts"], |
| 183 | + options: { |
| 184 | + module: ts.ModuleKind.CommonJS, |
| 185 | + target: ts.ScriptTarget.ES5, |
| 186 | + noImplicitAny: true, |
| 187 | + } |
| 188 | + }); |
| 189 | + }); |
| 190 | + }); |
| 191 | +} |
0 commit comments