1 | var globToRegexp = require("./index.js");
|
---|
2 | var assert = require("assert");
|
---|
3 |
|
---|
4 | function assertMatch(glob, str, opts) {
|
---|
5 | //console.log(glob, globToRegexp(glob, opts));
|
---|
6 | assert.ok(globToRegexp(glob, opts).test(str));
|
---|
7 | }
|
---|
8 |
|
---|
9 | function assertNotMatch(glob, str, opts) {
|
---|
10 | //console.log(glob, globToRegexp(glob, opts));
|
---|
11 | assert.equal(false, globToRegexp(glob, opts).test(str));
|
---|
12 | }
|
---|
13 |
|
---|
14 | function test(globstar) {
|
---|
15 | // Match everything
|
---|
16 | assertMatch("*", "foo");
|
---|
17 | assertMatch("*", "foo", { flags: 'g' });
|
---|
18 |
|
---|
19 | // Match the end
|
---|
20 | assertMatch("f*", "foo");
|
---|
21 | assertMatch("f*", "foo", { flags: 'g' });
|
---|
22 |
|
---|
23 | // Match the start
|
---|
24 | assertMatch("*o", "foo");
|
---|
25 | assertMatch("*o", "foo", { flags: 'g' });
|
---|
26 |
|
---|
27 | // Match the middle
|
---|
28 | assertMatch("f*uck", "firetruck");
|
---|
29 | assertMatch("f*uck", "firetruck", { flags: 'g' });
|
---|
30 |
|
---|
31 | // Don't match without Regexp 'g'
|
---|
32 | assertNotMatch("uc", "firetruck");
|
---|
33 | // Match anywhere with RegExp 'g'
|
---|
34 | assertMatch("uc", "firetruck", { flags: 'g' });
|
---|
35 |
|
---|
36 | // Match zero characters
|
---|
37 | assertMatch("f*uck", "fuck");
|
---|
38 | assertMatch("f*uck", "fuck", { flags: 'g' });
|
---|
39 |
|
---|
40 | // More complex matches
|
---|
41 | assertMatch("*.min.js", "http://example.com/jquery.min.js", {globstar: false});
|
---|
42 | assertMatch("*.min.*", "http://example.com/jquery.min.js", {globstar: false});
|
---|
43 | assertMatch("*/js/*.js", "http://example.com/js/jquery.min.js", {globstar: false});
|
---|
44 |
|
---|
45 | // More complex matches with RegExp 'g' flag (complex regression)
|
---|
46 | assertMatch("*.min.*", "http://example.com/jquery.min.js", { flags: 'g' });
|
---|
47 | assertMatch("*.min.js", "http://example.com/jquery.min.js", { flags: 'g' });
|
---|
48 | assertMatch("*/js/*.js", "http://example.com/js/jquery.min.js", { flags: 'g' });
|
---|
49 |
|
---|
50 | // Test string "\\\\/$^+?.()=!|{},[].*" represents <glob>\\/$^+?.()=!|{},[].*</glob>
|
---|
51 | // The equivalent regex is: /^\\\/\$\^\+\?\.\(\)\=\!\|\{\}\,\[\]\..*$/
|
---|
52 | // Both glob and regex match: \/$^+?.()=!|{},[].*
|
---|
53 | var testStr = "\\\\/$^+?.()=!|{},[].*";
|
---|
54 | var targetStr = "\\/$^+?.()=!|{},[].*";
|
---|
55 | assertMatch(testStr, targetStr);
|
---|
56 | assertMatch(testStr, targetStr, { flags: 'g' });
|
---|
57 |
|
---|
58 | // Equivalent matches without/with using RegExp 'g'
|
---|
59 | assertNotMatch(".min.", "http://example.com/jquery.min.js");
|
---|
60 | assertMatch("*.min.*", "http://example.com/jquery.min.js");
|
---|
61 | assertMatch(".min.", "http://example.com/jquery.min.js", { flags: 'g' });
|
---|
62 |
|
---|
63 | assertNotMatch("http:", "http://example.com/jquery.min.js");
|
---|
64 | assertMatch("http:*", "http://example.com/jquery.min.js");
|
---|
65 | assertMatch("http:", "http://example.com/jquery.min.js", { flags: 'g' });
|
---|
66 |
|
---|
67 | assertNotMatch("min.js", "http://example.com/jquery.min.js");
|
---|
68 | assertMatch("*.min.js", "http://example.com/jquery.min.js");
|
---|
69 | assertMatch("min.js", "http://example.com/jquery.min.js", { flags: 'g' });
|
---|
70 |
|
---|
71 | // Match anywhere (globally) using RegExp 'g'
|
---|
72 | assertMatch("min", "http://example.com/jquery.min.js", { flags: 'g' });
|
---|
73 | assertMatch("/js/", "http://example.com/js/jquery.min.js", { flags: 'g' });
|
---|
74 |
|
---|
75 | assertNotMatch("/js*jq*.js", "http://example.com/js/jquery.min.js");
|
---|
76 | assertMatch("/js*jq*.js", "http://example.com/js/jquery.min.js", { flags: 'g' });
|
---|
77 |
|
---|
78 | // Extended mode
|
---|
79 |
|
---|
80 | // ?: Match one character, no more and no less
|
---|
81 | assertMatch("f?o", "foo", { extended: true });
|
---|
82 | assertNotMatch("f?o", "fooo", { extended: true });
|
---|
83 | assertNotMatch("f?oo", "foo", { extended: true });
|
---|
84 |
|
---|
85 | // ?: Match one character with RegExp 'g'
|
---|
86 | assertMatch("f?o", "foo", { extended: true, globstar: globstar, flags: 'g' });
|
---|
87 | assertMatch("f?o", "fooo", { extended: true, globstar: globstar, flags: 'g' });
|
---|
88 | assertMatch("f?o?", "fooo", { extended: true, globstar: globstar, flags: 'g' });
|
---|
89 | assertNotMatch("?fo", "fooo", { extended: true, globstar: globstar, flags: 'g' });
|
---|
90 | assertNotMatch("f?oo", "foo", { extended: true, globstar: globstar, flags: 'g' });
|
---|
91 | assertNotMatch("foo?", "foo", { extended: true, globstar: globstar, flags: 'g' });
|
---|
92 |
|
---|
93 | // []: Match a character range
|
---|
94 | assertMatch("fo[oz]", "foo", { extended: true });
|
---|
95 | assertMatch("fo[oz]", "foz", { extended: true });
|
---|
96 | assertNotMatch("fo[oz]", "fog", { extended: true });
|
---|
97 |
|
---|
98 | // []: Match a character range and RegExp 'g' (regresion)
|
---|
99 | assertMatch("fo[oz]", "foo", { extended: true, globstar: globstar, flags: 'g' });
|
---|
100 | assertMatch("fo[oz]", "foz", { extended: true, globstar: globstar, flags: 'g' });
|
---|
101 | assertNotMatch("fo[oz]", "fog", { extended: true, globstar: globstar, flags: 'g' });
|
---|
102 |
|
---|
103 | // {}: Match a choice of different substrings
|
---|
104 | assertMatch("foo{bar,baaz}", "foobaaz", { extended: true });
|
---|
105 | assertMatch("foo{bar,baaz}", "foobar", { extended: true });
|
---|
106 | assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true });
|
---|
107 | assertMatch("foo{bar,b*z}", "foobuzz", { extended: true });
|
---|
108 |
|
---|
109 | // {}: Match a choice of different substrings and RegExp 'g' (regression)
|
---|
110 | assertMatch("foo{bar,baaz}", "foobaaz", { extended: true, globstar: globstar, flags: 'g' });
|
---|
111 | assertMatch("foo{bar,baaz}", "foobar", { extended: true, globstar: globstar, flags: 'g' });
|
---|
112 | assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' });
|
---|
113 | assertMatch("foo{bar,b*z}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' });
|
---|
114 |
|
---|
115 | // More complex extended matches
|
---|
116 | assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
117 | "http://foo.baaz.com/jquery.min.js",
|
---|
118 | { extended: true });
|
---|
119 | assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
120 | "http://moz.buzz.com/index.html",
|
---|
121 | { extended: true });
|
---|
122 | assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
123 | "http://moz.buzz.com/index.htm",
|
---|
124 | { extended: true });
|
---|
125 | assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
126 | "http://moz.bar.com/index.html",
|
---|
127 | { extended: true });
|
---|
128 | assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
129 | "http://flozz.buzz.com/index.html",
|
---|
130 | { extended: true });
|
---|
131 |
|
---|
132 | // More complex extended matches and RegExp 'g' (regresion)
|
---|
133 | assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
134 | "http://foo.baaz.com/jquery.min.js",
|
---|
135 | { extended: true, globstar: globstar, flags: 'g' });
|
---|
136 | assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
137 | "http://moz.buzz.com/index.html",
|
---|
138 | { extended: true, globstar: globstar, flags: 'g' });
|
---|
139 | assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
140 | "http://moz.buzz.com/index.htm",
|
---|
141 | { extended: true, globstar: globstar, flags: 'g' });
|
---|
142 | assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
143 | "http://moz.bar.com/index.html",
|
---|
144 | { extended: true, globstar: globstar, flags: 'g' });
|
---|
145 | assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
|
---|
146 | "http://flozz.buzz.com/index.html",
|
---|
147 | { extended: true, globstar: globstar, flags: 'g' });
|
---|
148 |
|
---|
149 | // globstar
|
---|
150 | assertMatch("http://foo.com/**/{*.js,*.html}",
|
---|
151 | "http://foo.com/bar/jquery.min.js",
|
---|
152 | { extended: true, globstar: globstar, flags: 'g' });
|
---|
153 | assertMatch("http://foo.com/**/{*.js,*.html}",
|
---|
154 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
155 | { extended: true, globstar: globstar, flags: 'g' });
|
---|
156 | assertMatch("http://foo.com/**",
|
---|
157 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
158 | { extended: true, globstar: globstar, flags: 'g' });
|
---|
159 |
|
---|
160 | // Remaining special chars should still match themselves
|
---|
161 | // Test string "\\\\/$^+.()=!|,.*" represents <glob>\\/$^+.()=!|,.*</glob>
|
---|
162 | // The equivalent regex is: /^\\\/\$\^\+\.\(\)\=\!\|\,\..*$/
|
---|
163 | // Both glob and regex match: \/$^+.()=!|,.*
|
---|
164 | var testExtStr = "\\\\/$^+.()=!|,.*";
|
---|
165 | var targetExtStr = "\\/$^+.()=!|,.*";
|
---|
166 | assertMatch(testExtStr, targetExtStr, { extended: true });
|
---|
167 | assertMatch(testExtStr, targetExtStr, { extended: true, globstar: globstar, flags: 'g' });
|
---|
168 | }
|
---|
169 |
|
---|
170 | // regression
|
---|
171 | // globstar false
|
---|
172 | test(false)
|
---|
173 | // globstar true
|
---|
174 | test(true);
|
---|
175 |
|
---|
176 | // globstar specific tests
|
---|
177 | assertMatch("/foo/*", "/foo/bar.txt", {globstar: true });
|
---|
178 | assertMatch("/foo/**", "/foo/baz.txt", {globstar: true });
|
---|
179 | assertMatch("/foo/**", "/foo/bar/baz.txt", {globstar: true });
|
---|
180 | assertMatch("/foo/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
|
---|
181 | assertMatch("/foo/**/*.txt", "/foo/bar/baz.txt", {globstar: true });
|
---|
182 | assertMatch("/foo/**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
|
---|
183 | assertMatch("/foo/**/bar.txt", "/foo/bar.txt", {globstar: true });
|
---|
184 | assertMatch("/foo/**/**/bar.txt", "/foo/bar.txt", {globstar: true });
|
---|
185 | assertMatch("/foo/**/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
|
---|
186 | assertMatch("/foo/**/*.txt", "/foo/bar.txt", {globstar: true });
|
---|
187 | assertMatch("/foo/**/**/*.txt", "/foo/bar.txt", {globstar: true });
|
---|
188 | assertMatch("/foo/**/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
|
---|
189 | assertMatch("**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
|
---|
190 | assertMatch("**/foo.txt", "foo.txt", {globstar: true });
|
---|
191 | assertMatch("**/*.txt", "foo.txt", {globstar: true });
|
---|
192 |
|
---|
193 | assertNotMatch("/foo/*", "/foo/bar/baz.txt", {globstar: true });
|
---|
194 | assertNotMatch("/foo/*.txt", "/foo/bar/baz.txt", {globstar: true });
|
---|
195 | assertNotMatch("/foo/*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
|
---|
196 | assertNotMatch("/foo/*/bar.txt", "/foo/bar.txt", {globstar: true });
|
---|
197 | assertNotMatch("/foo/*/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
|
---|
198 | assertNotMatch("/foo/**.txt", "/foo/bar/baz/qux.txt", {globstar: true });
|
---|
199 | assertNotMatch("/foo/bar**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
|
---|
200 | assertNotMatch("/foo/bar**", "/foo/bar/baz.txt", {globstar: true });
|
---|
201 | assertNotMatch("**/.txt", "/foo/bar/baz/qux.txt", {globstar: true });
|
---|
202 | assertNotMatch("*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
|
---|
203 | assertNotMatch("*/*.txt", "foo.txt", {globstar: true });
|
---|
204 |
|
---|
205 | assertNotMatch("http://foo.com/*",
|
---|
206 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
207 | { extended: true, globstar: true });
|
---|
208 | assertNotMatch("http://foo.com/*",
|
---|
209 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
210 | { globstar: true });
|
---|
211 |
|
---|
212 | assertMatch("http://foo.com/*",
|
---|
213 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
214 | { globstar: false });
|
---|
215 | assertMatch("http://foo.com/**",
|
---|
216 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
217 | { globstar: true });
|
---|
218 |
|
---|
219 | assertMatch("http://foo.com/*/*/jquery.min.js",
|
---|
220 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
221 | { globstar: true });
|
---|
222 | assertMatch("http://foo.com/**/jquery.min.js",
|
---|
223 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
224 | { globstar: true });
|
---|
225 | assertMatch("http://foo.com/*/*/jquery.min.js",
|
---|
226 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
227 | { globstar: false });
|
---|
228 | assertMatch("http://foo.com/*/jquery.min.js",
|
---|
229 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
230 | { globstar: false });
|
---|
231 | assertNotMatch("http://foo.com/*/jquery.min.js",
|
---|
232 | "http://foo.com/bar/baz/jquery.min.js",
|
---|
233 | { globstar: true });
|
---|
234 |
|
---|
235 | console.log("Ok!");
|
---|