Skip to content

Commit c727efd

Browse files
author
Yui T
committed
Fix unicode comparision and counting error for test262
1 parent 778c91e commit c727efd

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/harness/harness.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
declare var require: any;
2424
declare var process: any;
25+
declare var Buffer: any;
2526

2627
// this will work in the browser via browserify
2728
var _chai: typeof chai = require('chai');
@@ -1207,7 +1208,6 @@ module Harness {
12071208

12081209
export function getErrorBaseline(inputFiles: { unitName: string; content: string }[], diagnostics: HarnessDiagnostic[]) {
12091210
diagnostics.sort(compareDiagnostics);
1210-
12111211
var outputLines: string[] = [];
12121212
// Count up all the errors we find so we don't miss any
12131213
var totalErrorsReported = 0;
@@ -1298,8 +1298,13 @@ module Harness {
12981298
return diagnostic.filename && isLibraryFile(diagnostic.filename);
12991299
});
13001300

1301+
var test262HarnessDiagnostics = ts.countWhere(diagnostics, diagnostic => {
1302+
// Count an error generated from tests262-harness folder.This should only apply for test262
1303+
return diagnostic.filename && diagnostic.filename.indexOf("test262-harness") >= 0;
1304+
});
1305+
13011306
// Verify we didn't miss any errors in total
1302-
assert.equal(totalErrorsReported + numLibraryDiagnostics, diagnostics.length, 'total number of errors');
1307+
assert.equal(totalErrorsReported + numLibraryDiagnostics + test262HarnessDiagnostics, diagnostics.length, 'total number of errors');
13031308

13041309
return minimalDiagnosticsToString(diagnostics) +
13051310
ts.sys.newLine + ts.sys.newLine + outputLines.join('\r\n');
@@ -1642,7 +1647,8 @@ module Harness {
16421647
}
16431648

16441649
function writeComparison(expected: string, actual: string, relativeFilename: string, actualFilename: string, descriptionForDescribe: string) {
1645-
if (expected != actual) {
1650+
var encoded_actual = (new Buffer(actual)).toString('utf8')
1651+
if (expected != encoded_actual) {
16461652
// Overwrite & issue error
16471653
var errMsg = 'The baseline file ' + relativeFilename + ' has changed';
16481654
throw new Error(errMsg);

tests/webhost/webtsc.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ module TypeScript.WebTsc {
44

55
declare var RealActiveXObject: { new (s: string): any };
66

7-
function getWScriptSystem(): System {
7+
function getWScriptSystem() {
88
var fso = new RealActiveXObject("Scripting.FileSystemObject");
9+
10+
var fileStream = new ActiveXObject("ADODB.Stream");
11+
fileStream.Type = 2 /*text*/;
12+
913
var args: string[] = [];
1014
for (var i = 0; i < WScript.Arguments.length; i++) {
1115
args[i] = WScript.Arguments.Item(i);
@@ -19,17 +23,35 @@ module TypeScript.WebTsc {
1923
writeErr(s: string): void {
2024
WScript.StdErr.Write(s);
2125
},
22-
readFile(fileName: string): string {
26+
readFile(fileName: string, encoding?: string): string {
27+
if (!fso.FileExists(fileName)) {
28+
return undefined;
29+
}
30+
fileStream.Open();
2331
try {
24-
var f = fso.OpenTextFile(fileName, 1);
25-
var s: string = f.ReadAll();
26-
// TODO: Properly handle byte order marks
27-
if (s.length >= 3 && s.charCodeAt(0) === 0xEF && s.charCodeAt(1) === 0xBB && s.charCodeAt(2) === 0xBF) s = s.slice(3);
28-
f.Close();
32+
if (encoding) {
33+
fileStream.Charset = encoding;
34+
fileStream.LoadFromFile(fileName);
35+
}
36+
else {
37+
// Load file and read the first two bytes into a string with no interpretation
38+
fileStream.Charset = "x-ansi";
39+
fileStream.LoadFromFile(fileName);
40+
var bom = fileStream.ReadText(2) || "";
41+
// Position must be at 0 before encoding can be changed
42+
fileStream.Position = 0;
43+
// [0xFF,0xFE] and [0xFE,0xFF] mean utf-16 (little or big endian), otherwise default to utf-8
44+
fileStream.Charset = bom.length >= 2 && (bom.charCodeAt(0) === 0xFF && bom.charCodeAt(1) === 0xFE || bom.charCodeAt(0) === 0xFE && bom.charCodeAt(1) === 0xFF) ? "unicode" : "utf-8";
45+
}
46+
// ReadText method always strips byte order mark from resulting string
47+
return fileStream.ReadText();
2948
}
3049
catch (e) {
50+
throw e;
51+
}
52+
finally {
53+
fileStream.Close();
3154
}
32-
return s;
3355
},
3456
writeFile(fileName: string, data: string): boolean {
3557
var f = fso.CreateTextFile(fileName, true);

0 commit comments

Comments
 (0)