8000 Merge branch 'feature/test-pattern-counts' into develop · wimpyprogrammer/strings-to-regex@9943332 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9943332

Browse files
Merge branch 'feature/test-pattern-counts' into develop
2 parents b7dff28 + 59fc125 commit 9943332

File tree

3 files changed

+124
-13
lines changed

3 files changed

+124
-13
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
"jest-when": "^2.7.0",
5858
"prettier": "^1.13.7",
5959
"pretty-quick": "^1.6.0",
60+
"regenerator-runtime": "^0.13.3",
61+
"regex-to-strings": "^1.0.0",
6062
"rimraf": "^2.6.2",
6163
"ts-jest": "^24.1.0",
6264
"ts-loader": "^6.1.0",

src/utils/pattern.spec.ts

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,115 @@
1+
import 'regenerator-runtime/runtime';
2+
3+
import { expandAll } from 'regex-to-strings';
14
import { CharTrie } from '../types/charTrie';
25
import { build } from './pattern';
36

47
describe('build', () => {
58
it('returns empty pattern for empty trie', () => {
69
const trie = CharTrie.create({});
7-
expect(build(trie)).toBe('');
10+
11+
const pattern = build(trie);
12+
expect(pattern).toBe('');
13+
14+
const expansions = expandAll(pattern);
15+
expect(expansions).toStrictEqual(['']);
816
});
917

1018
it('returns a basic pattern for a single-level tree', () => {
1119
const trie = CharTrie.create({ foo: {} });
12-
expect(build(trie)).toBe('foo');
20+
21+
const pattern = build(trie);
22+
expect(pattern).toBe('foo');
23+
24+
const expansions = expandAll(pattern);
25+
expect(expansions).toStrictEqual(['foo']);
1326
});
1427

1528
it('returns an alternation group for a branching trie', () => {
1629
const trie = CharTrie.create({ ba: { r: {}, z: {} } });
17-
expect(build(trie)).toBe('ba(r|z)');
30+
31+
const pattern = build(trie);
32+
expect(pattern).toBe('ba(r|z)');
33+
34+
const expansions = expandAll(pattern);
35+
expect(expansions).toHaveLength(2);
36+
expect(expansions).toEqual(expect.arrayContaining(['bar', 'baz']));
1837
});
1938

2039
it('returns an alternation group with an empty option for subset words', () => {
2140
const trie = CharTrie.create({ ba: { '': {}, z: {} } });
22-
expect(build(trie)).toBe('ba(|z)');
41+
42+
const pattern = build(trie);
43+
expect(pattern).toBe('ba(|z)');
44+
45+
const expansions = expandAll(pattern);
46+
expect(expansions).toHaveLength(2);
47+
expect(expansions).toEqual(expect.arrayContaining(['ba', 'baz']));
2348
});
2449

2550
it('returns nested alternation groups for deep branches', () => {
2651
const trie = CharTrie.create({ foo: { '': {}, ba: { r: {}, z: {} } } });
27-
expect(build(trie)).toBe('foo(|ba(r|z))');
52+
53+
const pattern = build(trie);
54+
expect(pattern).toBe('foo(|ba(r|z))');
55+
56+
const expansions = expandAll(pattern);
57+
expect(expansions).toHaveLength(3);
58+
expect(expansions).toEqual(
59+
expect.arrayContaining(['foo', 'foobar', 'foobaz'])
60+
);
2861
});
2962

3063
it('returns a wrapping alternation group for top-level branches', () => {
3164
const trie = CharTrie.create({ foo: {}, bar: {} });
32-
expect(build(trie)).toBe('(foo|bar)');
65+
66+
const pattern = build(trie);
67+
expect(pattern).toBe('(foo|bar)');
68+
69+
const expansions = expandAll(pattern);
70+
expect(expansions).toHaveLength(2);
71+
expect(expansions).toEqual(expect.arrayContaining(['foo', 'bar']));
3372
});
3473

3574
it('preserves leading whitespace', () => {
3675
const trie = CharTrie.create({ ' foo': {} });
37-
expect(build(trie)).toBe(' foo');
76+
77+
const pattern = build(trie);
78+
expect(pattern).toBe(' foo');
79+
80+
const expansions = expandAll(pattern);
81+
expect(expansions).toStrictEqual([' foo']);
3882
});
3983

4084
it('preserves trailing whitespace', () => {
4185
const trie = CharTrie.create({ 'foo ': {} });
42-
expect(build(trie)).toBe('foo ');
86+
87+
const pattern = build(trie);
88+
expect(pattern).toBe('foo ');
89+
90+
const expansions = expandAll(pattern);
91+
expect(expansions).toStrictEqual(['foo ']);
4392
});
4493

4594
it('preserves mid-word whitespace', () => {
4695
const trie = CharTrie.create({ 'foo bar': {} });
47-
expect(build(trie)).toBe('foo bar');
96+
97+
const pattern = build(trie);
98+
expect(pattern).toBe('foo bar');
99+
100+
const expansions = expandAll(pattern);
101+
expect(expansions).toStrictEqual(['foo bar']);
48102
});
49103

50104
it('escapes characters with special meaning in RegEx', () => {
51105
const specialChars = String.raw`foo.?+|(){}^$\[]bar`;
52106
const trie = CharTrie.create({ [specialChars]: {} });
53107

54-
const pattern = String.raw`foo\.\?\+\|\(\)\{\}\^\$\\\[\]bar`;
55-
expect(build(trie)).toBe(pattern);
108+
const pattern = build(trie);
109+
expect(pattern).toBe(String.raw`foo\.\?\+\|\(\)\{\}\^\$\\\[\]bar`);
110+
111+
const expansions = expandAll(pattern);
112+
expect(expansions).toStrictEqual([specialChars]);
56113
});
57114

58115
it('processes complex trie', () => {
@@ -72,7 +129,7 @@ describe('build', () => {
72129
},
73130
O: { hio: {}, klahoma: {}, regon: {} },
74131
});
75-
const pattern = [
132+
const expectedPattern = [
76133
'(',
77134
'M(a(ine|ryland|ssachusetts)|i(chigan|nnesota|ss(issippi|ouri))|ontana)',
78135
'|',
@@ -81,6 +138,34 @@ describe('build', () => {
81138
'O(hio|klahoma|regon)',
82139
')',
83140
].join('');
84-
expect(build(trie)).toBe(pattern);
141+
142+
const pattern = build(trie);
143+
expect(pattern).toBe(expectedPattern);
144+
145+
const expansions = expandAll(pattern);
146+
expect(expansions).toHaveLength(19);
147+
expect(expansions).toEqual(
148+
expect.arrayContaining([
149+
'Maine',
150+
'Maryland',
151+
'Massachusetts',
152+
'Michigan',
153+
'Minnesota',
154+
'Mississippi',
155+
'Missouri',
156+
'Montana',
157+
'Nebraska',
158+
'Nevada',
159+
'New Hampshire',
160+
'New Jersey',
161+
'New Mexico',
162+
'New York',
163+
'North Carolina',
164+
'North Dakota',
165+
'Ohio',
166+
'Oklahoma',
167+
'Oregon',
168+
])
169+
);
85170
});
86171
});

yarn.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5152,6 +5152,11 @@ pako@~1.0.5:
51525152
resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.10.tgz#4328badb5086a426aa90f541977d4955da5c9732"
51535153
integrity sha512-0DTvPVU3ed8+HNXOu5Bs+o//Mbdj9VNQMUOe9oKCwh8l0GNwpTDMKCWbRjgtD291AWnkAgkqA/LOnQS8AmS1tw==
51545154

5155+
pandemonium@^1.4.1:
5156+
version "1.4.1"
5157+
resolved "https://registry.yarnpkg.com/pandemonium/-/pandemonium-1.4.1.tgz#61f4fb0df843a9282b5f7bce7a467ed05857961d"
5158+
integrity sha512-KhwY9xv8tZGQE8L7FfzaTHrLH+JnarUsDlsa8mqfisjtU3J00P362IL52Ei/EhDp025yBDzPuES/zMdWvvAR5g==
5159+
51555160
parallel-transform@^1.1.0:
51565161
version "1.2.0"
51575162
resolved "https://registry.yarnpkg.com/parallel-transform/-/parallel-transform-1.2.0.tgz#9049ca37d6cb2182c3b1d2c720be94d14a5814fc"
@@ -5611,6 +5616,11 @@ regenerate@^1.4.0:
56115616
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11"
56125617
integrity sha512-1G6jJVDWrt0rK99kBjvEtziZNCICAuvIPkSiUFIQxVP06RCVpq3dmDo2oi6ABpYaDYaTRr67BEhL8r1wgEZZKg==
56135618

5619+
regenerator-runtime@^0.13.3:
5620+
version "0.13.3"
5621+
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz#7cf6a77d8f5c6f60eb73c5fc1955b2ceb01e6bf5"
5622+
integrity sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==
5623+
56145624
regenerator-transform@^0.14.0:
56155625
version "0.14.1"
56165626
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.14.1.tgz#3b2fce4e1ab7732c08f665dfdb314749c7ddd2fb"
@@ -5626,6 +5636,20 @@ regex-not@^1.0.0, regex-not@^1.0.2:
56265636
extend-shallow "^3.0.2"
56275637
safe-regex "^1.1.0"
56285638

5639+
regex-to-strings@^1.0.0:
5640+
version "1.0.0"
5641+
resolved "https://registry.yarnpkg.com/regex-to-strings/-/regex-to-strings-1.0.0.tgz#9c6af78ee1a1a9f36ef9eb77aab63dff79a2f5b2"
5642+
integrity sha512-/UYNT/Jv1bF8ZuJM7fDeM6ivxN4562ATlCOwg4QMMe3xH/paJlDo7tIxs22rehGFinr+KLV5Pig7rHSwIfLzBQ==
5643+
dependencies:
5644+
escape-string-regexp "^1.0.5"
5645+
pandemonium "^1.4.1"
5646+
regexp-tree "0.1.11"
5647+
5648+
regexp-tree@0.1.11:
5649+
version "0.1.11"
5650+
resolved "https://registry.yarnpkg.com/regexp-tree/-/regexp-tree-0.1.11.tgz#c9c7f00fcf722e0a56c7390983a7a63dd6c272f3"
5651+
integrity sha512-7/l/DgapVVDzZobwMCCgMlqiqyLFJ0cduo/j+3BcDJIB+yJdsYCfKuI3l/04NV+H/rfNRdPIDbXNZHM9XvQatg==
5652+
56295653
regexp-tree@^0.1.13:
56305654
version "0.1.13"
56315655
resolved "https://registry.yarnpkg.com/regexp-tree/-/regexp-tree-0.1.13.tgz#5b19ab9377edc68bc3679256840bb29afc158d7f"

0 commit comments

Comments
 (0)
0