blob: 58f1080a6d151ba28e7f766294c9e3113648f718 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001var globToRegexp = require("./index.js");
2var assert = require("assert");
3
4function assertMatch(glob, str, opts) {
5 //console.log(glob, globToRegexp(glob, opts));
6 assert.ok(globToRegexp(glob, opts).test(str));
7}
8
9function assertNotMatch(glob, str, opts) {
10 //console.log(glob, globToRegexp(glob, opts));
11 assert.equal(false, globToRegexp(glob, opts).test(str));
12}
13
14function 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 var testStr = "\\/$^+?.()=!|{},[].*"
51 assertMatch(testStr, testStr);
52 assertMatch(testStr, testStr, { flags: 'g' });
53
54 // Equivalent matches without/with using RegExp 'g'
55 assertNotMatch(".min.", "http://example.com/jquery.min.js");
56 assertMatch("*.min.*", "http://example.com/jquery.min.js");
57 assertMatch(".min.", "http://example.com/jquery.min.js", { flags: 'g' });
58
59 assertNotMatch("http:", "http://example.com/jquery.min.js");
60 assertMatch("http:*", "http://example.com/jquery.min.js");
61 assertMatch("http:", "http://example.com/jquery.min.js", { flags: 'g' });
62
63 assertNotMatch("min.js", "http://example.com/jquery.min.js");
64 assertMatch("*.min.js", "http://example.com/jquery.min.js");
65 assertMatch("min.js", "http://example.com/jquery.min.js", { flags: 'g' });
66
67 // Match anywhere (globally) using RegExp 'g'
68 assertMatch("min", "http://example.com/jquery.min.js", { flags: 'g' });
69 assertMatch("/js/", "http://example.com/js/jquery.min.js", { flags: 'g' });
70
71 assertNotMatch("/js*jq*.js", "http://example.com/js/jquery.min.js");
72 assertMatch("/js*jq*.js", "http://example.com/js/jquery.min.js", { flags: 'g' });
73
74 // Extended mode
75
76 // ?: Match one character, no more and no less
77 assertMatch("f?o", "foo", { extended: true });
78 assertNotMatch("f?o", "fooo", { extended: true });
79 assertNotMatch("f?oo", "foo", { extended: true });
80
81 // ?: Match one character with RegExp 'g'
82 assertMatch("f?o", "foo", { extended: true, globstar: globstar, flags: 'g' });
83 assertMatch("f?o", "fooo", { extended: true, globstar: globstar, flags: 'g' });
84 assertMatch("f?o?", "fooo", { extended: true, globstar: globstar, flags: 'g' });
85 assertNotMatch("?fo", "fooo", { extended: true, globstar: globstar, flags: 'g' });
86 assertNotMatch("f?oo", "foo", { extended: true, globstar: globstar, flags: 'g' });
87 assertNotMatch("foo?", "foo", { extended: true, globstar: globstar, flags: 'g' });
88
89 // []: Match a character range
90 assertMatch("fo[oz]", "foo", { extended: true });
91 assertMatch("fo[oz]", "foz", { extended: true });
92 assertNotMatch("fo[oz]", "fog", { extended: true });
93
94 // []: Match a character range and RegExp 'g' (regresion)
95 assertMatch("fo[oz]", "foo", { extended: true, globstar: globstar, flags: 'g' });
96 assertMatch("fo[oz]", "foz", { extended: true, globstar: globstar, flags: 'g' });
97 assertNotMatch("fo[oz]", "fog", { extended: true, globstar: globstar, flags: 'g' });
98
99 // {}: Match a choice of different substrings
100 assertMatch("foo{bar,baaz}", "foobaaz", { extended: true });
101 assertMatch("foo{bar,baaz}", "foobar", { extended: true });
102 assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true });
103 assertMatch("foo{bar,b*z}", "foobuzz", { extended: true });
104
105 // {}: Match a choice of different substrings and RegExp 'g' (regression)
106 assertMatch("foo{bar,baaz}", "foobaaz", { extended: true, globstar: globstar, flags: 'g' });
107 assertMatch("foo{bar,baaz}", "foobar", { extended: true, globstar: globstar, flags: 'g' });
108 assertNotMatch("foo{bar,baaz}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' });
109 assertMatch("foo{bar,b*z}", "foobuzz", { extended: true, globstar: globstar, flags: 'g' });
110
111 // More complex extended matches
112 assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
113 "http://foo.baaz.com/jquery.min.js",
114 { extended: true });
115 assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
116 "http://moz.buzz.com/index.html",
117 { extended: true });
118 assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
119 "http://moz.buzz.com/index.htm",
120 { extended: true });
121 assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
122 "http://moz.bar.com/index.html",
123 { extended: true });
124 assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
125 "http://flozz.buzz.com/index.html",
126 { extended: true });
127
128 // More complex extended matches and RegExp 'g' (regresion)
129 assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
130 "http://foo.baaz.com/jquery.min.js",
131 { extended: true, globstar: globstar, flags: 'g' });
132 assertMatch("http://?o[oz].b*z.com/{*.js,*.html}",
133 "http://moz.buzz.com/index.html",
134 { extended: true, globstar: globstar, flags: 'g' });
135 assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
136 "http://moz.buzz.com/index.htm",
137 { extended: true, globstar: globstar, flags: 'g' });
138 assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
139 "http://moz.bar.com/index.html",
140 { extended: true, globstar: globstar, flags: 'g' });
141 assertNotMatch("http://?o[oz].b*z.com/{*.js,*.html}",
142 "http://flozz.buzz.com/index.html",
143 { extended: true, globstar: globstar, flags: 'g' });
144
145 // globstar
146 assertMatch("http://foo.com/**/{*.js,*.html}",
147 "http://foo.com/bar/jquery.min.js",
148 { extended: true, globstar: globstar, flags: 'g' });
149 assertMatch("http://foo.com/**/{*.js,*.html}",
150 "http://foo.com/bar/baz/jquery.min.js",
151 { extended: true, globstar: globstar, flags: 'g' });
152 assertMatch("http://foo.com/**",
153 "http://foo.com/bar/baz/jquery.min.js",
154 { extended: true, globstar: globstar, flags: 'g' });
155
156 // Remaining special chars should still match themselves
157 var testExtStr = "\\/$^+.()=!|,.*"
158 assertMatch(testExtStr, testExtStr, { extended: true });
159 assertMatch(testExtStr, testExtStr, { extended: true, globstar: globstar, flags: 'g' });
160}
161
162// regression
163// globstar false
164test(false)
165// globstar true
166test(true);
167
168// globstar specific tests
169assertMatch("/foo/*", "/foo/bar.txt", {globstar: true });
170assertMatch("/foo/**", "/foo/baz.txt", {globstar: true });
171assertMatch("/foo/**", "/foo/bar/baz.txt", {globstar: true });
172assertMatch("/foo/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
173assertMatch("/foo/**/*.txt", "/foo/bar/baz.txt", {globstar: true });
174assertMatch("/foo/**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
175assertMatch("/foo/**/bar.txt", "/foo/bar.txt", {globstar: true });
176assertMatch("/foo/**/**/bar.txt", "/foo/bar.txt", {globstar: true });
177assertMatch("/foo/**/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
178assertMatch("/foo/**/*.txt", "/foo/bar.txt", {globstar: true });
179assertMatch("/foo/**/**/*.txt", "/foo/bar.txt", {globstar: true });
180assertMatch("/foo/**/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
181assertMatch("**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
182assertMatch("**/foo.txt", "foo.txt", {globstar: true });
183assertMatch("**/*.txt", "foo.txt", {globstar: true });
184
185assertNotMatch("/foo/*", "/foo/bar/baz.txt", {globstar: true });
186assertNotMatch("/foo/*.txt", "/foo/bar/baz.txt", {globstar: true });
187assertNotMatch("/foo/*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
188assertNotMatch("/foo/*/bar.txt", "/foo/bar.txt", {globstar: true });
189assertNotMatch("/foo/*/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
190assertNotMatch("/foo/**.txt", "/foo/bar/baz/qux.txt", {globstar: true });
191assertNotMatch("/foo/bar**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
192assertNotMatch("/foo/bar**", "/foo/bar/baz.txt", {globstar: true });
193assertNotMatch("**/.txt", "/foo/bar/baz/qux.txt", {globstar: true });
194assertNotMatch("*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
195assertNotMatch("*/*.txt", "foo.txt", {globstar: true });
196
197assertNotMatch("http://foo.com/*",
198 "http://foo.com/bar/baz/jquery.min.js",
199 { extended: true, globstar: true });
200assertNotMatch("http://foo.com/*",
201 "http://foo.com/bar/baz/jquery.min.js",
202 { globstar: true });
203
204assertMatch("http://foo.com/*",
205 "http://foo.com/bar/baz/jquery.min.js",
206 { globstar: false });
207assertMatch("http://foo.com/**",
208 "http://foo.com/bar/baz/jquery.min.js",
209 { globstar: true });
210
211assertMatch("http://foo.com/*/*/jquery.min.js",
212 "http://foo.com/bar/baz/jquery.min.js",
213 { globstar: true });
214assertMatch("http://foo.com/**/jquery.min.js",
215 "http://foo.com/bar/baz/jquery.min.js",
216 { globstar: true });
217assertMatch("http://foo.com/*/*/jquery.min.js",
218 "http://foo.com/bar/baz/jquery.min.js",
219 { globstar: false });
220assertMatch("http://foo.com/*/jquery.min.js",
221 "http://foo.com/bar/baz/jquery.min.js",
222 { globstar: false });
223assertNotMatch("http://foo.com/*/jquery.min.js",
224 "http://foo.com/bar/baz/jquery.min.js",
225 { globstar: true });
226
227console.log("Ok!");