mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-13 06:20:23 -06:00
Add unittest for testing convert compiler-options and typing-options
This commit is contained in:
parent
157b8e7456
commit
f4e920e2c1
@ -146,12 +146,14 @@ var harnessSources = harnessCoreSources.concat([
|
||||
"session.ts",
|
||||
"versionCache.ts",
|
||||
"convertToBase64.ts",
|
||||
"transpile.ts",
|
||||
"transpile.ts",
|
||||
"reuseProgramStructure.ts",
|
||||
"cachingInServerLSHost.ts",
|
||||
"moduleResolution.ts",
|
||||
"tsconfigParsing.ts",
|
||||
"commandLineParsing.ts"
|
||||
"commandLineParsing.ts",
|
||||
"convertCompilerOptionsFromJson.ts",
|
||||
"convertTypingOptionsFromJson.ts"
|
||||
].map(function (f) {
|
||||
return path.join(unittestsDirectory, f);
|
||||
})).concat([
|
||||
|
||||
@ -656,7 +656,7 @@ namespace ts {
|
||||
function getFileNames(errors: Diagnostic[]): string[] {
|
||||
let fileNames: string[] = [];
|
||||
if (hasProperty(json, "files")) {
|
||||
if (json["files"] instanceof Array) {
|
||||
if (isArray(json["files"])) {
|
||||
fileNames = map(<string[]>json["files"], s => combinePaths(basePath, s));
|
||||
}
|
||||
else {
|
||||
@ -667,7 +667,7 @@ namespace ts {
|
||||
const filesSeen: Map<boolean> = {};
|
||||
|
||||
let exclude: string[] = [];
|
||||
if (json["exclude"] instanceof Array) {
|
||||
if (isArray(json["exclude"])) {
|
||||
exclude = json["exclude"];
|
||||
}
|
||||
else {
|
||||
@ -716,18 +716,8 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } {
|
||||
const errors: Diagnostic[] = [];
|
||||
const options = convertOptionsFromJson<CompilerOptions>(optionDeclarations, jsonOptions, basePath, configFileName, errors);
|
||||
|
||||
if (configFileName && getBaseFileName(configFileName) === "jsconfig.json" && typeof options.allowJs === "undefined") {
|
||||
options.allowJs = true;
|
||||
}
|
||||
|
||||
return { options, errors };
|
||||
}
|
||||
|
||||
function convertOptionsFromJson<T extends CompilerOptions | TypingOptions>(optionDeclarations: CommandLineOption[], jsonOptions: any, basePath: string, configFileName: string, errors: Diagnostic[]): T {
|
||||
/* @internal */
|
||||
export function convertOptionsFromJson<T extends CompilerOptions | TypingOptions>(optionDeclarations: CommandLineOption[], jsonOptions: any, basePath: string, configFileName: string, errors: Diagnostic[]): T {
|
||||
const options = {} as T;
|
||||
|
||||
if (!jsonOptions) {
|
||||
@ -781,11 +771,10 @@ namespace ts {
|
||||
}
|
||||
else {
|
||||
errors.push(createCompilerDiagnostic(opt.error));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function convertJsonOptionOfListType(option: CommandLineOptionOfListType, values: any[], basePath: string, errors: Diagnostic[]): any[] {
|
||||
return ts.map(values, v => convertJsonOption(option.element, v, basePath, errors));
|
||||
return ts.map(values, v => convertJsonOption(option.element, v, basePath, errors)).filter(v => { return v != undefined; });
|
||||
}
|
||||
}
|
||||
|
||||
78
tests/cases/unittests/convertCompilerOptionsFromJson.ts
Normal file
78
tests/cases/unittests/convertCompilerOptionsFromJson.ts
Normal file
@ -0,0 +1,78 @@
|
||||
/// <reference path="..\..\..\src\harness\harness.ts" />
|
||||
/// <reference path="..\..\..\src\compiler\commandLineParser.ts" />
|
||||
|
||||
namespace ts {
|
||||
describe('convertCompilerOptionsFromJson', () => {
|
||||
function assertCompilerOptions(json: any, expectedResult: { compilerOptions: CompilerOptions, errors: Diagnostic[] }) {
|
||||
const actualErrors: Diagnostic[] = [];
|
||||
const actualCompilerOptions = convertOptionsFromJson<CompilerOptions>(optionDeclarations, json["compilerOptions"], "/apath/", "tsconfig.json", actualErrors);
|
||||
|
||||
const parsedCompilerOptions = JSON.stringify(actualCompilerOptions);
|
||||
const expectedCompilerOptions = JSON.stringify(expectedResult.compilerOptions);
|
||||
assert.equal(parsedCompilerOptions, expectedCompilerOptions);
|
||||
|
||||
const expectedErrors = expectedResult.errors;
|
||||
assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`);
|
||||
for (let i = 0; i < actualErrors.length; ++i) {
|
||||
const actualError = actualErrors[i];
|
||||
const expectedError = expectedErrors[i];
|
||||
assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`);
|
||||
assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`);
|
||||
}
|
||||
}
|
||||
|
||||
const correctFormatOptions = {
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"noImplicitAny": false,
|
||||
"sourceMap": false,
|
||||
"lib": ["es5", "es6.array", "es6.symbol"]
|
||||
}
|
||||
}
|
||||
|
||||
const incorrectLibOption = {
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es5",
|
||||
"noImplicitAny": false,
|
||||
"sourceMap": false,
|
||||
"lib": ["es5", "es6.array", "es8"]
|
||||
}
|
||||
}
|
||||
|
||||
it("Convert correctly format JSON to compiler-options ", () => {
|
||||
assertCompilerOptions(correctFormatOptions, {
|
||||
compilerOptions: <CompilerOptions>{
|
||||
module: ModuleKind.CommonJS,
|
||||
target: ScriptTarget.ES5,
|
||||
noImplicitAny: false,
|
||||
sourceMap: false,
|
||||
lib: ["lib.es5.d.ts", "lib.es6.array.d.ts", "lib.es6.symbol.d.ts"]
|
||||
},
|
||||
errors: <Diagnostic[]>[]
|
||||
});
|
||||
});
|
||||
|
||||
it("Convert incorrectly option of libs to compiler-options ", () => {
|
||||
debugger;
|
||||
assertCompilerOptions(incorrectLibOption, {
|
||||
compilerOptions: <CompilerOptions>{
|
||||
module: ModuleKind.CommonJS,
|
||||
target: ScriptTarget.ES5,
|
||||
noImplicitAny: false,
|
||||
sourceMap: false,
|
||||
lib: ["lib.es5.d.ts", "lib.es6.array.d.ts"]
|
||||
},
|
||||
errors: [{
|
||||
file: undefined,
|
||||
start: 0,
|
||||
length: 0,
|
||||
messageText: "",
|
||||
code: Diagnostics.Arguments_for_library_option_must_be_Colon_0.code,
|
||||
category: Diagnostics.Arguments_for_library_option_must_be_Colon_0.category
|
||||
}]
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
44
tests/cases/unittests/convertTypingOptionsFromJson.ts
Normal file
44
tests/cases/unittests/convertTypingOptionsFromJson.ts
Normal file
@ -0,0 +1,44 @@
|
||||
/// <reference path="..\..\..\src\harness\harness.ts" />
|
||||
/// <reference path="..\..\..\src\compiler\commandLineParser.ts" />
|
||||
|
||||
namespace ts {
|
||||
describe('convertTypingOptionsFromJson', () => {
|
||||
function assertTypingOptions(json: any, expectedResult: { typingOptions: TypingOptions, errors: Diagnostic[] }) {
|
||||
const actualErrors: Diagnostic[] = [];
|
||||
const actualTypingOptions = convertOptionsFromJson<TypingOptions>(typingOptionDeclarations, json["typingOptions"], "/apath/", "tsconfig.json", actualErrors);
|
||||
|
||||
const parsedTypingOptions = JSON.stringify(actualTypingOptions);
|
||||
const expectedTypingOptions = JSON.stringify(expectedResult.typingOptions);
|
||||
assert.equal(parsedTypingOptions, parsedTypingOptions);
|
||||
|
||||
const expectedErrors = expectedResult.errors;
|
||||
assert.isTrue(expectedResult.errors.length === actualErrors.length, `Expected error: ${JSON.stringify(expectedResult.errors)}. Actual error: ${JSON.stringify(actualErrors)}.`);
|
||||
for (let i = 0; i < actualErrors.length; ++i) {
|
||||
const actualError = actualErrors[i];
|
||||
const expectedError = expectedErrors[i];
|
||||
assert.equal(actualError.code, expectedError.code, `Expected error-code: ${JSON.stringify(expectedError.code)}. Actual error-code: ${JSON.stringify(actualError.code)}.`);
|
||||
assert.equal(actualError.category, expectedError.category, `Expected error-category: ${JSON.stringify(expectedError.category)}. Actual error-category: ${JSON.stringify(actualError.category)}.`);
|
||||
}
|
||||
}
|
||||
|
||||
const correctFormatOptions = {
|
||||
"typingOptions": {
|
||||
"enableAutoDiscovery": true,
|
||||
"include": ["0.d.ts", "1.d.ts"],
|
||||
"exclude": ["0.js", "1.js"]
|
||||
}
|
||||
}
|
||||
|
||||
it("Convert correctly format JSON to compiler-options ", () => {
|
||||
debugger;
|
||||
assertTypingOptions(correctFormatOptions, {
|
||||
typingOptions: <TypingOptions>{
|
||||
enableAutoDiscovery: true,
|
||||
include: ["/apath/0.d.ts", "/apath/1.d.ts"],
|
||||
exclude: ["/apath/0.js", "/apath/1.js"]
|
||||
},
|
||||
errors: <Diagnostic[]>[]
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user