tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_query.spec.ts (5528B)


      1 export const description = `
      2 Tests for TestQuery
      3 `;
      4 
      5 import { makeTestGroup } from '../common/framework/test_group.js';
      6 import { parseQuery } from '../common/internal/query/parseQuery.js';
      7 import {
      8  TestQueryMultiFile,
      9  TestQueryMultiTest,
     10  TestQueryMultiCase,
     11  TestQuerySingleCase,
     12  TestQuery,
     13 } from '../common/internal/query/query.js';
     14 
     15 import { UnitTest } from './unit_test.js';
     16 
     17 class F extends UnitTest {
     18  expectToString(q: TestQuery, exp: string) {
     19    this.expect(q.toString() === exp);
     20  }
     21 
     22  expectQueriesEqual(q1: TestQuery, q2: TestQuery) {
     23    this.expect(q1.level === q2.level);
     24 
     25    if (q1.level >= 1) {
     26      this.expect(q1.isMultiFile === q2.isMultiFile);
     27      this.expect(q1.suite === q2.suite);
     28      this.expect(q1.filePathParts.length === q2.filePathParts.length);
     29      for (let i = 0; i < q1.filePathParts.length; i++) {
     30        this.expect(q1.filePathParts[i] === q2.filePathParts[i]);
     31      }
     32    }
     33 
     34    if (q1.level >= 2) {
     35      const p1 = q1 as TestQueryMultiTest;
     36      const p2 = q2 as TestQueryMultiTest;
     37 
     38      this.expect(p1.isMultiTest === p2.isMultiTest);
     39      this.expect(p1.testPathParts.length === p2.testPathParts.length);
     40      for (let i = 0; i < p1.testPathParts.length; i++) {
     41        this.expect(p1.testPathParts[i] === p2.testPathParts[i]);
     42      }
     43    }
     44 
     45    if (q1.level >= 3) {
     46      const p1 = q1 as TestQueryMultiCase;
     47      const p2 = q2 as TestQueryMultiCase;
     48 
     49      this.expect(p1.isMultiCase === p2.isMultiCase);
     50      this.expect(Object.keys(p1.params).length === Object.keys(p2.params).length);
     51      for (const key of Object.keys(p1.params)) {
     52        this.expect(key in p2.params);
     53        const v1 = p1.params[key];
     54        const v2 = p2.params[key];
     55        this.expect(
     56          v1 === v2 ||
     57            (typeof v1 === 'number' && isNaN(v1)) === (typeof v2 === 'number' && isNaN(v2))
     58        );
     59        this.expect(Object.is(v1, -0) === Object.is(v2, -0));
     60      }
     61    }
     62  }
     63 
     64  expectQueryParse(s: string, q: TestQuery) {
     65    this.expectQueriesEqual(q, parseQuery(s));
     66  }
     67 }
     68 
     69 export const g = makeTestGroup(F);
     70 
     71 g.test('constructor').fn(t => {
     72  t.shouldThrow('Error', () => new TestQueryMultiTest('suite', [], []));
     73 
     74  t.shouldThrow('Error', () => new TestQueryMultiCase('suite', ['a'], [], {}));
     75  t.shouldThrow('Error', () => new TestQueryMultiCase('suite', [], ['c'], {}));
     76  t.shouldThrow('Error', () => new TestQueryMultiCase('suite', [], [], {}));
     77 
     78  t.shouldThrow('Error', () => new TestQuerySingleCase('suite', ['a'], [], {}));
     79  t.shouldThrow('Error', () => new TestQuerySingleCase('suite', [], ['c'], {}));
     80  t.shouldThrow('Error', () => new TestQuerySingleCase('suite', [], [], {}));
     81 });
     82 
     83 g.test('toString').fn(t => {
     84  t.expectToString(new TestQueryMultiFile('s', []), 's:*');
     85  t.expectToString(new TestQueryMultiFile('s', ['a']), 's:a,*');
     86  t.expectToString(new TestQueryMultiFile('s', ['a', 'b']), 's:a,b,*');
     87  t.expectToString(new TestQueryMultiTest('s', ['a', 'b'], []), 's:a,b:*');
     88  t.expectToString(new TestQueryMultiTest('s', ['a', 'b'], ['c']), 's:a,b:c,*');
     89  t.expectToString(new TestQueryMultiTest('s', ['a', 'b'], ['c', 'd']), 's:a,b:c,d,*');
     90  t.expectToString(new TestQueryMultiCase('s', ['a', 'b'], ['c', 'd'], {}), 's:a,b:c,d:*');
     91  t.expectToString(
     92    new TestQueryMultiCase('s', ['a', 'b'], ['c', 'd'], { x: 1 }),
     93    's:a,b:c,d:x=1;*'
     94  );
     95  t.expectToString(
     96    new TestQueryMultiCase('s', ['a', 'b'], ['c', 'd'], { x: 1, y: 2 }),
     97    's:a,b:c,d:x=1;y=2;*'
     98  );
     99  t.expectToString(
    100    new TestQuerySingleCase('s', ['a', 'b'], ['c', 'd'], { x: 1, y: 2 }),
    101    's:a,b:c,d:x=1;y=2'
    102  );
    103  t.expectToString(new TestQuerySingleCase('s', ['a', 'b'], ['c', 'd'], {}), 's:a,b:c,d:');
    104 
    105  // Test handling of magic param value that convert to NaN/undefined/Infinity/etc.
    106  t.expectToString(new TestQuerySingleCase('s', ['a'], ['b'], { c: NaN }), 's:a:b:c="_nan_"');
    107  t.expectToString(
    108    new TestQuerySingleCase('s', ['a'], ['b'], { c: undefined }),
    109    's:a:b:c="_undef_"'
    110  );
    111  t.expectToString(new TestQuerySingleCase('s', ['a'], ['b'], { c: -0 }), 's:a:b:c="_negzero_"');
    112 });
    113 
    114 g.test('parseQuery').fn(t => {
    115  t.expectQueryParse('s:*', new TestQueryMultiFile('s', []));
    116  t.expectQueryParse('s:a,*', new TestQueryMultiFile('s', ['a']));
    117  t.expectQueryParse('s:a,b,*', new TestQueryMultiFile('s', ['a', 'b']));
    118  t.expectQueryParse('s:a,b:*', new TestQueryMultiTest('s', ['a', 'b'], []));
    119  t.expectQueryParse('s:a,b:c,*', new TestQueryMultiTest('s', ['a', 'b'], ['c']));
    120  t.expectQueryParse('s:a,b:c,d,*', new TestQueryMultiTest('s', ['a', 'b'], ['c', 'd']));
    121  t.expectQueryParse('s:a,b:c,d:*', new TestQueryMultiCase('s', ['a', 'b'], ['c', 'd'], {}));
    122  t.expectQueryParse(
    123    's:a,b:c,d:x=1;*',
    124    new TestQueryMultiCase('s', ['a', 'b'], ['c', 'd'], { x: 1 })
    125  );
    126  t.expectQueryParse(
    127    's:a,b:c,d:x=1;y=2;*',
    128    new TestQueryMultiCase('s', ['a', 'b'], ['c', 'd'], { x: 1, y: 2 })
    129  );
    130  t.expectQueryParse(
    131    's:a,b:c,d:x=1;y=2',
    132    new TestQuerySingleCase('s', ['a', 'b'], ['c', 'd'], { x: 1, y: 2 })
    133  );
    134  t.expectQueryParse('s:a,b:c,d:', new TestQuerySingleCase('s', ['a', 'b'], ['c', 'd'], {}));
    135 
    136  // Test handling of magic param value that convert to NaN/undefined/Infinity/etc.
    137  t.expectQueryParse('s:a:b:c="_nan_"', new TestQuerySingleCase('s', ['a'], ['b'], { c: NaN }));
    138  t.expectQueryParse(
    139    's:a:b:c="_undef_"',
    140    new TestQuerySingleCase('s', ['a'], ['b'], { c: undefined })
    141  );
    142  t.expectQueryParse('s:a:b:c="_negzero_"', new TestQuerySingleCase('s', ['a'], ['b'], { c: -0 }));
    143 });