Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(395)

Side by Side Diff: pkg/glob/test/match_test.dart

Issue 506993004: Create a glob package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:io';
6
7 import 'package:glob/glob.dart';
8 import 'package:path/path.dart' as p;
9 import 'package:unittest/unittest.dart';
10
11 const ASCII_WITHOUT_SLASH = "\t\n\r !\"#\$%&'()*+`-.0123456789:;<=>?@ABCDEFGHIJ"
12 "KLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
13
14 void main() {
15 test("literals match exactly", () {
16 expect("foo", contains(new Glob("foo")));
17 expect("foo/bar", contains(new Glob("foo/bar")));
18 expect("foo*", contains(new Glob(r"foo\*")));
19 });
20
21 group("star", () {
22 test("matches non-separator characters", () {
23 var glob = new Glob("*");
24 expect(ASCII_WITHOUT_SLASH, contains(glob));
25 });
26
27 test("matches the empty string", () {
28 expect("foo", contains(new Glob("foo*")));
29 expect("", contains(new Glob("*")));
30 });
31
32 test("doesn't match separators", () {
33 var glob = new Glob("*");
34 expect("foo/bar", isNot(contains(glob)));
35 });
36 });
37
38 group("double star", () {
39 test("matches non-separator characters", () {
40 var glob = new Glob("**");
41 expect(ASCII_WITHOUT_SLASH, contains(glob));
42 });
43
44 test("matches the empty string", () {
45 var glob = new Glob("foo**");
46 expect("foo", contains(glob));
47 });
48
49 test("matches any level of nesting", () {
50 var glob = new Glob("**");
51 expect("a", contains(glob));
52 expect("a/b/c/d/e/f", contains(glob));
53 });
54
55 test("doesn't match unresolved dot dots", () {
56 expect("../foo/bar", isNot(contains(new Glob("**"))));
57 });
58
59 test("matches entities containing dot dots", () {
60 expect("..foo/bar", contains(new Glob("**")));
61 expect("foo../bar", contains(new Glob("**")));
62 expect("foo/..bar", contains(new Glob("**")));
63 expect("foo/bar..", contains(new Glob("**")));
64 });
65 });
66
67 group("any char", () {
68 test("matches any non-separator character", () {
69 var glob = new Glob("foo?");
70 for (var char in ASCII_WITHOUT_SLASH.split('')) {
71 expect("foo$char", contains(glob));
72 }
73 });
74
75 test("doesn't match a separator", () {
76 expect("foo/bar", isNot(contains(new Glob("foo?bar"))));
77 });
78 });
79
80 group("range", () {
81 test("can match individual characters", () {
82 var glob = new Glob("foo[a<.*]");
83 expect("fooa", contains(glob));
84 expect("foo<", contains(glob));
85 expect("foo.", contains(glob));
86 expect("foo*", contains(glob));
87 expect("foob", isNot(contains(glob)));
88 expect("foo>", isNot(contains(glob)));
89 });
90
91 test("can match a range of characters", () {
92 var glob = new Glob("foo[a-z]");
93 expect("fooa", contains(glob));
94 expect("foon", contains(glob));
95 expect("fooz", contains(glob));
96 expect("foo`", isNot(contains(glob)));
97 expect("foo{", isNot(contains(glob)));
98 });
99
100 test("can match multiple ranges of characters", () {
101 var glob = new Glob("foo[a-zA-Z]");
102 expect("fooa", contains(glob));
103 expect("foon", contains(glob));
104 expect("fooz", contains(glob));
105 expect("fooA", contains(glob));
106 expect("fooN", contains(glob));
107 expect("fooZ", contains(glob));
108 expect("foo?", isNot(contains(glob)));
109 expect("foo{", isNot(contains(glob)));
110 });
111
112 test("can match individual characters and ranges of characters", () {
113 var glob = new Glob("foo[a-z_A-Z]");
114 expect("fooa", contains(glob));
115 expect("foon", contains(glob));
116 expect("fooz", contains(glob));
117 expect("fooA", contains(glob));
118 expect("fooN", contains(glob));
119 expect("fooZ", contains(glob));
120 expect("foo_", contains(glob));
121 expect("foo?", isNot(contains(glob)));
122 expect("foo{", isNot(contains(glob)));
123 });
124
125 test("can be negated", () {
126 var glob = new Glob("foo[^a<.*]");
127 expect("fooa", isNot(contains(glob)));
128 expect("foo<", isNot(contains(glob)));
129 expect("foo.", isNot(contains(glob)));
130 expect("foo*", isNot(contains(glob)));
131 expect("foob", contains(glob));
132 expect("foo>", contains(glob));
133 });
134
135 test("never matches separators", () {
136 expect("foo/bar", isNot(contains(new Glob("foo[/]bar"))));
137 // "\t-~" contains "/".
138 expect("foo/bar", isNot(contains(new Glob("foo[\t-~]bar"))));
139 expect("foo/bar", isNot(contains(new Glob("foo[^a]bar"))));
140 });
141
142 test("allows dangling -", () {
143 expect("-", contains(new Glob(r"[-]")));
144
145 var glob = new Glob(r"[a-]");
146 expect("-", contains(glob));
147 expect("a", contains(glob));
148
149 glob = new Glob(r"[-b]");
150 expect("-", contains(glob));
151 expect("b", contains(glob));
152 });
153
154 test("allows multiple -s", () {
155 expect("-", contains(new Glob(r"[--]")));
156 expect("-", contains(new Glob(r"[---]")));
157
158 var glob = new Glob(r"[--a]");
159 expect("-", contains(glob));
160 expect("a", contains(glob));
161 });
162
163 test("doesn't choke on RegExp-active characters", () {
164 var glob = new Glob(r"foo[\]].*");
165 expect("foobar", isNot(contains(glob)));
166 expect("foo].*", contains(glob));
167 });
168 });
169
170 group("options", () {
171 test("match if any of the options match", () {
172 var glob = new Glob("foo/{bar,baz,bang}");
173 expect("foo/bar", contains(glob));
174 expect("foo/baz", contains(glob));
175 expect("foo/bang", contains(glob));
176 expect("foo/qux", isNot(contains(glob)));
177 });
178
179 test("can contain nested operators", () {
180 var glob = new Glob("foo/{ba?,*az,ban{g,f}}");
181 expect("foo/bar", contains(glob));
182 expect("foo/baz", contains(glob));
183 expect("foo/bang", contains(glob));
184 expect("foo/qux", isNot(contains(glob)));
185 });
186
187 test("can conditionally match separators", () {
188 var glob = new Glob("foo/{bar,baz/bang}");
189 expect("foo/bar", contains(glob));
190 expect("foo/baz/bang", contains(glob));
191 expect("foo/baz", isNot(contains(glob)));
192 expect("foo/bar/bang", isNot(contains(glob)));
193 });
194 });
195
196 group("normalization", () {
197 test("extra slashes are ignored", () {
198 expect("foo//bar", contains(new Glob("foo/bar")));
199 expect("foo/", contains(new Glob("*")));
200 });
201
202 test("dot directories are ignored", () {
203 expect("foo/./bar", contains(new Glob("foo/bar")));
204 expect("foo/.", contains(new Glob("foo")));
205 });
206
207 test("dot dot directories are resolved", () {
208 expect("foo/../bar", contains(new Glob("bar")));
209 expect("../foo/bar", contains(new Glob("../foo/bar")));
210 expect("foo/../../bar", contains(new Glob("../bar")));
211 });
212
213 test("Windows separators are converted in a Windows context", () {
214 expect(r"foo\bar", contains(new Glob("foo/bar", context: p.windows)));
215 expect(r"foo\bar/baz",
216 contains(new Glob("foo/bar/baz", context: p.windows)));
217 });
218 });
219
220 test("an absolute path can be matched by a relative glob", () {
221 var path = p.absolute('foo/bar');
222 expect(path, contains(new Glob("foo/bar")));
223 });
224
225 test("a relative path can be matched by an absolute glob", () {
226 var pattern = p.absolute('foo/bar');
227 if (Platform.isWindows) pattern = pattern.replaceAll('\\', '/');
228 expect('foo/bar', contains(new Glob(pattern)));
229 });
230
231 group("with recursive: true", () {
232 var glob = new Glob("foo/bar", recursive: true);
233
234 test("still matches basic files", () {
235 expect("foo/bar", contains(glob));
236 });
237
238 test("matches subfiles", () {
239 expect("foo/bar/baz", contains(glob));
240 expect("foo/bar/baz/bang", contains(glob));
241 });
242
243 test("doesn't match suffixes", () {
244 expect("foo/barbaz", isNot(contains(glob)));
245 expect("foo/barbaz/bang", isNot(contains(glob)));
246 });
247 });
248
249 test("absolute POSIX paths", () {
250 expect("/foo/bar", contains(new Glob("/foo/bar", context: p.posix)));
251 expect("/foo/bar", isNot(contains(new Glob("**", context: p.posix))));
252 expect("/foo/bar", contains(new Glob("/**", context: p.posix)));
253 });
254
255 test("absolute Windows paths", () {
256 expect(r"C:\foo\bar", contains(new Glob("C:/foo/bar", context: p.windows)));
257 expect(r"C:\foo\bar", isNot(contains(new Glob("**", context: p.windows))));
258 expect(r"C:\foo\bar", contains(new Glob("C:/**", context: p.windows)));
259
260 expect(r"\\foo\bar\baz",
261 contains(new Glob("//foo/bar/baz", context: p.windows)));
262 expect(r"\\foo\bar\baz",
263 isNot(contains(new Glob("**", context: p.windows))));
264 expect(r"\\foo\bar\baz", contains(new Glob("//**", context: p.windows)));
265 expect(r"\\foo\bar\baz",
266 contains(new Glob("//foo/**", context: p.windows)));
267 });
268
269 test("absolute URL paths", () {
270 expect(r"http://foo.com/bar",
271 contains(new Glob("http://foo.com/bar", context: p.url)));
272 expect(r"http://foo.com/bar",
273 isNot(contains(new Glob("**", context: p.url))));
274 expect(r"http://foo.com/bar",
275 contains(new Glob("http://**", context: p.url)));
276 expect(r"http://foo.com/bar",
277 contains(new Glob("http://foo.com/**", context: p.url)));
278
279 expect("/foo/bar", contains(new Glob("/foo/bar", context: p.url)));
280 expect("/foo/bar", isNot(contains(new Glob("**", context: p.url))));
281 expect("/foo/bar", contains(new Glob("/**", context: p.url)));
282 });
283 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698