source: trip-planner-front/node_modules/glob-to-regexp/test.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 10.6 KB
Line 
1var 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 // 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
172test(false)
173// globstar true
174test(true);
175
176// globstar specific tests
177assertMatch("/foo/*", "/foo/bar.txt", {globstar: true });
178assertMatch("/foo/**", "/foo/baz.txt", {globstar: true });
179assertMatch("/foo/**", "/foo/bar/baz.txt", {globstar: true });
180assertMatch("/foo/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
181assertMatch("/foo/**/*.txt", "/foo/bar/baz.txt", {globstar: true });
182assertMatch("/foo/**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
183assertMatch("/foo/**/bar.txt", "/foo/bar.txt", {globstar: true });
184assertMatch("/foo/**/**/bar.txt", "/foo/bar.txt", {globstar: true });
185assertMatch("/foo/**/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
186assertMatch("/foo/**/*.txt", "/foo/bar.txt", {globstar: true });
187assertMatch("/foo/**/**/*.txt", "/foo/bar.txt", {globstar: true });
188assertMatch("/foo/**/*/*.txt", "/foo/bar/baz.txt", {globstar: true });
189assertMatch("**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
190assertMatch("**/foo.txt", "foo.txt", {globstar: true });
191assertMatch("**/*.txt", "foo.txt", {globstar: true });
192
193assertNotMatch("/foo/*", "/foo/bar/baz.txt", {globstar: true });
194assertNotMatch("/foo/*.txt", "/foo/bar/baz.txt", {globstar: true });
195assertNotMatch("/foo/*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
196assertNotMatch("/foo/*/bar.txt", "/foo/bar.txt", {globstar: true });
197assertNotMatch("/foo/*/*/baz.txt", "/foo/bar/baz.txt", {globstar: true });
198assertNotMatch("/foo/**.txt", "/foo/bar/baz/qux.txt", {globstar: true });
199assertNotMatch("/foo/bar**/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
200assertNotMatch("/foo/bar**", "/foo/bar/baz.txt", {globstar: true });
201assertNotMatch("**/.txt", "/foo/bar/baz/qux.txt", {globstar: true });
202assertNotMatch("*/*.txt", "/foo/bar/baz/qux.txt", {globstar: true });
203assertNotMatch("*/*.txt", "foo.txt", {globstar: true });
204
205assertNotMatch("http://foo.com/*",
206 "http://foo.com/bar/baz/jquery.min.js",
207 { extended: true, globstar: true });
208assertNotMatch("http://foo.com/*",
209 "http://foo.com/bar/baz/jquery.min.js",
210 { globstar: true });
211
212assertMatch("http://foo.com/*",
213 "http://foo.com/bar/baz/jquery.min.js",
214 { globstar: false });
215assertMatch("http://foo.com/**",
216 "http://foo.com/bar/baz/jquery.min.js",
217 { globstar: true });
218
219assertMatch("http://foo.com/*/*/jquery.min.js",
220 "http://foo.com/bar/baz/jquery.min.js",
221 { globstar: true });
222assertMatch("http://foo.com/**/jquery.min.js",
223 "http://foo.com/bar/baz/jquery.min.js",
224 { globstar: true });
225assertMatch("http://foo.com/*/*/jquery.min.js",
226 "http://foo.com/bar/baz/jquery.min.js",
227 { globstar: false });
228assertMatch("http://foo.com/*/jquery.min.js",
229 "http://foo.com/bar/baz/jquery.min.js",
230 { globstar: false });
231assertNotMatch("http://foo.com/*/jquery.min.js",
232 "http://foo.com/bar/baz/jquery.min.js",
233 { globstar: true });
234
235console.log("Ok!");
Note: See TracBrowser for help on using the repository browser.