1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
//// [duplicateLocalVariable1.ts]
/ /@module: commonjs
//import FileManager = require('filemanager');
//import App = require('app');
declare var FileManager: any;
declare var App: any;
var TestFileDir = ".\\TempTestFiles";
export class TestCase {
constructor (public name: string, public test: ()=>boolean, public errorMessageRegEx?: string) {
}
}
export class TestRunner {
private tests: TestCase[] = [];
static arrayCompare(arg1: any[], arg2: any[]): boolean {
return (arg1.every(function (val, index) { return val === arg2[index] }));
}
public addTest(test: TestCase) {
this.tests.push(test);
}
public run() {
var success = true;
for (var test in this.tests) {
var exception = false;
var testcase = <TestCase>this.tests[test]
var testResult: boolean = false;
try {
testResult = testcase.test();
}
catch (e) {
exception = true;
testResult = false;
if (typeof testcase.errorMessageRegEx === "string") {
if (testcase.errorMessageRegEx === "") { // Any error is fine
testResult = true;
} else if (e.message) {
var regex = new RegExp(testcase.errorMessageRegEx);
testResult = regex.test(e.message);
}
}
if (testResult === false) {
//console.log(e.message);
}
}
if ((testcase.errorMessageRegEx !== undefined) && !exception) {
success = false;
} else if (!testResult) {
success = false;
}
}
if (success) {
} else {
}
}
}
export var tests: TestRunner = (function () {
var testRunner = new TestRunner();
// First 3 are for simple harness validation
testRunner.addTest(new TestCase("Basic test", function () { return true; }));
testRunner.addTest(new TestCase("Test for any error", function () { throw new Error(); return false; }, ""));
testRunner.addTest(new TestCase("Test RegEx error message match", function () { throw new Error("Should also pass"); return false; }, "Should [also]+ pass"));
testRunner.addTest(new TestCase("Test array compare true", function () { return TestRunner.arrayCompare([1, 2, 3], [1, 2, 3]); }));
testRunner.addTest(new TestCase("Test array compare false", function () { return !TestRunner.arrayCompare([3, 2, 3], [1, 2, 3]); }));
// File detection tests
testRunner.addTest(new TestCase("Check file exists",
function () {
return FileManager.DirectoryManager.fileExists(TestFileDir + "\\Test.txt");
}));
testRunner.addTest(new TestCase("Check file doesn't exist",
function () {
return !FileManager.DirectoryManager.fileExists(TestFileDir + "\\Test2.txt");
}));
// File pattern matching tests
testRunner.addTest(new TestCase("Check text file match",
function () {
return (FileManager.FileBuffer.isTextFile("C:\\somedir\\readme.txt") &&
FileManager.FileBuffer.isTextFile("C:\\spaces path\\myapp.str") &&
FileManager.FileBuffer.isTextFile("C:\\somedir\\code.js"))
}));
testRunner.addTest(new TestCase("Check makefile match",
function () {
return FileManager.FileBuffer.isTextFile("C:\\some dir\\makefile");
}));
testRunner.addTest(new TestCase("Check binary file doesn't match",
function () {
return (!FileManager.FileBuffer.isTextFile("C:\\somedir\\app.exe") &&
!FileManager.FileBuffer.isTextFile("C:\\somedir\\my lib.dll"));
}));
// Command-line parameter tests
testRunner.addTest(new TestCase("Check App defaults",
function () {
var app = new App.App([]);
return (app.fixLines === false &&
app.recurse === true &&
app.lineEndings === "CRLF" &&
app.matchPattern === undefined &&
app.rootDirectory === ".\\" &&
app.encodings[0] === "ascii" &&
app.encodings[1] === "utf8nobom");
}));
testRunner.addTest(new TestCase("Check App params",
function () {
var app = new App.App(["-dir=C:\\test dir", "-lineEndings=LF", "-encodings=utf16be,ascii", "-recurse=false", "-fixlines"]);
return (app.fixLines === true &&
app.lineEndings === "LF" &&
app.recurse === false &&
app.matchPattern === undefined &&
app.rootDirectory === "C:\\test dir" &&
app.encodings[0] === "utf16be" &&
app.encodings[1] === "ascii" &&
app.encodings.length === 2);
}));
// File BOM detection tests
testRunner.addTest(new TestCase("Check encoding detection no BOM",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\noBOM.txt");
return fb.bom === 'none' && fb.encoding === 'utf8';
}));
testRunner.addTest(new TestCase("Check encoding detection UTF8 BOM",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
return fb.bom === 'utf8' && fb.encoding === 'utf8';
}));
testRunner.addTest(new TestCase("Check encoding detection UTF16be BOM",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF16BE.txt");
return fb.bom === 'utf16be' && fb.encoding === 'utf16be';
}));
testRunner.addTest(new TestCase("Check encoding detection UTF16le BOM",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF16LE.txt");
return fb.bom === 'utf16le' && fb.encoding === 'utf16le';
}));
testRunner.addTest(new TestCase("Check encoding on 1 bytes file",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\1bytefile.txt");
return fb.bom === 'none' && fb.encoding === 'utf8';
}));
testRunner.addTest(new TestCase("Check encoding on 0 bytes file",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\0bytefile.txt");
return fb.bom === 'none' && fb.encoding === 'utf8';
}));
// UTF8 encoding tests
testRunner.addTest(new TestCase("Check byte reader",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
var chars = [];
for (var i = 0; i < 11; i++) {
chars.push(fb.readByte());
}
return TestRunner.arrayCompare(chars, [0x54, 0xC3, 0xA8, 0xE1, 0xB4, 0xA3, 0xE2, 0x80, 0xA0, 0x0D, 0x0A]);
}));
testRunner.addTest(new TestCase("Check UTF8 decoding",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
var chars = [];
for (var i = 0; i < 6; i++) {
chars.push(fb.readUtf8CodePoint());
}
return TestRunner.arrayCompare(chars, [0x0054, 0x00E8, 0x1D23, 0x2020, 0x000D, 0x000A]);
}));
testRunner.addTest(new TestCase("Check UTF8 encoding",
function () {
var fb = new FileManager.FileBuffer(20);
fb.writeUtf8Bom();
var chars = [0x0054, 0x00E8, 0x1D23, 0x2020, 0x000D, 0x000A];
for (var i in chars) {
fb.writeUtf8CodePoint(chars[i]);
}
fb.index = 0;
var bytes = [];
for (var i = 0; i < 14; i++) {
bytes.push(fb.readByte());
}
var expected = [0xEF, 0xBB, 0xBF, 0x54, 0xC3, 0xA8, 0xE1, 0xB4, 0xA3, 0xE2, 0x80, 0xA0, 0x0D, 0x0A];
return TestRunner.arrayCompare(bytes, expected);
}));
// Test reading and writing files
testRunner.addTest(new TestCase("Check saving a file",
function () {
var filename = TestFileDir + "\\tmpUTF16LE.txt";
var fb = new FileManager.FileBuffer(14);
fb.writeUtf16leBom();
var chars = [0x0054, 0x00E8, 0x1D23, 0x2020, 0x000D, 0x000A];
chars.forEach(function (val) { fb.writeUtf16CodePoint(val, false); });
fb.save(filename);
var savedFile = new FileManager.FileBuffer(filename);
if (savedFile.encoding !== 'utf16le') {
throw Error("Incorrect encoding");
}
var expectedBytes = [0xFF, 0xFE, 0x54, 0x00, 0xE8, 0x00, 0x23, 0x1D, 0x20, 0x20, 0x0D, 0x00, 0x0A, 0x00]
savedFile.index = 0;
expectedBytes.forEach(function (val) {
var byteVal = savedFile.readByte();
if (byteVal !== val) {
throw Error("Incorrect byte value");
}
});
return true;
}));
testRunner.addTest(new TestCase("Check reading past buffer asserts",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
var result = fb.readByte(200);
return true;
}, "read beyond buffer length"));
testRunner.addTest(new TestCase("Check writing past buffer asserts",
function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
fb.writeByte(5, 200);
return true;
}, "write beyond buffer length"));
// Non-BMP unicode char tests
testRunner.addTest(new TestCase("Read non-BMP utf16 chars",
function () {
var savedFile = new FileManager.FileBuffer(TestFileDir + "\\utf16leNonBmp.txt");
if (savedFile.encoding !== 'utf16le') {
throw Error("Incorrect encoding");
}
var codePoints = [];
for (var i = 0; i < 6; i++) {
codePoints.push(savedFile.readUtf16CodePoint(false));
}
var expectedCodePoints = [0x10480, 0x10481, 0x10482, 0x54, 0x68, 0x69];
return TestRunner.arrayCompare(codePoints, expectedCodePoints);
}));
testRunner.addTest(new TestCase("Read non-BMP utf8 chars",
function () {
var savedFile = new FileManager.FileBuffer(TestFileDir + "\\utf8NonBmp.txt");
if (savedFile.encoding !== 'utf8') {
throw Error("Incorrect encoding");
}
var codePoints = [];
for (var i = 0; i < 6; i++) {
codePoints.push(savedFile.readUtf8CodePoint());
}
var expectedCodePoints = [0x10480, 0x10481, 0x10482, 0x54, 0x68, 0x69];
return TestRunner.arrayCompare(codePoints, expectedCodePoints);
}));
testRunner.addTest(new TestCase("Write non-BMP utf8 chars",
function () {
var filename = TestFileDir + "\\tmpUTF8nonBmp.txt";
var fb = new FileManager.FileBuffer(15);
var chars = [0x10480, 0x10481, 0x10482, 0x54, 0x68, 0x69];
chars.forEach(function (val) { fb.writeUtf8CodePoint(val); });
fb.save(filename);
var savedFile = new FileManager.FileBuffer(filename);
if (savedFile.encoding !== 'utf8') {
throw Error("Incorrect encoding");
}
var expectedBytes = [0xF0, 0x90, 0x92, 0x80, 0xF0, 0x90, 0x92, 0x81, 0xF0, 0x90, 0x92, 0x82, 0x54, 0x68, 0x69];
expectedBytes.forEach(function (val) {
var byteVal = savedFile.readByte();
if (byteVal !== val) {
throw Error("Incorrect byte value");
}
});
return true;
}));
testRunner.addTest(new TestCase("Test invalid lead UTF8 byte",
function () {
var filename = TestFileDir + "\\utf8BadLeadByte.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Invalid UTF8 byte sequence at index: 4"));
testRunner.addTest(new TestCase("Test invalid tail UTF8 byte",
function () {
var filename = TestFileDir + "\\utf8InvalidTail.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Trailing byte invalid at index: 8"));
testRunner.addTest(new TestCase("Test ANSI fails validation",
function () {
var filename = TestFileDir + "\\ansi.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Trailing byte invalid at index: 6"));
testRunner.addTest(new TestCase("Test UTF-16LE with invalid surrogate trail fails",
function () {
var filename = TestFileDir + "\\utf16leInvalidSurrogate.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Trail surrogate has an invalid value"));
testRunner.addTest(new TestCase("Test UTF-16BE with invalid surrogate head fails",
function () {
var filename = TestFileDir + "\\UTF16BEInvalidSurrogate.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Byte sequence starts with a trail surrogate"));
testRunner.addTest(new TestCase("Test UTF-16LE with missing trail surrogate fails",
function () {
var filename = TestFileDir + "\\utf16leMissingTrailSurrogate.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Trail surrogate has an invalid value"));
// Count of CRs & LFs
testRunner.addTest(new TestCase("Count character occurrences",
function () {
var filename = TestFileDir + "\\charCountASCII.txt";
var fb = new FileManager.FileBuffer(filename);
var result = (fb.countCR === 5 && fb.countLF === 4 && fb.countCRLF === 5 && fb.countHT === 3);
return result;
}));
// Control characters in text
testRunner.addTest(new TestCase("Test file with control character",
function () {
var filename = TestFileDir + "\\controlChar.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Codepoint at index: 3 has control value: 8"));
return testRunner;
})();
//// [duplicateLocalVariable1.js]
"use strict";
exports.__esModule = true;
/ /;
commonjs;
var TestFileDir = ".\\TempTestFiles";
var TestCase = /** @class */ (function () {
function TestCase(name, test, errorMessageRegEx) {
this.name = name;
this.test = test;
this.errorMessageRegEx = errorMessageRegEx;
}
return TestCase;
}());
exports.TestCase = TestCase;
var TestRunner = /** @class */ (function () {
function TestRunner() {
this.tests = [];
}
TestRunner.arrayCompare = function (arg1, arg2) {
return (arg1.every(function (val, index) { return val === arg2[index]; }));
};
TestRunner.prototype.addTest = function (test) {
this.tests.push(test);
};
TestRunner.prototype.run = function () {
var success = true;
for (var test in this.tests) {
var exception = false;
var testcase = this.tests[test];
var testResult = false;
try {
testResult = testcase.test();
}
catch (e) {
exception = true;
testResult = false;
if (typeof testcase.errorMessageRegEx === "string") {
if (testcase.errorMessageRegEx === "") { // Any error is fine
testResult = true;
}
else if (e.message) {
var regex = new RegExp(testcase.errorMessageRegEx);
testResult = regex.test(e.message);
}
}
if (testResult === false) {
//console.log(e.message);
}
}
if ((testcase.errorMessageRegEx !== undefined) && !exception) {
success = false;
}
else if (!testResult) {
success = false;
}
}
if (success) {
}
else {
}
};
return TestRunner;
}());
exports.TestRunner = TestRunner;
exports.tests = (function () {
var testRunner = new TestRunner();
// First 3 are for simple harness validation
testRunner.addTest(new TestCase("Basic test", function () { return true; }));
testRunner.addTest(new TestCase("Test for any error", function () { throw new Error(); return false; }, ""));
testRunner.addTest(new TestCase("Test RegEx error message match", function () { throw new Error("Should also pass"); return false; }, "Should [also]+ pass"));
testRunner.addTest(new TestCase("Test array compare true", function () { return TestRunner.arrayCompare([1, 2, 3], [1, 2, 3]); }));
testRunner.addTest(new TestCase("Test array compare false", function () { return !TestRunner.arrayCompare([3, 2, 3], [1, 2, 3]); }));
// File detection tests
testRunner.addTest(new TestCase("Check file exists", function () {
return FileManager.DirectoryManager.fileExists(TestFileDir + "\\Test.txt");
}));
testRunner.addTest(new TestCase("Check file doesn't exist", function () {
return !FileManager.DirectoryManager.fileExists(TestFileDir + "\\Test2.txt");
}));
// File pattern matching tests
testRunner.addTest(new TestCase("Check text file match", function () {
return (FileManager.FileBuffer.isTextFile("C:\\somedir\\readme.txt") &&
FileManager.FileBuffer.isTextFile("C:\\spaces path\\myapp.str") &&
FileManager.FileBuffer.isTextFile("C:\\somedir\\code.js"));
}));
testRunner.addTest(new TestCase("Check makefile match", function () {
return FileManager.FileBuffer.isTextFile("C:\\some dir\\makefile");
}));
testRunner.addTest(new TestCase("Check binary file doesn't match", function () {
return (!FileManager.FileBuffer.isTextFile("C:\\somedir\\app.exe") &&
!FileManager.FileBuffer.isTextFile("C:\\somedir\\my lib.dll"));
}));
// Command-line parameter tests
testRunner.addTest(new TestCase("Check App defaults", function () {
var app = new App.App([]);
return (app.fixLines === false &&
app.recurse === true &&
app.lineEndings === "CRLF" &&
app.matchPattern === undefined &&
app.rootDirectory === ".\\" &&
app.encodings[0] === "ascii" &&
app.encodings[1] === "utf8nobom");
}));
testRunner.addTest(new TestCase("Check App params", function () {
var app = new App.App(["-dir=C:\\test dir", "-lineEndings=LF", "-encodings=utf16be,ascii", "-recurse=false", "-fixlines"]);
return (app.fixLines === true &&
app.lineEndings === "LF" &&
app.recurse === false &&
app.matchPattern === undefined &&
app.rootDirectory === "C:\\test dir" &&
app.encodings[0] === "utf16be" &&
app.encodings[1] === "ascii" &&
app.encodings.length === 2);
}));
// File BOM detection tests
testRunner.addTest(new TestCase("Check encoding detection no BOM", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\noBOM.txt");
return fb.bom === 'none' && fb.encoding === 'utf8';
}));
testRunner.addTest(new TestCase("Check encoding detection UTF8 BOM", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
return fb.bom === 'utf8' && fb.encoding === 'utf8';
}));
testRunner.addTest(new TestCase("Check encoding detection UTF16be BOM", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF16BE.txt");
return fb.bom === 'utf16be' && fb.encoding === 'utf16be';
}));
testRunner.addTest(new TestCase("Check encoding detection UTF16le BOM", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF16LE.txt");
return fb.bom === 'utf16le' && fb.encoding === 'utf16le';
}));
testRunner.addTest(new TestCase("Check encoding on 1 bytes file", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\1bytefile.txt");
return fb.bom === 'none' && fb.encoding === 'utf8';
}));
testRunner.addTest(new TestCase("Check encoding on 0 bytes file", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\0bytefile.txt");
return fb.bom === 'none' && fb.encoding === 'utf8';
}));
// UTF8 encoding tests
testRunner.addTest(new TestCase("Check byte reader", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
var chars = [];
for (var i = 0; i < 11; i++) {
chars.push(fb.readByte());
}
return TestRunner.arrayCompare(chars, [0x54, 0xC3, 0xA8, 0xE1, 0xB4, 0xA3, 0xE2, 0x80, 0xA0, 0x0D, 0x0A]);
}));
testRunner.addTest(new TestCase("Check UTF8 decoding", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
var chars = [];
for (var i = 0; i < 6; i++) {
chars.push(fb.readUtf8CodePoint());
}
return TestRunner.arrayCompare(chars, [0x0054, 0x00E8, 0x1D23, 0x2020, 0x000D, 0x000A]);
}));
testRunner.addTest(new TestCase("Check UTF8 encoding", function () {
var fb = new FileManager.FileBuffer(20);
fb.writeUtf8Bom();
var chars = [0x0054, 0x00E8, 0x1D23, 0x2020, 0x000D, 0x000A];
for (var i in chars) {
fb.writeUtf8CodePoint(chars[i]);
}
fb.index = 0;
var bytes = [];
for (var i = 0; i < 14; i++) {
bytes.push(fb.readByte());
}
var expected = [0xEF, 0xBB, 0xBF, 0x54, 0xC3, 0xA8, 0xE1, 0xB4, 0xA3, 0xE2, 0x80, 0xA0, 0x0D, 0x0A];
return TestRunner.arrayCompare(bytes, expected);
}));
// Test reading and writing files
testRunner.addTest(new TestCase("Check saving a file", function () {
var filename = TestFileDir + "\\tmpUTF16LE.txt";
var fb = new FileManager.FileBuffer(14);
fb.writeUtf16leBom();
var chars = [0x0054, 0x00E8, 0x1D23, 0x2020, 0x000D, 0x000A];
chars.forEach(function (val) { fb.writeUtf16CodePoint(val, false); });
fb.save(filename);
var savedFile = new FileManager.FileBuffer(filename);
if (savedFile.encoding !== 'utf16le') {
throw Error("Incorrect encoding");
}
var expectedBytes = [0xFF, 0xFE, 0x54, 0x00, 0xE8, 0x00, 0x23, 0x1D, 0x20, 0x20, 0x0D, 0x00, 0x0A, 0x00];
savedFile.index = 0;
expectedBytes.forEach(function (val) {
var byteVal = savedFile.readByte();
if (byteVal !== val) {
throw Error("Incorrect byte value");
}
});
return true;
}));
testRunner.addTest(new TestCase("Check reading past buffer asserts", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
var result = fb.readByte(200);
return true;
}, "read beyond buffer length"));
testRunner.addTest(new TestCase("Check writing past buffer asserts", function () {
var fb = new FileManager.FileBuffer(TestFileDir + "\\UTF8BOM.txt");
fb.writeByte(5, 200);
return true;
}, "write beyond buffer length"));
// Non-BMP unicode char tests
testRunner.addTest(new TestCase("Read non-BMP utf16 chars", function () {
var savedFile = new FileManager.FileBuffer(TestFileDir + "\\utf16leNonBmp.txt");
if (savedFile.encoding !== 'utf16le') {
throw Error("Incorrect encoding");
}
var codePoints = [];
for (var i = 0; i < 6; i++) {
codePoints.push(savedFile.readUtf16CodePoint(false));
}
var expectedCodePoints = [0x10480, 0x10481, 0x10482, 0x54, 0x68, 0x69];
return TestRunner.arrayCompare(codePoints, expectedCodePoints);
}));
testRunner.addTest(new TestCase("Read non-BMP utf8 chars", function () {
var savedFile = new FileManager.FileBuffer(TestFileDir + "\\utf8NonBmp.txt");
if (savedFile.encoding !== 'utf8') {
throw Error("Incorrect encoding");
}
var codePoints = [];
for (var i = 0; i < 6; i++) {
codePoints.push(savedFile.readUtf8CodePoint());
}
var expectedCodePoints = [0x10480, 0x10481, 0x10482, 0x54, 0x68, 0x69];
return TestRunner.arrayCompare(codePoints, expectedCodePoints);
}));
testRunner.addTest(new TestCase("Write non-BMP utf8 chars", function () {
var filename = TestFileDir + "\\tmpUTF8nonBmp.txt";
var fb = new FileManager.FileBuffer(15);
var chars = [0x10480, 0x10481, 0x10482, 0x54, 0x68, 0x69];
chars.forEach(function (val) { fb.writeUtf8CodePoint(val); });
fb.save(filename);
var savedFile = new FileManager.FileBuffer(filename);
if (savedFile.encoding !== 'utf8') {
throw Error("Incorrect encoding");
}
var expectedBytes = [0xF0, 0x90, 0x92, 0x80, 0xF0, 0x90, 0x92, 0x81, 0xF0, 0x90, 0x92, 0x82, 0x54, 0x68, 0x69];
expectedBytes.forEach(function (val) {
var byteVal = savedFile.readByte();
if (byteVal !== val) {
throw Error("Incorrect byte value");
}
});
return true;
}));
testRunner.addTest(new TestCase("Test invalid lead UTF8 byte", function () {
var filename = TestFileDir + "\\utf8BadLeadByte.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Invalid UTF8 byte sequence at index: 4"));
testRunner.addTest(new TestCase("Test invalid tail UTF8 byte", function () {
var filename = TestFileDir + "\\utf8InvalidTail.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Trailing byte invalid at index: 8"));
testRunner.addTest(new TestCase("Test ANSI fails validation", function () {
var filename = TestFileDir + "\\ansi.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Trailing byte invalid at index: 6"));
testRunner.addTest(new TestCase("Test UTF-16LE with invalid surrogate trail fails", function () {
var filename = TestFileDir + "\\utf16leInvalidSurrogate.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Trail surrogate has an invalid value"));
testRunner.addTest(new TestCase("Test UTF-16BE with invalid surrogate head fails", function () {
var filename = TestFileDir + "\\UTF16BEInvalidSurrogate.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Byte sequence starts with a trail surrogate"));
testRunner.addTest(new TestCase("Test UTF-16LE with missing trail surrogate fails", function () {
var filename = TestFileDir + "\\utf16leMissingTrailSurrogate.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Trail surrogate has an invalid value"));
// Count of CRs & LFs
testRunner.addTest(new TestCase("Count character occurrences", function () {
var filename = TestFileDir + "\\charCountASCII.txt";
var fb = new FileManager.FileBuffer(filename);
var result = (fb.countCR === 5 && fb.countLF === 4 && fb.countCRLF === 5 && fb.countHT === 3);
return result;
}));
// Control characters in text
testRunner.addTest(new TestCase("Test file with control character", function () {
var filename = TestFileDir + "\\controlChar.txt";
var fb = new FileManager.FileBuffer(filename);
return true;
}, "Codepoint at index: 3 has control value: 8"));
return testRunner;
})();
|