|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
| 5 | +import {SnapshotTester} from '../../testing/SnapshotTester.js'; |
| 6 | + |
5 | 7 | import * as TextUtils from './text_utils.js'; |
6 | 8 |
|
7 | 9 | type SplitByRegexExpected = Array<[string, number, number]>; |
@@ -449,7 +451,7 @@ describe('TextUtils', () => { |
449 | 451 | }); |
450 | 452 | }); |
451 | 453 |
|
452 | | - describe('BalancedJSONTokenizer', () => { |
| 454 | + describe('BalancedJSONTokenizer', function() { |
453 | 455 | it('can be instantiated successfully', () => { |
454 | 456 | const callback = () => {}; |
455 | 457 | const findMultiple = false; |
@@ -529,6 +531,124 @@ describe('TextUtils', () => { |
529 | 531 | assert.deepEqual(callbackResults, [], 'callback had unexpected results'); |
530 | 532 | assert.strictEqual(tokenizer.remainder(), ']]', 'remainder was incorrect'); |
531 | 533 | }); |
| 534 | + |
| 535 | + const snapshotTester = new SnapshotTester(this, import.meta); |
| 536 | + |
| 537 | + it('matches quotes', function() { |
| 538 | + const testStrings = [ |
| 539 | + {'odd back slashes with text around': 'tes\\"t'}, |
| 540 | + {'escaped double quotes': '"test"'}, |
| 541 | + {'escaped back slash before double quote': 'test\\'}, |
| 542 | + {1: 2}, |
| 543 | + {'': ''}, |
| 544 | + {'nested brackets': {}}, |
| 545 | + {'nested brackets with double quotes': {'': ''}}, |
| 546 | + {etc: {'\\': '"'}}, |
| 547 | + {etc: {'\\\\': '\\'}}, |
| 548 | + {etc: {'\\\\"': '\\\\"'}}, |
| 549 | + ]; |
| 550 | + const results: string[] = []; |
| 551 | + const callback = (str: string) => results.push(str); |
| 552 | + |
| 553 | + for (const testString of testStrings) { |
| 554 | + const jsonString = JSON.stringify(testString); |
| 555 | + results.push('Parsing ' + jsonString); |
| 556 | + const tokenizer = new TextUtils.TextUtils.BalancedJSONTokenizer(callback); |
| 557 | + const result = tokenizer.write(jsonString); |
| 558 | + assert.isTrue(result, `tokenizer.write() returned ${result}, true expected`); |
| 559 | + } |
| 560 | + snapshotTester.assert(this, results.join('\n')); |
| 561 | + }); |
| 562 | + |
| 563 | + it('matches sequence using one shot', function() { |
| 564 | + const testData = [ |
| 565 | + {one: 'one'}, |
| 566 | + [{one: 'one'}, {two: 'two'}], |
| 567 | + [{one: 'one'}, {two: 'two'}, {three: 'three'}], |
| 568 | + ]; |
| 569 | + const results: string[] = []; |
| 570 | + const callback = (str: string) => results.push(str); |
| 571 | + |
| 572 | + for (const data of testData) { |
| 573 | + const jsonString = JSON.stringify(data); |
| 574 | + results.push('Parsing ' + jsonString); |
| 575 | + const tokenizer = new TextUtils.TextUtils.BalancedJSONTokenizer(callback); |
| 576 | + const result = tokenizer.write(jsonString); |
| 577 | + assert.isDefined(result, `tokenizer.write() returned ${result}, true expected`); |
| 578 | + } |
| 579 | + snapshotTester.assert(this, results.join('\n')); |
| 580 | + }); |
| 581 | + |
| 582 | + it('matches sequence using multiple', function() { |
| 583 | + const testData = [ |
| 584 | + {one: 'one'}, |
| 585 | + [{one: 'one'}, {two: 'two'}], |
| 586 | + [{one: 'one'}, {two: 'two'}, {three: 'three'}], |
| 587 | + ]; |
| 588 | + const results: string[] = []; |
| 589 | + const callback = (str: string) => results.push(str); |
| 590 | + |
| 591 | + for (const data of testData) { |
| 592 | + const jsonString = JSON.stringify(data); |
| 593 | + results.push('Parsing ' + jsonString); |
| 594 | + const tokenizer = new TextUtils.TextUtils.BalancedJSONTokenizer(callback, true); |
| 595 | + const result = tokenizer.write(jsonString); |
| 596 | + const expectedResult = !(data instanceof Array); |
| 597 | + assert.strictEqual(result, expectedResult, `tokenizer.write() returned ${result}, ${expectedResult} expected`); |
| 598 | + } |
| 599 | + snapshotTester.assert(this, results.join('\n')); |
| 600 | + }); |
| 601 | + |
| 602 | + it('incremental writes', function() { |
| 603 | + const testStrings = [ |
| 604 | + {'odd back slashes with text around': 'tes\\"t'}, |
| 605 | + {'escaped double quotes': '"test"'}, |
| 606 | + {'escaped back slash before double quote': 'test\\'}, |
| 607 | + {1: 2}, |
| 608 | + {'': ''}, |
| 609 | + {'nested brackets': {}}, |
| 610 | + {'nested brackets with double quotes': {'': ''}}, |
| 611 | + {etc: {'\\': '"'}}, |
| 612 | + {etc: {'\\\\': '\\'}}, |
| 613 | + {etc: {'\\\\"': '\\\\"'}}, |
| 614 | + ]; |
| 615 | + const results: string[] = []; |
| 616 | + const callback = (str: string) => results.push(str); |
| 617 | + const jsonString = JSON.stringify(testStrings); |
| 618 | + let tokenizer = new TextUtils.TextUtils.BalancedJSONTokenizer(callback, true); |
| 619 | + |
| 620 | + results.push('Running at once:'); |
| 621 | + const result = tokenizer.write(jsonString); |
| 622 | + assert.isDefined(result, `tokenizer.write() returned ${result}, false expected`); |
| 623 | + if (result) { |
| 624 | + assert.fail(`tokenizer.write() returned ${result}, false expected`); |
| 625 | + } |
| 626 | + |
| 627 | + for (const sample of [3, 15, 50]) { |
| 628 | + tokenizer = new TextUtils.TextUtils.BalancedJSONTokenizer(callback, true); |
| 629 | + results.push('Running by ' + sample + ':'); |
| 630 | + for (let i = 0; i < jsonString.length; i += sample) { |
| 631 | + const result = tokenizer.write(jsonString.substring(i, i + sample)); |
| 632 | + const expectedResult = (i + sample < jsonString.length); |
| 633 | + assert.strictEqual( |
| 634 | + !!result, expectedResult, `tokenizer.write() returned ${result}, ${expectedResult} expected`); |
| 635 | + } |
| 636 | + } |
| 637 | + snapshotTester.assert(this, results.join('\n')); |
| 638 | + }); |
| 639 | + |
| 640 | + it('garbage after object', function() { |
| 641 | + const testString = '[{a: \'b\'}], {\'x\': {a: \'b\'}}'; |
| 642 | + const results: string[] = []; |
| 643 | + const callback = (str: string) => results.push(str); |
| 644 | + |
| 645 | + results.push('Parsing ' + testString); |
| 646 | + const tokenizer = new TextUtils.TextUtils.BalancedJSONTokenizer(callback, true); |
| 647 | + const result = tokenizer.write(testString); |
| 648 | + assert.isFalse(result, `tokenizer.write() returned ${result}, false expected`); |
| 649 | + |
| 650 | + snapshotTester.assert(this, results.join('\n')); |
| 651 | + }); |
532 | 652 | }); |
533 | 653 |
|
534 | 654 | describe('isMinified', () => { |
|
0 commit comments