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