From c727efd323996593b85836304877e6a20e7fe821 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 19 Jan 2015 14:58:34 -0800 Subject: [PATCH] Fix unicode comparision and counting error for test262 --- src/harness/harness.ts | 12 +++++++++--- tests/webhost/webtsc.ts | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/harness/harness.ts b/src/harness/harness.ts index ecae4ee47a..fbe1124b37 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -22,6 +22,7 @@ declare var require: any; declare var process: any; +declare var Buffer: any; // this will work in the browser via browserify var _chai: typeof chai = require('chai'); @@ -1207,7 +1208,6 @@ module Harness { export function getErrorBaseline(inputFiles: { unitName: string; content: string }[], diagnostics: HarnessDiagnostic[]) { diagnostics.sort(compareDiagnostics); - var outputLines: string[] = []; // Count up all the errors we find so we don't miss any var totalErrorsReported = 0; @@ -1298,8 +1298,13 @@ module Harness { return diagnostic.filename && isLibraryFile(diagnostic.filename); }); + var test262HarnessDiagnostics = ts.countWhere(diagnostics, diagnostic => { + // Count an error generated from tests262-harness folder.This should only apply for test262 + return diagnostic.filename && diagnostic.filename.indexOf("test262-harness") >= 0; + }); + // Verify we didn't miss any errors in total - assert.equal(totalErrorsReported + numLibraryDiagnostics, diagnostics.length, 'total number of errors'); + assert.equal(totalErrorsReported + numLibraryDiagnostics + test262HarnessDiagnostics, diagnostics.length, 'total number of errors'); return minimalDiagnosticsToString(diagnostics) + ts.sys.newLine + ts.sys.newLine + outputLines.join('\r\n'); @@ -1642,7 +1647,8 @@ module Harness { } function writeComparison(expected: string, actual: string, relativeFilename: string, actualFilename: string, descriptionForDescribe: string) { - if (expected != actual) { + var encoded_actual = (new Buffer(actual)).toString('utf8') + if (expected != encoded_actual) { // Overwrite & issue error var errMsg = 'The baseline file ' + relativeFilename + ' has changed'; throw new Error(errMsg); diff --git a/tests/webhost/webtsc.ts b/tests/webhost/webtsc.ts index c88d2e8f51..ad3c06c856 100644 --- a/tests/webhost/webtsc.ts +++ b/tests/webhost/webtsc.ts @@ -4,8 +4,12 @@ module TypeScript.WebTsc { declare var RealActiveXObject: { new (s: string): any }; - function getWScriptSystem(): System { + function getWScriptSystem() { var fso = new RealActiveXObject("Scripting.FileSystemObject"); + + var fileStream = new ActiveXObject("ADODB.Stream"); + fileStream.Type = 2 /*text*/; + var args: string[] = []; for (var i = 0; i < WScript.Arguments.length; i++) { args[i] = WScript.Arguments.Item(i); @@ -19,17 +23,35 @@ module TypeScript.WebTsc { writeErr(s: string): void { WScript.StdErr.Write(s); }, - readFile(fileName: string): string { + readFile(fileName: string, encoding?: string): string { + if (!fso.FileExists(fileName)) { + return undefined; + } + fileStream.Open(); try { - var f = fso.OpenTextFile(fileName, 1); - var s: string = f.ReadAll(); - // TODO: Properly handle byte order marks - if (s.length >= 3 && s.charCodeAt(0) === 0xEF && s.charCodeAt(1) === 0xBB && s.charCodeAt(2) === 0xBF) s = s.slice(3); - f.Close(); + if (encoding) { + fileStream.Charset = encoding; + fileStream.LoadFromFile(fileName); + } + else { + // Load file and read the first two bytes into a string with no interpretation + fileStream.Charset = "x-ansi"; + fileStream.LoadFromFile(fileName); + var bom = fileStream.ReadText(2) || ""; + // Position must be at 0 before encoding can be changed + fileStream.Position = 0; + // [0xFF,0xFE] and [0xFE,0xFF] mean utf-16 (little or big endian), otherwise default to utf-8 + fileStream.Charset = bom.length >= 2 && (bom.charCodeAt(0) === 0xFF && bom.charCodeAt(1) === 0xFE || bom.charCodeAt(0) === 0xFE && bom.charCodeAt(1) === 0xFF) ? "unicode" : "utf-8"; + } + // ReadText method always strips byte order mark from resulting string + return fileStream.ReadText(); } catch (e) { + throw e; + } + finally { + fileStream.Close(); } - return s; }, writeFile(fileName: string, data: string): boolean { var f = fso.CreateTextFile(fileName, true);