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

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("foo\\*")));
Bob Nystrom 2014/08/27 22:16:44 Raw string here?
nweiz 2014/09/02 19:48:15 Done.
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 var glob = new Glob("foo*");
29 expect("foo", contains(glob));
30 });
Bob Nystrom 2014/08/27 22:16:44 What about a totally empty string? expect("", con
nweiz 2014/09/02 19:48:15 Done.
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"))));
Bob Nystrom 2014/08/27 22:16:44 Should it be an error to use "/" in a [] then?
nweiz 2014/09/02 19:48:15 I'm not sure. On one hand, it won't ever match any
Bob Nystrom 2014/09/02 20:23:51 Yeah, I think it makes sense to allow ranges that
nweiz 2014/09/02 23:20:16 Done.
137 expect("foo/bar", isNot(contains(new Glob("foo[\t-~]bar"))));
Bob Nystrom 2014/08/27 22:16:44 Document that \t-~ is a range that includes "/".
nweiz 2014/09/02 19:48:15 Done.
138 expect("foo/bar", isNot(contains(new Glob("foo[^a]bar"))));
139 });
140
141 test("doesn't choke on RegExp-active characters", () {
142 var glob = new Glob(r"foo[\]].*");
143 expect("foobar", isNot(contains(glob)));
144 expect("foo].*", contains(glob));
145 });
146 });
147
148 group("options", () {
149 test("match if any of the options match", () {
150 var glob = new Glob("foo/{bar,baz,bang}");
151 expect("foo/bar", contains(glob));
152 expect("foo/baz", contains(glob));
153 expect("foo/bang", contains(glob));
154 expect("foo/qux", isNot(contains(glob)));
155 });
156
157 test("can contain nested operators", () {
158 var glob = new Glob("foo/{ba?,*az,ban{g,f}}");
159 expect("foo/bar", contains(glob));
160 expect("foo/baz", contains(glob));
161 expect("foo/bang", contains(glob));
162 expect("foo/qux", isNot(contains(glob)));
163 });
164
165 test("can conditionally match separators", () {
166 var glob = new Glob("foo/{bar,baz/bang}");
167 expect("foo/bar", contains(glob));
168 expect("foo/baz/bang", contains(glob));
169 expect("foo/baz", isNot(contains(glob)));
170 expect("foo/bar/bang", isNot(contains(glob)));
171 });
172 });
173
174 group("normalization", () {
175 test("extra slashes are ignored", () {
176 expect("foo//bar", contains(new Glob("foo/bar")));
177 expect("foo/", contains(new Glob("*")));
178 });
179
180 test("dot directories are ignored", () {
181 expect("foo/./bar", contains(new Glob("foo/bar")));
182 expect("foo/.", contains(new Glob("foo")));
183 });
184
185 test("dot dot directories are resolved", () {
186 expect("foo/../bar", contains(new Glob("bar")));
187 expect("../foo/bar", contains(new Glob("../foo/bar")));
188 expect("foo/../../bar", contains(new Glob("../bar")));
189 });
190
191 test("Windows separators are converted in a Windows context", () {
192 expect(r"foo\bar", contains(new Glob("foo/bar", context: p.windows)));
193 expect(r"foo\bar/baz",
194 contains(new Glob("foo/bar/baz", context: p.windows)));
195 });
196 });
197
198 test("an absolute path can be matched by a relative glob", () {
199 var path = p.absolute('foo/bar');
200 expect(path, contains(new Glob("foo/bar")));
201 });
202
203 test("a relative path can be matched by an absolute glob", () {
204 var pattern = p.absolute('foo/bar');
205 if (Platform.isWindows) pattern = pattern.replaceAll('\\', '/');
206 expect('foo/bar', contains(new Glob(pattern)));
207 });
208
209 group("with recursive: true", () {
210 var glob = new Glob("foo/bar", recursive: true);
211
212 test("still matches basic files", () {
213 expect("foo/bar", contains(glob));
214 });
215
216 test("matches subfiles", () {
217 expect("foo/bar/baz", contains(glob));
218 expect("foo/bar/baz/bang", contains(glob));
219 });
220
221 test("doesn't match suffixes", () {
222 expect("foo/barbaz", isNot(contains(glob)));
223 expect("foo/barbaz/bang", isNot(contains(glob)));
224 });
225 });
226
227 test("absolute POSIX paths", () {
228 expect("/foo/bar", contains(new Glob("/foo/bar", context: p.posix)));
229 expect("/foo/bar", isNot(contains(new Glob("**", context: p.posix))));
230 expect("/foo/bar", contains(new Glob("/**", context: p.posix)));
231 });
232
233 test("absolute Windows paths", () {
234 expect(r"C:\foo\bar", contains(new Glob("C:/foo/bar", context: p.windows)));
235 expect(r"C:\foo\bar", isNot(contains(new Glob("**", context: p.windows))));
236 expect(r"C:\foo\bar", contains(new Glob("C:/**", context: p.windows)));
237
238 expect(r"\\foo\bar\baz",
239 contains(new Glob("//foo/bar/baz", context: p.windows)));
240 expect(r"\\foo\bar\baz",
241 isNot(contains(new Glob("**", context: p.windows))));
242 expect(r"\\foo\bar\baz", contains(new Glob("//**", context: p.windows)));
243 expect(r"\\foo\bar\baz",
244 contains(new Glob("//foo/**", context: p.windows)));
245 });
246
247 test("absolute URL paths", () {
248 expect(r"http://foo.com/bar",
249 contains(new Glob("http://foo.com/bar", context: p.url)));
250 expect(r"http://foo.com/bar",
251 isNot(contains(new Glob("**", context: p.url))));
252 expect(r"http://foo.com/bar",
253 contains(new Glob("http://**", context: p.url)));
254 expect(r"http://foo.com/bar",
255 contains(new Glob("http://foo.com/**", context: p.url)));
256
257 expect("/foo/bar", contains(new Glob("/foo/bar", context: p.url)));
258 expect("/foo/bar", isNot(contains(new Glob("**", context: p.url))));
259 expect("/foo/bar", contains(new Glob("/**", context: p.url)));
260 });
261 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698