1
+ import 'regenerator-runtime/runtime' ;
2
+
3
+ import { expandAll } from 'regex-to-strings' ;
1
4
import { CharTrie } from '../types/charTrie' ;
2
5
import { build } from './pattern' ;
3
6
4
7
describe ( 'build' , ( ) => {
5
8
it ( 'returns empty pattern for empty trie' , ( ) => {
6
9
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 ( [ '' ] ) ;
8
16
} ) ;
9
17
10
18
it ( 'returns a basic pattern for a single-level tree' , ( ) => {
11
19
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' ] ) ;
13
26
} ) ;
14
27
15
28
it ( 'returns an alternation group for a branching trie' , ( ) => {
16
29
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' ] ) ) ;
18
37
} ) ;
19
38
20
39
it ( 'returns an alternation group with an empty option for subset words' , ( ) => {
21
40
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' ] ) ) ;
23
48
} ) ;
24
49
25
50
it ( 'returns nested alternation groups for deep branches' , ( ) => {
26
51
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
+ ) ;
28
61
} ) ;
29
62
30
63
it ( 'returns a wrapping alternation group for top-level branches' , ( ) => {
31
64
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' ] ) ) ;
33
72
} ) ;
34
73
35
74
it ( 'preserves leading whitespace' , ( ) => {
36
75
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' ] ) ;
38
82
} ) ;
39
83
40
84
it ( 'preserves trailing whitespace' , ( ) => {
41
85
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 ' ] ) ;
43
92
} ) ;
44
93
45
94
it ( 'preserves mid-word whitespace' , ( ) => {
46
95
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' ] ) ;
48
102
} ) ;
49
103
50
104
it ( 'escapes characters with special meaning in RegEx' , ( ) => {
51
105
const specialChars = String . raw `foo.?+|(){}^$\[]bar` ;
52
106
const trie = CharTrie . create ( { [ specialChars ] : { } } ) ;
53
107
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 ] ) ;
56
113
} ) ;
57
114
58
115
it ( 'processes complex trie' , ( ) => {
@@ -72,7 +129,7 @@ describe('build', () => {
72
129
} ,
73
130
O : { hio : { } , klahoma : { } , regon : { } } ,
74
131
} ) ;
75
- const pattern = [
132
+ const expectedPattern = [
76
133
'(' ,
77
134
'M(a(ine|ryland|ssachusetts)|i(chigan|nnesota|ss(issippi|ouri))|ontana)' ,
78
135
'|' ,
@@ -81,6 +138,34 @@ describe('build', () => {
81
138
'O(hio|klahoma|regon)' ,
82
139
')' ,
83
140
] . 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
+ ) ;
85
170
} ) ;
86
171
} ) ;
0 commit comments