tor-browser

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

query_compare.spec.ts (5481B)


      1 export const description = `
      2 Tests for TestQuery comparison
      3 `;
      4 
      5 import { makeTestGroup } from '../common/framework/test_group.js';
      6 import { compareQueries, Ordering } from '../common/internal/query/compare.js';
      7 import {
      8  TestQuery,
      9  TestQuerySingleCase,
     10  TestQueryMultiFile,
     11  TestQueryMultiTest,
     12  TestQueryMultiCase,
     13 } from '../common/internal/query/query.js';
     14 
     15 import { UnitTest } from './unit_test.js';
     16 
     17 class F extends UnitTest {
     18  expectQ(a: TestQuery, exp: '<' | '=' | '>' | '!', b: TestQuery) {
     19    const [expOrdering, expInvOrdering] =
     20      exp === '<'
     21        ? [Ordering.StrictSubset, Ordering.StrictSuperset]
     22        : exp === '='
     23        ? [Ordering.Equal, Ordering.Equal]
     24        : exp === '>'
     25        ? [Ordering.StrictSuperset, Ordering.StrictSubset]
     26        : [Ordering.Unordered, Ordering.Unordered];
     27    {
     28      const act = compareQueries(a, b);
     29      this.expect(act === expOrdering, `${a} ${b}  got ${act}, exp ${expOrdering}`);
     30    }
     31    {
     32      const act = compareQueries(a, b);
     33      this.expect(act === expOrdering, `${b} ${a}  got ${act}, exp ${expInvOrdering}`);
     34    }
     35  }
     36 
     37  expectWellOrdered(...qs: TestQuery[]) {
     38    for (let i = 0; i < qs.length; ++i) {
     39      this.expectQ(qs[i], '=', qs[i]);
     40      for (let j = i + 1; j < qs.length; ++j) {
     41        this.expectQ(qs[i], '>', qs[j]);
     42      }
     43    }
     44  }
     45 
     46  expectUnordered(...qs: TestQuery[]) {
     47    for (let i = 0; i < qs.length; ++i) {
     48      this.expectQ(qs[i], '=', qs[i]);
     49      for (let j = i + 1; j < qs.length; ++j) {
     50        this.expectQ(qs[i], '!', qs[j]);
     51      }
     52    }
     53  }
     54 }
     55 
     56 export const g = makeTestGroup(F);
     57 
     58 // suite:*  >  suite:a,*  >  suite:a,b,*   >  suite:a,b:*
     59 // suite:a,b:*  >  suite:a,b:c,*  >  suite:a,b:c,d,*  >  suite:a,b:c,d:*
     60 // suite:a,b:c,d:*  >  suite:a,b:c,d:x=1;*  >  suite:a,b:c,d:x=1;y=2;*  >  suite:a,b:c,d:x=1;y=2
     61 // suite:a;* (unordered) suite:b;*
     62 g.test('well_ordered').fn(t => {
     63  t.expectWellOrdered(
     64    new TestQueryMultiFile('suite', []),
     65    new TestQueryMultiFile('suite', ['a']),
     66    new TestQueryMultiFile('suite', ['a', 'b']),
     67    new TestQueryMultiTest('suite', ['a', 'b'], []),
     68    new TestQueryMultiTest('suite', ['a', 'b'], ['c']),
     69    new TestQueryMultiTest('suite', ['a', 'b'], ['c', 'd']),
     70    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], {}),
     71    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], { x: 1 }),
     72    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], { x: 1, y: 2 }),
     73    new TestQuerySingleCase('suite', ['a', 'b'], ['c', 'd'], { x: 1, y: 2 })
     74  );
     75  t.expectWellOrdered(
     76    new TestQueryMultiFile('suite', []),
     77    new TestQueryMultiFile('suite', ['a']),
     78    new TestQueryMultiFile('suite', ['a', 'b']),
     79    new TestQueryMultiTest('suite', ['a', 'b'], []),
     80    new TestQueryMultiTest('suite', ['a', 'b'], ['c']),
     81    new TestQueryMultiTest('suite', ['a', 'b'], ['c', 'd']),
     82    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], {}),
     83    new TestQuerySingleCase('suite', ['a', 'b'], ['c', 'd'], {})
     84  );
     85 });
     86 
     87 g.test('unordered').fn(t => {
     88  t.expectUnordered(
     89    new TestQueryMultiFile('suite', ['a']), //
     90    new TestQueryMultiFile('suite', ['x'])
     91  );
     92  t.expectUnordered(
     93    new TestQueryMultiFile('suite', ['a', 'b']),
     94    new TestQueryMultiFile('suite', ['a', 'x'])
     95  );
     96  t.expectUnordered(
     97    new TestQueryMultiTest('suite', ['a', 'b'], ['c']),
     98    new TestQueryMultiTest('suite', ['a', 'b'], ['x']),
     99    new TestQueryMultiTest('suite', ['a'], []),
    100    new TestQueryMultiTest('suite', ['a', 'x'], [])
    101  );
    102  t.expectUnordered(
    103    new TestQueryMultiTest('suite', ['a', 'b'], ['c', 'd']),
    104    new TestQueryMultiTest('suite', ['a', 'b'], ['c', 'x']),
    105    new TestQueryMultiTest('suite', ['a'], []),
    106    new TestQueryMultiTest('suite', ['a', 'x'], [])
    107  );
    108  t.expectUnordered(
    109    new TestQueryMultiTest('suite', ['a', 'b'], ['c', 'd']),
    110    new TestQueryMultiTest('suite', ['a', 'b'], ['c', 'x']),
    111    new TestQueryMultiTest('suite', ['a'], []),
    112    new TestQueryMultiTest('suite', ['a', 'x'], ['c'])
    113  );
    114  t.expectUnordered(
    115    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], { x: 1 }),
    116    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], { x: 9 }),
    117    new TestQueryMultiCase('suite', ['a', 'b'], ['c'], { x: 9 })
    118  );
    119  t.expectUnordered(
    120    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], { x: 1, y: 2 }),
    121    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], { x: 1, y: 8 }),
    122    new TestQueryMultiCase('suite', ['a', 'b'], ['c'], { x: 1, y: 8 })
    123  );
    124  t.expectUnordered(
    125    new TestQuerySingleCase('suite', ['a', 'b'], ['c', 'd'], { x: 1, y: 2 }),
    126    new TestQuerySingleCase('suite', ['a', 'b'], ['c', 'd'], { x: 1, y: 8 }),
    127    new TestQuerySingleCase('suite', ['a', 'b'], ['c'], { x: 1, y: 8 })
    128  );
    129  t.expectUnordered(
    130    new TestQuerySingleCase('suite1', ['bar', 'buzz', 'buzz'], ['zap'], {}),
    131    new TestQueryMultiTest('suite1', ['bar'], [])
    132  );
    133  // Expect that 0.0 and -0.0 are treated as different queries
    134  t.expectUnordered(
    135    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], { x: 0.0 }),
    136    new TestQueryMultiCase('suite', ['a', 'b'], ['c', 'd'], { x: -0.0 })
    137  );
    138  t.expectUnordered(
    139    new TestQuerySingleCase('suite', ['a', 'b'], ['c', 'd'], { x: 0.0, y: 0.0 }),
    140    new TestQuerySingleCase('suite', ['a', 'b'], ['c', 'd'], { x: 0.0, y: -0.0 }),
    141    new TestQuerySingleCase('suite', ['a', 'b'], ['c', 'd'], { x: -0.0, y: 0.0 }),
    142    new TestQuerySingleCase('suite', ['a', 'b'], ['c', 'd'], { x: -0.0, y: -0.0 })
    143  );
    144 });