Add tests for generateBOM switch

This commit is contained in:
Mohamed Hegazy
2014-08-06 12:05:01 -07:00
parent b1335e1795
commit 35c1577ff5
7 changed files with 68 additions and 15 deletions

View File

@@ -118,6 +118,10 @@ class CompilerBaselineRunner extends RunnerBase {
}
});
function getByteOrderMarkText(file: Harness.Compiler.GeneratedFile): string {
return file.writeByteOrderMark ? String.fromCharCode(ts.CharacterCodes.byteOrderMark) : '';
}
function getErrorBaseline(toBeCompiled: { unitName: string; content: string }[],
otherFiles: { unitName: string; content: string }[],
result: Harness.Compiler.CompilerResult
@@ -282,6 +286,7 @@ class CompilerBaselineRunner extends RunnerBase {
var jsCode = '';
for (var i = 0; i < result.files.length; i++) {
jsCode += '//// [' + Harness.Path.getFileName(result.files[i].fileName) + ']\r\n';
jsCode += getByteOrderMarkText(result.files[i]);
jsCode += result.files[i].code;
// Re-enable this if we want to do another comparison of old vs new compiler baselines
// jsCode += SyntacticCleaner.clean(result.files[i].code);
@@ -291,6 +296,7 @@ class CompilerBaselineRunner extends RunnerBase {
jsCode += '\r\n\r\n';
for (var i = 0; i < result.files.length; i++) {
jsCode += '//// [' + Harness.Path.getFileName(result.declFilesCode[i].fileName) + ']\r\n';
jsCode += getByteOrderMarkText(result.declFilesCode[i]);
jsCode += result.declFilesCode[i].code;
}
}
@@ -320,6 +326,7 @@ class CompilerBaselineRunner extends RunnerBase {
var sourceMapCode = '';
for (var i = 0; i < result.sourceMaps.length; i++) {
sourceMapCode += '//// [' + Harness.Path.getFileName(result.sourceMaps[i].fileName) + ']\r\n';
sourceMapCode += getByteOrderMarkText(result.sourceMaps[i]);
sourceMapCode += result.sourceMaps[i].code;
}

View File

@@ -712,6 +712,10 @@ module Harness {
// Not supported yet
break;
case 'generatebom':
options.generateBOM = !!setting.value;
break;
default:
throw new Error('Unsupported compiler setting ' + setting.flag);
}
@@ -725,13 +729,10 @@ module Harness {
inputFiles.forEach(register);
otherFiles.forEach(register);
var fileOutputs: {
fileName: string;
file: string;
}[] = [];
var fileOutputs: GeneratedFile[] = [];
var programFiles = inputFiles.map(file => file.unitName);
var program = ts.createProgram(programFiles, options, createCompilerHost(filemap, (fn, contents) => fileOutputs.push({ fileName: fn, file: contents })));
var program = ts.createProgram(programFiles, options, createCompilerHost(filemap, (fn, contents, writeByteOrderMark) => fileOutputs.push({ fileName: fn, code: contents, writeByteOrderMark: writeByteOrderMark })));
var hadParseErrors = program.getDiagnostics().length > 0;
@@ -810,6 +811,7 @@ module Harness {
export interface GeneratedFile {
fileName: string;
code: string;
writeByteOrderMark: boolean;
}
function stringEndsWith(str: string, end: string) {
@@ -837,19 +839,18 @@ module Harness {
public sourceMapRecord: string;
/** @param fileResults an array of strings for the fileName and an ITextWriter with its code */
constructor(fileResults: { fileName: string; file: string; }[], errors: MinimalDiagnostic[], sourceMapRecordLines: string[]) {
constructor(fileResults: GeneratedFile[], errors: MinimalDiagnostic[], sourceMapRecordLines: string[]) {
var lines: string[] = [];
fileResults.forEach(emittedFile => {
var fileObj = { fileName: emittedFile.fileName, code: emittedFile.file };
if (isDTS(emittedFile.fileName)) {
// .d.ts file, add to declFiles emit
this.declFilesCode.push(fileObj);
this.declFilesCode.push(emittedFile);
} else if (isJS(emittedFile.fileName)) {
// .js file, add to files
this.files.push(fileObj);
this.files.push(emittedFile);
} else if (isJSMap(emittedFile.fileName)) {
this.sourceMaps.push(fileObj);
this.sourceMaps.push(emittedFile);
} else {
throw new Error('Unrecognized file extension for file ' + emittedFile.fileName);
}
@@ -896,7 +897,7 @@ module Harness {
var optionRegex = /^[\/]{2}\s*@(\w+)\s*:\s*(\S*)/gm; // multiple matches on multiple lines
// List of allowed metadata names
var fileMetadataNames = ["filename", "comments", "declaration", "module", "nolib", "sourcemap", "target", "out", "outDir", "noimplicitany", "noresolve", "newline", "newlines"];
var fileMetadataNames = ["filename", "comments", "declaration", "module", "nolib", "sourcemap", "target", "out", "outDir", "noimplicitany", "noresolve", "newline", "newlines", "generatebom"];
function extractCompilerSettings(content: string): CompilerSetting[] {

View File

@@ -25,10 +25,8 @@ interface ProjectRunnerTestCaseResolutionInfo extends ProjectRunnerTestCase {
emittedFiles: string[]; // List of files that wre emitted by the compiler
}
interface BatchCompileProjectTestCaseEmittedFile {
interface BatchCompileProjectTestCaseEmittedFile extends Harness.Compiler.GeneratedFile {
emittedFileName: string;
code: string;
fileName: string;
}
interface BatchCompileProjectTestCaseResult {
@@ -209,7 +207,7 @@ class ProjectRunner extends RunnerBase {
ensureDirectoryStructure(ts.getDirectoryPath(ts.normalizePath(outputFilePath)));
sys.writeFile(outputFilePath, data, writeByteOrderMark);
outputFiles.push({ emittedFileName: filename, code: data, fileName: diskRelativeName });
outputFiles.push({ emittedFileName: filename, code: data, fileName: diskRelativeName, writeByteOrderMark: writeByteOrderMark });
}
function getCurrentDirectory() {

View File

@@ -0,0 +1,11 @@
//// [generateBOM.ts]
// JS and d.ts output should have a BOM but not the sourcemap
var x;
//// [generateBOM.js]
var x;
//# sourceMappingURL=generateBOM.js.map
//// [generateBOM.d.ts]
declare var x: any;

View File

@@ -0,0 +1,2 @@
//// [generateBOM.js.map]
{"version":3,"file":"generateBOM.js","sourceRoot":"","sources":["generateBOM.ts"],"names":[],"mappings":"AAEA,IAAI,CAAC,CAAC"}

View File

@@ -0,0 +1,28 @@
===================================================================
JsFile: generateBOM.js
mapUrl: generateBOM.js.map
sourceRoot:
sources: generateBOM.ts
===================================================================
-------------------------------------------------------------------
emittedFile:tests/cases/compiler/generateBOM.js
sourceFile:generateBOM.ts
-------------------------------------------------------------------
>>>var x;
1 >
2 >^^^^
3 > ^
4 > ^
5 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
>// JS and d.ts output should have a BOM but not the sourcemap
>
2 >var
3 > x
4 > ;
1 >Emitted(1, 1) Source(3, 1) + SourceIndex(0)
2 >Emitted(1, 5) Source(3, 5) + SourceIndex(0)
3 >Emitted(1, 6) Source(3, 6) + SourceIndex(0)
4 >Emitted(1, 7) Source(3, 7) + SourceIndex(0)
---
>>>//# sourceMappingURL=generateBOM.js.map

View File

@@ -0,0 +1,6 @@
//@generateBOM: true
//@declaration: true
//@sourcemap: true
// JS and d.ts output should have a BOM but not the sourcemap
var x;