/
serializeTrie.test.js
98 lines (87 loc) · 3.89 KB
/
serializeTrie.test.js
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
"use strict";
const chai = require("chai");
const serializeTrie = require("../../lib/tries/serializeTrie");
const expect = chai.expect;
describe("serializeTrie()", () => {
[
[[], ""],
// Identical lines are ignored
[["a.a", "a.a", "a.a"], "a>a"],
// Down separators are applied
[["a", "a.a", "a.a.a", "a.a.a.a"], "a>a>a>a"],
// The occurence order has no impact on the result
[["a.a.a.a", "a.a.a", "a.a", "a"], "a>a>a>a"],
// Same level separators are applied
[["a.a", "b.a", "c.a"], "a>a,b,c"],
// Up separators are applied
[["a", "a.a", "a.a.a", "a.b.a", "a.c.a"], "a>a>a<b>a<c>a"],
// Reset separators are applied
[["a.a.a.a", "b.b.b", "c.c"], "a>a>a>a|b>b>b|c>c"],
// Mixed cases
[["a.a.a.a", "b.a", "c.a", "a.a.b", "a.a.b.b", "b.a.b.b"], "a>a>a>a<<b,c|b>a>a<b>a>a,b"],
// Real-world use cases
[["uk", "ac.uk", "co.uk"], "uk>ac,co"],
[["pl", "gov.pl", "ap.gov.pl"], "pl>gov>ap"],
[["pl", "gov.pl", "ap.gov.pl", "net.pl"], "pl>gov>ap<net"],
[["pl", "gov.pl", "ap.gov.pl", "uk", "ac.uk", "co.uk"], "pl>gov>ap|uk>ac,co"],
// Meaningful characters like the wildcard and the negation are not omitted
[["*.ck", "!www.ck"], "ck>!www,*"],
// This example is from the private domains list.
// They are without the company domain (.ua in this case)
[["cc.ua", "inf.ua", "ltd.ua"], "ua>cc,inf,ltd"],
].forEach(args => {
const parsedList = args[0];
const expectedString = args[1];
it(`maps ${JSON.stringify(parsedList)} on ${JSON.stringify(expectedString)}`, () => {
expect(serializeTrie(parsedList)).to.equal(expectedString);
});
});
describe(`type ${serializeTrie.TYPE_LIGHT}`, () => {
const type = serializeTrie.TYPE_LIGHT;
[
// All tlds with just one element are not included in the list
[["a"], ""],
[["a", "b", "c"], ""],
// One level tlds are filtered out, two-level tlds not
[["a.a", "b", "c"], "a>a"],
// Real-world use cases
[["com", "de", "uk", "co.uk"], "uk>co"],
[["jp", "岐阜.jp", "静岡.jp", "موقع"], "jp>岐阜,静岡"],
].forEach(args => {
const parsedList = args[0];
const expectedString = args[1];
it(`maps ${JSON.stringify(parsedList)} on ${JSON.stringify(expectedString)}`, () => {
expect(serializeTrie(parsedList, type)).to.equal(expectedString);
});
});
});
describe(`type ${serializeTrie.TYPE_COMPLETE}`, () => {
const type = serializeTrie.TYPE_COMPLETE;
[
// One level tlds are included
[["a"], "a"],
[["a", "b", "c"], "a|b|c"],
[["a.a", "b", "c"], "a>a|b|c"],
// Real-world use cases
[["com", "de", "uk", "co.uk"], "com|de|uk>co"],
[["uk", "ac.uk", "co.uk"], "uk>ac,co"],
[["pl", "gov.pl", "ap.gov.pl"], "pl>gov>ap"],
[["pl", "gov.pl", "ap.gov.pl", "net.pl"], "pl>gov>ap<net"],
[["pl", "gov.pl", "ap.gov.pl", "uk", "ac.uk", "co.uk"], "pl>gov>ap|uk>ac,co"],
[["jp", "岐阜.jp", "静岡.jp", "موقع"], "jp>岐阜,静岡|موقع"],
].forEach(args => {
const parsedList = args[0];
const expectedString = args[1];
it(`maps ${JSON.stringify(parsedList)} on ${JSON.stringify(expectedString)}`, () => {
expect(serializeTrie(parsedList, type)).to.equal(expectedString);
});
});
});
describe("wrong usage", () => {
expect(() => {
serializeTrie([], "unsupported type");
}).to.throw(
'Cannot serialize trie: Unknown trie type "unsupported type". Expected type to be one of "complete", "light"'
);
});
});