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

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
« no previous file with comments | « pkg/glob/test/glob_test.dart ('k') | pkg/glob/test/parse_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // "\t-~" contains "/".
137 expect("foo/bar", isNot(contains(new Glob("foo[\t-~]bar"))));
138 expect("foo/bar", isNot(contains(new Glob("foo[^a]bar"))));
139 });
140
141 test("allows dangling -", () {
142 expect("-", contains(new Glob(r"[-]")));
143
144 var glob = new Glob(r"[a-]");
145 expect("-", contains(glob));
146 expect("a", contains(glob));
147
148 glob = new Glob(r"[-b]");
149 expect("-", contains(glob));
150 expect("b", contains(glob));
151 });
152
153 test("allows multiple -s", () {
154 expect("-", contains(new Glob(r"[--]")));
155 expect("-", contains(new Glob(r"[---]")));
156
157 var glob = new Glob(r"[--a]");
158 expect("-", contains(glob));
159 expect("a", contains(glob));
160 });
161
162 test("allows negated /", () {
163 expect("foo-bar", contains(new Glob("foo[^/]bar")));
164 });
165
166 test("doesn't choke on RegExp-active characters", () {
167 var glob = new Glob(r"foo[\]].*");
168 expect("foobar", isNot(contains(glob)));
169 expect("foo].*", contains(glob));
170 });
171 });
172
173 group("options", () {
174 test("match if any of the options match", () {
175 var glob = new Glob("foo/{bar,baz,bang}");
176 expect("foo/bar", contains(glob));
177 expect("foo/baz", contains(glob));
178 expect("foo/bang", contains(glob));
179 expect("foo/qux", isNot(contains(glob)));
180 });
181
182 test("can contain nested operators", () {
183 var glob = new Glob("foo/{ba?,*az,ban{g,f}}");
184 expect("foo/bar", contains(glob));
185 expect("foo/baz", contains(glob));
186 expect("foo/bang", contains(glob));
187 expect("foo/qux", isNot(contains(glob)));
188 });
189
190 test("can conditionally match separators", () {
191 var glob = new Glob("foo/{bar,baz/bang}");
192 expect("foo/bar", contains(glob));
193 expect("foo/baz/bang", contains(glob));
194 expect("foo/baz", isNot(contains(glob)));
195 expect("foo/bar/bang", isNot(contains(glob)));
196 });
197 });
198
199 group("normalization", () {
200 test("extra slashes are ignored", () {
201 expect("foo//bar", contains(new Glob("foo/bar")));
202 expect("foo/", contains(new Glob("*")));
203 });
204
205 test("dot directories are ignored", () {
206 expect("foo/./bar", contains(new Glob("foo/bar")));
207 expect("foo/.", contains(new Glob("foo")));
208 });
209
210 test("dot dot directories are resolved", () {
211 expect("foo/../bar", contains(new Glob("bar")));
212 expect("../foo/bar", contains(new Glob("../foo/bar")));
213 expect("foo/../../bar", contains(new Glob("../bar")));
214 });
215
216 test("Windows separators are converted in a Windows context", () {
217 expect(r"foo\bar", contains(new Glob("foo/bar", context: p.windows)));
218 expect(r"foo\bar/baz",
219 contains(new Glob("foo/bar/baz", context: p.windows)));
220 });
221 });
222
223 test("an absolute path can be matched by a relative glob", () {
224 var path = p.absolute('foo/bar');
225 expect(path, contains(new Glob("foo/bar")));
226 });
227
228 test("a relative path can be matched by an absolute glob", () {
229 var pattern = p.absolute('foo/bar');
230 if (Platform.isWindows) pattern = pattern.replaceAll('\\', '/');
231 expect('foo/bar', contains(new Glob(pattern)));
232 });
233
234 group("with recursive: true", () {
235 var glob = new Glob("foo/bar", recursive: true);
236
237 test("still matches basic files", () {
238 expect("foo/bar", contains(glob));
239 });
240
241 test("matches subfiles", () {
242 expect("foo/bar/baz", contains(glob));
243 expect("foo/bar/baz/bang", contains(glob));
244 });
245
246 test("doesn't match suffixes", () {
247 expect("foo/barbaz", isNot(contains(glob)));
248 expect("foo/barbaz/bang", isNot(contains(glob)));
249 });
250 });
251
252 test("absolute POSIX paths", () {
253 expect("/foo/bar", contains(new Glob("/foo/bar", context: p.posix)));
254 expect("/foo/bar", isNot(contains(new Glob("**", context: p.posix))));
255 expect("/foo/bar", contains(new Glob("/**", context: p.posix)));
256 });
257
258 test("absolute Windows paths", () {
259 expect(r"C:\foo\bar", contains(new Glob("C:/foo/bar", context: p.windows)));
260 expect(r"C:\foo\bar", isNot(contains(new Glob("**", context: p.windows))));
261 expect(r"C:\foo\bar", contains(new Glob("C:/**", context: p.windows)));
262
263 expect(r"\\foo\bar\baz",
264 contains(new Glob("//foo/bar/baz", context: p.windows)));
265 expect(r"\\foo\bar\baz",
266 isNot(contains(new Glob("**", context: p.windows))));
267 expect(r"\\foo\bar\baz", contains(new Glob("//**", context: p.windows)));
268 expect(r"\\foo\bar\baz",
269 contains(new Glob("//foo/**", context: p.windows)));
270 });
271
272 test("absolute URL paths", () {
273 expect(r"http://foo.com/bar",
274 contains(new Glob("http://foo.com/bar", context: p.url)));
275 expect(r"http://foo.com/bar",
276 isNot(contains(new Glob("**", context: p.url))));
277 expect(r"http://foo.com/bar",
278 contains(new Glob("http://**", context: p.url)));
279 expect(r"http://foo.com/bar",
280 contains(new Glob("http://foo.com/**", context: p.url)));
281
282 expect("/foo/bar", contains(new Glob("/foo/bar", context: p.url)));
283 expect("/foo/bar", isNot(contains(new Glob("**", context: p.url))));
284 expect("/foo/bar", contains(new Glob("/**", context: p.url)));
285 });
286 }
OLDNEW
« no previous file with comments | « pkg/glob/test/glob_test.dart ('k') | pkg/glob/test/parse_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698