| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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:unittest/unittest.dart'; | |
| 6 import 'package:path/path.dart' as path; | |
| 7 | |
| 8 main() { | |
| 9 var context = new path.Context(style: path.Style.url, | |
| 10 current: 'http://dartlang.org/root/path'); | |
| 11 | |
| 12 test('separator', () { | |
| 13 expect(context.separator, '/'); | |
| 14 }); | |
| 15 | |
| 16 test('extension', () { | |
| 17 expect(context.extension(''), ''); | |
| 18 expect(context.extension('foo.dart'), '.dart'); | |
| 19 expect(context.extension('foo.dart.js'), '.js'); | |
| 20 expect(context.extension('a.b/c'), ''); | |
| 21 expect(context.extension('a.b/c.d'), '.d'); | |
| 22 expect(context.extension(r'a.b\c'), r'.b\c'); | |
| 23 expect(context.extension('foo.dart/'), '.dart'); | |
| 24 expect(context.extension('foo.dart//'), '.dart'); | |
| 25 }); | |
| 26 | |
| 27 test('rootPrefix', () { | |
| 28 expect(context.rootPrefix(''), ''); | |
| 29 expect(context.rootPrefix('a'), ''); | |
| 30 expect(context.rootPrefix('a/b'), ''); | |
| 31 expect(context.rootPrefix('http://dartlang.org/a/c'), | |
| 32 'http://dartlang.org'); | |
| 33 expect(context.rootPrefix('file:///a/c'), 'file://'); | |
| 34 expect(context.rootPrefix('/a/c'), '/'); | |
| 35 expect(context.rootPrefix('http://dartlang.org/'), 'http://dartlang.org'); | |
| 36 expect(context.rootPrefix('file:///'), 'file://'); | |
| 37 expect(context.rootPrefix('http://dartlang.org'), 'http://dartlang.org'); | |
| 38 expect(context.rootPrefix('file://'), 'file://'); | |
| 39 expect(context.rootPrefix('/'), '/'); | |
| 40 expect(context.rootPrefix('foo/bar://'), ''); | |
| 41 }); | |
| 42 | |
| 43 test('dirname', () { | |
| 44 expect(context.dirname(''), '.'); | |
| 45 expect(context.dirname('a'), '.'); | |
| 46 expect(context.dirname('a/b'), 'a'); | |
| 47 expect(context.dirname('a/b/c'), 'a/b'); | |
| 48 expect(context.dirname('a/b.c'), 'a'); | |
| 49 expect(context.dirname('a/'), '.'); | |
| 50 expect(context.dirname('a/.'), 'a'); | |
| 51 expect(context.dirname(r'a\b/c'), r'a\b'); | |
| 52 expect(context.dirname('http://dartlang.org/a'), 'http://dartlang.org'); | |
| 53 expect(context.dirname('file:///a'), 'file://'); | |
| 54 expect(context.dirname('/a'), '/'); | |
| 55 expect(context.dirname('http://dartlang.org///a'), 'http://dartlang.org'); | |
| 56 expect(context.dirname('file://///a'), 'file://'); | |
| 57 expect(context.dirname('///a'), '/'); | |
| 58 expect(context.dirname('http://dartlang.org/'), 'http://dartlang.org'); | |
| 59 expect(context.dirname('http://dartlang.org'), 'http://dartlang.org'); | |
| 60 expect(context.dirname('file:///'), 'file://'); | |
| 61 expect(context.dirname('file://'), 'file://'); | |
| 62 expect(context.dirname('/'), '/'); | |
| 63 expect(context.dirname('http://dartlang.org///'), 'http://dartlang.org'); | |
| 64 expect(context.dirname('file://///'), 'file://'); | |
| 65 expect(context.dirname('///'), '/'); | |
| 66 expect(context.dirname('a/b/'), 'a'); | |
| 67 expect(context.dirname(r'a/b\c'), 'a'); | |
| 68 expect(context.dirname('a//'), '.'); | |
| 69 expect(context.dirname('a/b//'), 'a'); | |
| 70 expect(context.dirname('a//b'), 'a'); | |
| 71 }); | |
| 72 | |
| 73 test('basename', () { | |
| 74 expect(context.basename(''), ''); | |
| 75 expect(context.basename('a'), 'a'); | |
| 76 expect(context.basename('a/b'), 'b'); | |
| 77 expect(context.basename('a/b/c'), 'c'); | |
| 78 expect(context.basename('a/b.c'), 'b.c'); | |
| 79 expect(context.basename('a/'), 'a'); | |
| 80 expect(context.basename('a/.'), '.'); | |
| 81 expect(context.basename(r'a\b/c'), 'c'); | |
| 82 expect(context.basename('http://dartlang.org/a'), 'a'); | |
| 83 expect(context.basename('file:///a'), 'a'); | |
| 84 expect(context.basename('/a'), 'a'); | |
| 85 expect(context.basename('http://dartlang.org/'), 'http://dartlang.org'); | |
| 86 expect(context.basename('http://dartlang.org'), 'http://dartlang.org'); | |
| 87 expect(context.basename('file:///'), 'file://'); | |
| 88 expect(context.basename('file://'), 'file://'); | |
| 89 expect(context.basename('/'), '/'); | |
| 90 expect(context.basename('a/b/'), 'b'); | |
| 91 expect(context.basename(r'a/b\c'), r'b\c'); | |
| 92 expect(context.basename('a//'), 'a'); | |
| 93 expect(context.basename('a/b//'), 'b'); | |
| 94 expect(context.basename('a//b'), 'b'); | |
| 95 expect(context.basename('a b/c d.e f'), 'c d.e f'); | |
| 96 }); | |
| 97 | |
| 98 test('basenameWithoutExtension', () { | |
| 99 expect(context.basenameWithoutExtension(''), ''); | |
| 100 expect(context.basenameWithoutExtension('.'), '.'); | |
| 101 expect(context.basenameWithoutExtension('..'), '..'); | |
| 102 expect(context.basenameWithoutExtension('a'), 'a'); | |
| 103 expect(context.basenameWithoutExtension('a/b'), 'b'); | |
| 104 expect(context.basenameWithoutExtension('a/b/c'), 'c'); | |
| 105 expect(context.basenameWithoutExtension('a/b.c'), 'b'); | |
| 106 expect(context.basenameWithoutExtension('a/'), 'a'); | |
| 107 expect(context.basenameWithoutExtension('a/.'), '.'); | |
| 108 expect(context.basenameWithoutExtension(r'a/b\c'), r'b\c'); | |
| 109 expect(context.basenameWithoutExtension('a/.bashrc'), '.bashrc'); | |
| 110 expect(context.basenameWithoutExtension('a/b/c.d.e'), 'c.d'); | |
| 111 expect(context.basenameWithoutExtension('a//'), 'a'); | |
| 112 expect(context.basenameWithoutExtension('a/b//'), 'b'); | |
| 113 expect(context.basenameWithoutExtension('a//b'), 'b'); | |
| 114 expect(context.basenameWithoutExtension('a/b.c/'), 'b'); | |
| 115 expect(context.basenameWithoutExtension('a/b.c//'), 'b'); | |
| 116 expect(context.basenameWithoutExtension('a/b c.d e.f g'), 'b c.d e'); | |
| 117 }); | |
| 118 | |
| 119 test('isAbsolute', () { | |
| 120 expect(context.isAbsolute(''), false); | |
| 121 expect(context.isAbsolute('a'), false); | |
| 122 expect(context.isAbsolute('a/b'), false); | |
| 123 expect(context.isAbsolute('http://dartlang.org/a'), true); | |
| 124 expect(context.isAbsolute('file:///a'), true); | |
| 125 expect(context.isAbsolute('/a'), true); | |
| 126 expect(context.isAbsolute('http://dartlang.org/a/b'), true); | |
| 127 expect(context.isAbsolute('file:///a/b'), true); | |
| 128 expect(context.isAbsolute('/a/b'), true); | |
| 129 expect(context.isAbsolute('http://dartlang.org/'), true); | |
| 130 expect(context.isAbsolute('file:///'), true); | |
| 131 expect(context.isAbsolute('http://dartlang.org'), true); | |
| 132 expect(context.isAbsolute('file://'), true); | |
| 133 expect(context.isAbsolute('/'), true); | |
| 134 expect(context.isAbsolute('~'), false); | |
| 135 expect(context.isAbsolute('.'), false); | |
| 136 expect(context.isAbsolute('../a'), false); | |
| 137 expect(context.isAbsolute('C:/a'), false); | |
| 138 expect(context.isAbsolute(r'C:\a'), false); | |
| 139 expect(context.isAbsolute(r'\\a'), false); | |
| 140 }); | |
| 141 | |
| 142 test('isRelative', () { | |
| 143 expect(context.isRelative(''), true); | |
| 144 expect(context.isRelative('a'), true); | |
| 145 expect(context.isRelative('a/b'), true); | |
| 146 expect(context.isRelative('http://dartlang.org/a'), false); | |
| 147 expect(context.isRelative('file:///a'), false); | |
| 148 expect(context.isRelative('/a'), false); | |
| 149 expect(context.isRelative('http://dartlang.org/a/b'), false); | |
| 150 expect(context.isRelative('file:///a/b'), false); | |
| 151 expect(context.isRelative('/a/b'), false); | |
| 152 expect(context.isRelative('http://dartlang.org/'), false); | |
| 153 expect(context.isRelative('file:///'), false); | |
| 154 expect(context.isRelative('http://dartlang.org'), false); | |
| 155 expect(context.isRelative('file://'), false); | |
| 156 expect(context.isRelative('/'), false); | |
| 157 expect(context.isRelative('~'), true); | |
| 158 expect(context.isRelative('.'), true); | |
| 159 expect(context.isRelative('../a'), true); | |
| 160 expect(context.isRelative('C:/a'), true); | |
| 161 expect(context.isRelative(r'C:\a'), true); | |
| 162 expect(context.isRelative(r'\\a'), true); | |
| 163 }); | |
| 164 | |
| 165 test('isRootRelative', () { | |
| 166 expect(context.isRootRelative(''), false); | |
| 167 expect(context.isRootRelative('a'), false); | |
| 168 expect(context.isRootRelative('a/b'), false); | |
| 169 expect(context.isRootRelative('http://dartlang.org/a'), false); | |
| 170 expect(context.isRootRelative('file:///a'), false); | |
| 171 expect(context.isRootRelative('/a'), true); | |
| 172 expect(context.isRootRelative('http://dartlang.org/a/b'), false); | |
| 173 expect(context.isRootRelative('file:///a/b'), false); | |
| 174 expect(context.isRootRelative('/a/b'), true); | |
| 175 expect(context.isRootRelative('http://dartlang.org/'), false); | |
| 176 expect(context.isRootRelative('file:///'), false); | |
| 177 expect(context.isRootRelative('http://dartlang.org'), false); | |
| 178 expect(context.isRootRelative('file://'), false); | |
| 179 expect(context.isRootRelative('/'), true); | |
| 180 expect(context.isRootRelative('~'), false); | |
| 181 expect(context.isRootRelative('.'), false); | |
| 182 expect(context.isRootRelative('../a'), false); | |
| 183 expect(context.isRootRelative('C:/a'), false); | |
| 184 expect(context.isRootRelative(r'C:\a'), false); | |
| 185 expect(context.isRootRelative(r'\\a'), false); | |
| 186 }); | |
| 187 | |
| 188 group('join', () { | |
| 189 test('allows up to eight parts', () { | |
| 190 expect(context.join('a'), 'a'); | |
| 191 expect(context.join('a', 'b'), 'a/b'); | |
| 192 expect(context.join('a', 'b', 'c'), 'a/b/c'); | |
| 193 expect(context.join('a', 'b', 'c', 'd'), 'a/b/c/d'); | |
| 194 expect(context.join('a', 'b', 'c', 'd', 'e'), 'a/b/c/d/e'); | |
| 195 expect(context.join('a', 'b', 'c', 'd', 'e', 'f'), 'a/b/c/d/e/f'); | |
| 196 expect(context.join('a', 'b', 'c', 'd', 'e', 'f', 'g'), 'a/b/c/d/e/f/g'); | |
| 197 expect(context.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'), | |
| 198 'a/b/c/d/e/f/g/h'); | |
| 199 }); | |
| 200 | |
| 201 test('does not add separator if a part ends in one', () { | |
| 202 expect(context.join('a/', 'b', 'c/', 'd'), 'a/b/c/d'); | |
| 203 expect(context.join('a\\', 'b'), r'a\/b'); | |
| 204 }); | |
| 205 | |
| 206 test('ignores parts before an absolute path', () { | |
| 207 expect(context.join('a', 'http://dartlang.org', 'b', 'c'), | |
| 208 'http://dartlang.org/b/c'); | |
| 209 expect(context.join('a', 'file://', 'b', 'c'), 'file:///b/c'); | |
| 210 expect(context.join('a', '/', 'b', 'c'), '/b/c'); | |
| 211 expect(context.join('a', '/b', 'http://dartlang.org/c', 'd'), | |
| 212 'http://dartlang.org/c/d'); | |
| 213 expect(context.join( | |
| 214 'a', 'http://google.com/b', 'http://dartlang.org/c', 'd'), | |
| 215 'http://dartlang.org/c/d'); | |
| 216 expect(context.join('a', '/b', '/c', 'd'), '/c/d'); | |
| 217 expect(context.join('a', r'c:\b', 'c', 'd'), r'a/c:\b/c/d'); | |
| 218 expect(context.join('a', r'\\b', 'c', 'd'), r'a/\\b/c/d'); | |
| 219 }); | |
| 220 | |
| 221 test('preserves roots before a root-relative path', () { | |
| 222 expect(context.join('http://dartlang.org', 'a', '/b', 'c'), | |
| 223 'http://dartlang.org/b/c'); | |
| 224 expect(context.join('file://', 'a', '/b', 'c'), 'file:///b/c'); | |
| 225 expect(context.join('file://', 'a', '/b', 'c', '/d'), 'file:///d'); | |
| 226 }); | |
| 227 | |
| 228 test('ignores trailing nulls', () { | |
| 229 expect(context.join('a', null), equals('a')); | |
| 230 expect(context.join('a', 'b', 'c', null, null), equals('a/b/c')); | |
| 231 }); | |
| 232 | |
| 233 test('ignores empty strings', () { | |
| 234 expect(context.join(''), ''); | |
| 235 expect(context.join('', ''), ''); | |
| 236 expect(context.join('', 'a'), 'a'); | |
| 237 expect(context.join('a', '', 'b', '', '', '', 'c'), 'a/b/c'); | |
| 238 expect(context.join('a', 'b', ''), 'a/b'); | |
| 239 }); | |
| 240 | |
| 241 test('disallows intermediate nulls', () { | |
| 242 expect(() => context.join('a', null, 'b'), throwsArgumentError); | |
| 243 expect(() => context.join(null, 'a'), throwsArgumentError); | |
| 244 }); | |
| 245 | |
| 246 test('Join does not modify internal ., .., or trailing separators', () { | |
| 247 expect(context.join('a/', 'b/c/'), 'a/b/c/'); | |
| 248 expect(context.join('a/b/./c/..//', 'd/.././..//e/f//'), | |
| 249 'a/b/./c/..//d/.././..//e/f//'); | |
| 250 expect(context.join('a/b', 'c/../../../..'), 'a/b/c/../../../..'); | |
| 251 expect(context.join('a', 'b${context.separator}'), 'a/b/'); | |
| 252 }); | |
| 253 }); | |
| 254 | |
| 255 group('joinAll', () { | |
| 256 test('allows more than eight parts', () { | |
| 257 expect(context.joinAll(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']), | |
| 258 'a/b/c/d/e/f/g/h/i'); | |
| 259 }); | |
| 260 | |
| 261 test('ignores parts before an absolute path', () { | |
| 262 expect(context.joinAll(['a', 'http://dartlang.org', 'b', 'c']), | |
| 263 'http://dartlang.org/b/c'); | |
| 264 expect(context.joinAll(['a', 'file://', 'b', 'c']), 'file:///b/c'); | |
| 265 expect(context.joinAll(['a', '/', 'b', 'c']), '/b/c'); | |
| 266 expect(context.joinAll(['a', '/b', 'http://dartlang.org/c', 'd']), | |
| 267 'http://dartlang.org/c/d'); | |
| 268 expect(context.joinAll( | |
| 269 ['a', 'http://google.com/b', 'http://dartlang.org/c', 'd']), | |
| 270 'http://dartlang.org/c/d'); | |
| 271 expect(context.joinAll(['a', '/b', '/c', 'd']), '/c/d'); | |
| 272 expect(context.joinAll(['a', r'c:\b', 'c', 'd']), r'a/c:\b/c/d'); | |
| 273 expect(context.joinAll(['a', r'\\b', 'c', 'd']), r'a/\\b/c/d'); | |
| 274 }); | |
| 275 | |
| 276 test('preserves roots before a root-relative path', () { | |
| 277 expect(context.joinAll(['http://dartlang.org', 'a', '/b', 'c']), | |
| 278 'http://dartlang.org/b/c'); | |
| 279 expect(context.joinAll(['file://', 'a', '/b', 'c']), 'file:///b/c'); | |
| 280 expect(context.joinAll(['file://', 'a', '/b', 'c', '/d']), 'file:///d'); | |
| 281 }); | |
| 282 }); | |
| 283 | |
| 284 group('split', () { | |
| 285 test('simple cases', () { | |
| 286 expect(context.split(''), []); | |
| 287 expect(context.split('.'), ['.']); | |
| 288 expect(context.split('..'), ['..']); | |
| 289 expect(context.split('foo'), equals(['foo'])); | |
| 290 expect(context.split('foo/bar.txt'), equals(['foo', 'bar.txt'])); | |
| 291 expect(context.split('foo/bar/baz'), equals(['foo', 'bar', 'baz'])); | |
| 292 expect(context.split('foo/../bar/./baz'), | |
| 293 equals(['foo', '..', 'bar', '.', 'baz'])); | |
| 294 expect(context.split('foo//bar///baz'), equals(['foo', 'bar', 'baz'])); | |
| 295 expect(context.split('foo/\\/baz'), equals(['foo', '\\', 'baz'])); | |
| 296 expect(context.split('.'), equals(['.'])); | |
| 297 expect(context.split(''), equals([])); | |
| 298 expect(context.split('foo/'), equals(['foo'])); | |
| 299 expect(context.split('http://dartlang.org//'), | |
| 300 equals(['http://dartlang.org'])); | |
| 301 expect(context.split('file:////'), equals(['file://'])); | |
| 302 expect(context.split('//'), equals(['/'])); | |
| 303 }); | |
| 304 | |
| 305 test('includes the root for absolute paths', () { | |
| 306 expect(context.split('http://dartlang.org/foo/bar/baz'), | |
| 307 equals(['http://dartlang.org', 'foo', 'bar', 'baz'])); | |
| 308 expect(context.split('file:///foo/bar/baz'), | |
| 309 equals(['file://', 'foo', 'bar', 'baz'])); | |
| 310 expect(context.split('/foo/bar/baz'), equals(['/', 'foo', 'bar', 'baz'])); | |
| 311 expect(context.split('http://dartlang.org/'), | |
| 312 equals(['http://dartlang.org'])); | |
| 313 expect(context.split('http://dartlang.org'), | |
| 314 equals(['http://dartlang.org'])); | |
| 315 expect(context.split('file:///'), equals(['file://'])); | |
| 316 expect(context.split('file://'), equals(['file://'])); | |
| 317 expect(context.split('/'), equals(['/'])); | |
| 318 }); | |
| 319 }); | |
| 320 | |
| 321 group('normalize', () { | |
| 322 test('simple cases', () { | |
| 323 expect(context.normalize(''), '.'); | |
| 324 expect(context.normalize('.'), '.'); | |
| 325 expect(context.normalize('..'), '..'); | |
| 326 expect(context.normalize('a'), 'a'); | |
| 327 expect(context.normalize('http://dartlang.org/'), 'http://dartlang.org'); | |
| 328 expect(context.normalize('http://dartlang.org'), 'http://dartlang.org'); | |
| 329 expect(context.normalize('file://'), 'file://'); | |
| 330 expect(context.normalize('file:///'), 'file://'); | |
| 331 expect(context.normalize('/'), '/'); | |
| 332 expect(context.normalize(r'\'), r'\'); | |
| 333 expect(context.normalize('C:/'), 'C:'); | |
| 334 expect(context.normalize(r'C:\'), r'C:\'); | |
| 335 expect(context.normalize(r'\\'), r'\\'); | |
| 336 expect(context.normalize('a/./\xc5\u0bf8-;\u{1f085}\u{00}/c/d/../'), | |
| 337 'a/\xc5\u0bf8-;\u{1f085}\u{00}/c'); | |
| 338 }); | |
| 339 | |
| 340 test('collapses redundant separators', () { | |
| 341 expect(context.normalize(r'a/b/c'), r'a/b/c'); | |
| 342 expect(context.normalize(r'a//b///c////d'), r'a/b/c/d'); | |
| 343 }); | |
| 344 | |
| 345 test('does not collapse separators for other platform', () { | |
| 346 expect(context.normalize(r'a\\b\\\c'), r'a\\b\\\c'); | |
| 347 }); | |
| 348 | |
| 349 test('eliminates "." parts', () { | |
| 350 expect(context.normalize('./'), '.'); | |
| 351 expect(context.normalize('http://dartlang.org/.'), 'http://dartlang.org'); | |
| 352 expect(context.normalize('file:///.'), 'file://'); | |
| 353 expect(context.normalize('/.'), '/'); | |
| 354 expect(context.normalize('http://dartlang.org/./'), | |
| 355 'http://dartlang.org'); | |
| 356 expect(context.normalize('file:///./'), 'file://'); | |
| 357 expect(context.normalize('/./'), '/'); | |
| 358 expect(context.normalize('./.'), '.'); | |
| 359 expect(context.normalize('a/./b'), 'a/b'); | |
| 360 expect(context.normalize('a/.b/c'), 'a/.b/c'); | |
| 361 expect(context.normalize('a/././b/./c'), 'a/b/c'); | |
| 362 expect(context.normalize('././a'), 'a'); | |
| 363 expect(context.normalize('a/./.'), 'a'); | |
| 364 }); | |
| 365 | |
| 366 test('eliminates ".." parts', () { | |
| 367 expect(context.normalize('..'), '..'); | |
| 368 expect(context.normalize('../'), '..'); | |
| 369 expect(context.normalize('../../..'), '../../..'); | |
| 370 expect(context.normalize('../../../'), '../../..'); | |
| 371 expect(context.normalize('http://dartlang.org/..'), | |
| 372 'http://dartlang.org'); | |
| 373 expect(context.normalize('file:///..'), 'file://'); | |
| 374 expect(context.normalize('/..'), '/'); | |
| 375 expect(context.normalize('http://dartlang.org/../../..'), | |
| 376 'http://dartlang.org'); | |
| 377 expect(context.normalize('file:///../../..'), 'file://'); | |
| 378 expect(context.normalize('/../../..'), '/'); | |
| 379 expect(context.normalize('http://dartlang.org/../../../a'), | |
| 380 'http://dartlang.org/a'); | |
| 381 expect(context.normalize('file:///../../../a'), 'file:///a'); | |
| 382 expect(context.normalize('/../../../a'), '/a'); | |
| 383 expect(context.normalize('c:/..'), '.'); | |
| 384 expect(context.normalize('A:/../../..'), '../..'); | |
| 385 expect(context.normalize('a/..'), '.'); | |
| 386 expect(context.normalize('a/b/..'), 'a'); | |
| 387 expect(context.normalize('a/../b'), 'b'); | |
| 388 expect(context.normalize('a/./../b'), 'b'); | |
| 389 expect(context.normalize('a/b/c/../../d/e/..'), 'a/d'); | |
| 390 expect(context.normalize('a/b/../../../../c'), '../../c'); | |
| 391 expect(context.normalize('z/a/b/../../..\../c'), 'z/..\../c'); | |
| 392 expect(context.normalize('a/b\c/../d'), 'a/d'); | |
| 393 }); | |
| 394 | |
| 395 test('does not walk before root on absolute paths', () { | |
| 396 expect(context.normalize('..'), '..'); | |
| 397 expect(context.normalize('../'), '..'); | |
| 398 expect(context.normalize('http://dartlang.org/..'), | |
| 399 'http://dartlang.org'); | |
| 400 expect(context.normalize('http://dartlang.org/../a'), | |
| 401 'http://dartlang.org/a'); | |
| 402 expect(context.normalize('file:///..'), 'file://'); | |
| 403 expect(context.normalize('file:///../a'), 'file:///a'); | |
| 404 expect(context.normalize('/..'), '/'); | |
| 405 expect(context.normalize('a/..'), '.'); | |
| 406 expect(context.normalize('../a'), '../a'); | |
| 407 expect(context.normalize('/../a'), '/a'); | |
| 408 expect(context.normalize('c:/../a'), 'a'); | |
| 409 expect(context.normalize('/../a'), '/a'); | |
| 410 expect(context.normalize('a/b/..'), 'a'); | |
| 411 expect(context.normalize('../a/b/..'), '../a'); | |
| 412 expect(context.normalize('a/../b'), 'b'); | |
| 413 expect(context.normalize('a/./../b'), 'b'); | |
| 414 expect(context.normalize('a/b/c/../../d/e/..'), 'a/d'); | |
| 415 expect(context.normalize('a/b/../../../../c'), '../../c'); | |
| 416 expect(context.normalize('a/b/c/../../..d/./.e/f././'), 'a/..d/.e/f.'); | |
| 417 }); | |
| 418 | |
| 419 test('removes trailing separators', () { | |
| 420 expect(context.normalize('./'), '.'); | |
| 421 expect(context.normalize('.//'), '.'); | |
| 422 expect(context.normalize('a/'), 'a'); | |
| 423 expect(context.normalize('a/b/'), 'a/b'); | |
| 424 expect(context.normalize(r'a/b\'), r'a/b\'); | |
| 425 expect(context.normalize('a/b///'), 'a/b'); | |
| 426 }); | |
| 427 }); | |
| 428 | |
| 429 group('relative', () { | |
| 430 group('from absolute root', () { | |
| 431 test('given absolute path in root', () { | |
| 432 expect(context.relative('http://dartlang.org'), '../..'); | |
| 433 expect(context.relative('http://dartlang.org/'), '../..'); | |
| 434 expect(context.relative('/'), '../..'); | |
| 435 expect(context.relative('http://dartlang.org/root'), '..'); | |
| 436 expect(context.relative('/root'), '..'); | |
| 437 expect(context.relative('http://dartlang.org/root/path'), '.'); | |
| 438 expect(context.relative('/root/path'), '.'); | |
| 439 expect(context.relative('http://dartlang.org/root/path/a'), 'a'); | |
| 440 expect(context.relative('/root/path/a'), 'a'); | |
| 441 expect(context.relative('http://dartlang.org/root/path/a/b.txt'), | |
| 442 'a/b.txt'); | |
| 443 expect(context.relative('/root/path/a/b.txt'), 'a/b.txt'); | |
| 444 expect(context.relative('http://dartlang.org/root/a/b.txt'), | |
| 445 '../a/b.txt'); | |
| 446 expect(context.relative('/root/a/b.txt'), '../a/b.txt'); | |
| 447 }); | |
| 448 | |
| 449 test('given absolute path outside of root', () { | |
| 450 expect(context.relative('http://dartlang.org/a/b'), '../../a/b'); | |
| 451 expect(context.relative('/a/b'), '../../a/b'); | |
| 452 expect(context.relative('http://dartlang.org/root/path/a'), 'a'); | |
| 453 expect(context.relative('/root/path/a'), 'a'); | |
| 454 expect(context.relative('http://dartlang.org/root/path/a/b.txt'), | |
| 455 'a/b.txt'); | |
| 456 expect(context.relative('http://dartlang.org/root/path/a/b.txt'), | |
| 457 'a/b.txt'); | |
| 458 expect(context.relative('http://dartlang.org/root/a/b.txt'), | |
| 459 '../a/b.txt'); | |
| 460 }); | |
| 461 | |
| 462 test('given absolute path with different hostname/protocol', () { | |
| 463 expect(context.relative(r'http://google.com/a/b'), | |
| 464 r'http://google.com/a/b'); | |
| 465 expect(context.relative(r'file:///a/b'), | |
| 466 r'file:///a/b'); | |
| 467 }); | |
| 468 | |
| 469 test('given relative path', () { | |
| 470 // The path is considered relative to the root, so it basically just | |
| 471 // normalizes. | |
| 472 expect(context.relative(''), '.'); | |
| 473 expect(context.relative('.'), '.'); | |
| 474 expect(context.relative('a'), 'a'); | |
| 475 expect(context.relative('a/b.txt'), 'a/b.txt'); | |
| 476 expect(context.relative('../a/b.txt'), '../a/b.txt'); | |
| 477 expect(context.relative('a/./b/../c.txt'), 'a/c.txt'); | |
| 478 }); | |
| 479 | |
| 480 // Regression | |
| 481 test('from root-only path', () { | |
| 482 expect(context.relative('http://dartlang.org', | |
| 483 from: 'http://dartlang.org'), | |
| 484 '.'); | |
| 485 expect(context.relative('http://dartlang.org/root/path', | |
| 486 from: 'http://dartlang.org'), | |
| 487 'root/path'); | |
| 488 }); | |
| 489 }); | |
| 490 | |
| 491 group('from relative root', () { | |
| 492 var r = new path.Context(style: path.Style.url, current: 'foo/bar'); | |
| 493 | |
| 494 test('given absolute path', () { | |
| 495 expect(r.relative('http://google.com/'), equals('http://google.com')); | |
| 496 expect(r.relative('http://google.com'), equals('http://google.com')); | |
| 497 expect(r.relative('file:///'), equals('file://')); | |
| 498 expect(r.relative('file://'), equals('file://')); | |
| 499 expect(r.relative('/'), equals('/')); | |
| 500 expect(r.relative('/a/b'), equals('/a/b')); | |
| 501 }); | |
| 502 | |
| 503 test('given relative path', () { | |
| 504 // The path is considered relative to the root, so it basically just | |
| 505 // normalizes. | |
| 506 expect(r.relative(''), '.'); | |
| 507 expect(r.relative('.'), '.'); | |
| 508 expect(r.relative('..'), '..'); | |
| 509 expect(r.relative('a'), 'a'); | |
| 510 expect(r.relative('a/b.txt'), 'a/b.txt'); | |
| 511 expect(r.relative('../a/b.txt'), '../a/b.txt'); | |
| 512 expect(r.relative('a/./b/../c.txt'), 'a/c.txt'); | |
| 513 }); | |
| 514 }); | |
| 515 | |
| 516 group('from root-relative root', () { | |
| 517 var r = new path.Context(style: path.Style.url, current: '/foo/bar'); | |
| 518 | |
| 519 test('given absolute path', () { | |
| 520 expect(r.relative('http://google.com/'), equals('http://google.com')); | |
| 521 expect(r.relative('http://google.com'), equals('http://google.com')); | |
| 522 expect(r.relative('file:///'), equals('file://')); | |
| 523 expect(r.relative('file://'), equals('file://')); | |
| 524 expect(r.relative('/'), equals('../..')); | |
| 525 expect(r.relative('/a/b'), equals('../../a/b')); | |
| 526 }); | |
| 527 | |
| 528 test('given relative path', () { | |
| 529 // The path is considered relative to the root, so it basically just | |
| 530 // normalizes. | |
| 531 expect(r.relative(''), '.'); | |
| 532 expect(r.relative('.'), '.'); | |
| 533 expect(r.relative('..'), '..'); | |
| 534 expect(r.relative('a'), 'a'); | |
| 535 expect(r.relative('a/b.txt'), 'a/b.txt'); | |
| 536 expect(r.relative('../a/b.txt'), '../a/b.txt'); | |
| 537 expect(r.relative('a/./b/../c.txt'), 'a/c.txt'); | |
| 538 }); | |
| 539 }); | |
| 540 | |
| 541 test('from a root with extension', () { | |
| 542 var r = new path.Context(style: path.Style.url, current: '/dir.ext'); | |
| 543 expect(r.relative('/dir.ext/file'), 'file'); | |
| 544 }); | |
| 545 | |
| 546 test('with a root parameter', () { | |
| 547 expect(context.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz')); | |
| 548 expect( | |
| 549 context.relative('/foo/bar/baz', from: 'http://dartlang.org/foo/bar'), | |
| 550 equals('baz')); | |
| 551 expect( | |
| 552 context.relative('http://dartlang.org/foo/bar/baz', from: '/foo/bar'), | |
| 553 equals('baz')); | |
| 554 expect(context.relative('http://dartlang.org/foo/bar/baz', | |
| 555 from: 'file:///foo/bar'), | |
| 556 equals('http://dartlang.org/foo/bar/baz')); | |
| 557 expect(context.relative('http://dartlang.org/foo/bar/baz', | |
| 558 from: 'http://dartlang.org/foo/bar'), equals('baz')); | |
| 559 expect( | |
| 560 context.relative('/foo/bar/baz', from: 'file:///foo/bar'), | |
| 561 equals('http://dartlang.org/foo/bar/baz')); | |
| 562 expect( | |
| 563 context.relative('file:///foo/bar/baz', from: '/foo/bar'), | |
| 564 equals('file:///foo/bar/baz')); | |
| 565 | |
| 566 expect(context.relative('..', from: '/foo/bar'), equals('../../root')); | |
| 567 expect(context.relative('..', from: 'http://dartlang.org/foo/bar'), | |
| 568 equals('../../root')); | |
| 569 expect(context.relative('..', from: 'file:///foo/bar'), | |
| 570 equals('http://dartlang.org/root')); | |
| 571 expect(context.relative('..', from: '/foo/bar'), equals('../../root')); | |
| 572 | |
| 573 expect(context.relative('http://dartlang.org/foo/bar/baz', | |
| 574 from: 'foo/bar'), | |
| 575 equals('../../../../foo/bar/baz')); | |
| 576 expect(context.relative('file:///foo/bar/baz', from: 'foo/bar'), | |
| 577 equals('file:///foo/bar/baz')); | |
| 578 expect(context.relative('/foo/bar/baz', from: 'foo/bar'), | |
| 579 equals('../../../../foo/bar/baz')); | |
| 580 | |
| 581 expect(context.relative('..', from: 'foo/bar'), equals('../../..')); | |
| 582 }); | |
| 583 | |
| 584 test('with a root parameter and a relative root', () { | |
| 585 var r = new path.Context(style: path.Style.url, current: 'relative/root'); | |
| 586 expect(r.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz')); | |
| 587 expect( | |
| 588 r.relative('/foo/bar/baz', from: 'http://dartlang.org/foo/bar'), | |
| 589 equals('/foo/bar/baz')); | |
| 590 expect( | |
| 591 r.relative('http://dartlang.org/foo/bar/baz', from: '/foo/bar'), | |
| 592 equals('http://dartlang.org/foo/bar/baz')); | |
| 593 expect(r.relative('http://dartlang.org/foo/bar/baz', | |
| 594 from: 'file:///foo/bar'), | |
| 595 equals('http://dartlang.org/foo/bar/baz')); | |
| 596 expect(r.relative('http://dartlang.org/foo/bar/baz', | |
| 597 from: 'http://dartlang.org/foo/bar'), equals('baz')); | |
| 598 | |
| 599 expect(r.relative('http://dartlang.org/foo/bar/baz', from: 'foo/bar'), | |
| 600 equals('http://dartlang.org/foo/bar/baz')); | |
| 601 expect(r.relative('file:///foo/bar/baz', from: 'foo/bar'), | |
| 602 equals('file:///foo/bar/baz')); | |
| 603 expect(r.relative('/foo/bar/baz', from: 'foo/bar'), | |
| 604 equals('/foo/bar/baz')); | |
| 605 | |
| 606 expect(r.relative('..', from: 'foo/bar'), equals('../../..')); | |
| 607 }); | |
| 608 | |
| 609 test('from a . root', () { | |
| 610 var r = new path.Context(style: path.Style.url, current: '.'); | |
| 611 expect(r.relative('http://dartlang.org/foo/bar/baz'), | |
| 612 equals('http://dartlang.org/foo/bar/baz')); | |
| 613 expect(r.relative('file:///foo/bar/baz'), equals('file:///foo/bar/baz')); | |
| 614 expect(r.relative('/foo/bar/baz'), equals('/foo/bar/baz')); | |
| 615 expect(r.relative('foo/bar/baz'), equals('foo/bar/baz')); | |
| 616 }); | |
| 617 }); | |
| 618 | |
| 619 group('isWithin', () { | |
| 620 test('simple cases', () { | |
| 621 expect(context.isWithin('foo/bar', 'foo/bar'), isFalse); | |
| 622 expect(context.isWithin('foo/bar', 'foo/bar/baz'), isTrue); | |
| 623 expect(context.isWithin('foo/bar', 'foo/baz'), isFalse); | |
| 624 expect(context.isWithin('foo/bar', '../path/foo/bar/baz'), isTrue); | |
| 625 expect(context.isWithin( | |
| 626 'http://dartlang.org', 'http://dartlang.org/foo/bar'), | |
| 627 isTrue); | |
| 628 expect(context.isWithin( | |
| 629 'http://dartlang.org', 'http://pub.dartlang.org/foo/bar'), | |
| 630 isFalse); | |
| 631 expect(context.isWithin('http://dartlang.org', '/foo/bar'), isTrue); | |
| 632 expect(context.isWithin('http://dartlang.org/foo', '/foo/bar'), isTrue); | |
| 633 expect(context.isWithin('http://dartlang.org/foo', '/bar/baz'), isFalse); | |
| 634 expect(context.isWithin('baz', 'http://dartlang.org/root/path/baz/bang'), | |
| 635 isTrue); | |
| 636 expect(context.isWithin('baz', 'http://dartlang.org/root/path/bang/baz'), | |
| 637 isFalse); | |
| 638 }); | |
| 639 | |
| 640 test('from a relative root', () { | |
| 641 var r = new path.Context(style: path.Style.url, current: 'foo/bar'); | |
| 642 expect(context.isWithin('.', 'a/b/c'), isTrue); | |
| 643 expect(context.isWithin('.', '../a/b/c'), isFalse); | |
| 644 expect(context.isWithin('.', '../../a/foo/b/c'), isFalse); | |
| 645 expect(context.isWithin( | |
| 646 'http://dartlang.org/', 'http://dartlang.org/baz/bang'), | |
| 647 isTrue); | |
| 648 expect(context.isWithin('.', 'http://dartlang.org/baz/bang'), isFalse); | |
| 649 }); | |
| 650 }); | |
| 651 | |
| 652 group('absolute', () { | |
| 653 test('allows up to seven parts', () { | |
| 654 expect(context.absolute('a'), 'http://dartlang.org/root/path/a'); | |
| 655 expect(context.absolute('a', 'b'), 'http://dartlang.org/root/path/a/b'); | |
| 656 expect(context.absolute('a', 'b', 'c'), | |
| 657 'http://dartlang.org/root/path/a/b/c'); | |
| 658 expect(context.absolute('a', 'b', 'c', 'd'), | |
| 659 'http://dartlang.org/root/path/a/b/c/d'); | |
| 660 expect(context.absolute('a', 'b', 'c', 'd', 'e'), | |
| 661 'http://dartlang.org/root/path/a/b/c/d/e'); | |
| 662 expect(context.absolute('a', 'b', 'c', 'd', 'e', 'f'), | |
| 663 'http://dartlang.org/root/path/a/b/c/d/e/f'); | |
| 664 expect(context.absolute('a', 'b', 'c', 'd', 'e', 'f', 'g'), | |
| 665 'http://dartlang.org/root/path/a/b/c/d/e/f/g'); | |
| 666 }); | |
| 667 | |
| 668 test('does not add separator if a part ends in one', () { | |
| 669 expect(context.absolute('a/', 'b', 'c/', 'd'), | |
| 670 'http://dartlang.org/root/path/a/b/c/d'); | |
| 671 expect(context.absolute(r'a\', 'b'), | |
| 672 r'http://dartlang.org/root/path/a\/b'); | |
| 673 }); | |
| 674 | |
| 675 test('ignores parts before an absolute path', () { | |
| 676 expect(context.absolute('a', '/b', '/c', 'd'), 'http://dartlang.org/c/d'); | |
| 677 expect(context.absolute('a', '/b', 'file:///c', 'd'), 'file:///c/d'); | |
| 678 expect(context.absolute('a', r'c:\b', 'c', 'd'), | |
| 679 r'http://dartlang.org/root/path/a/c:\b/c/d'); | |
| 680 expect(context.absolute('a', r'\\b', 'c', 'd'), | |
| 681 r'http://dartlang.org/root/path/a/\\b/c/d'); | |
| 682 }); | |
| 683 }); | |
| 684 | |
| 685 test('withoutExtension', () { | |
| 686 expect(context.withoutExtension(''), ''); | |
| 687 expect(context.withoutExtension('a'), 'a'); | |
| 688 expect(context.withoutExtension('.a'), '.a'); | |
| 689 expect(context.withoutExtension('a.b'), 'a'); | |
| 690 expect(context.withoutExtension('a/b.c'), 'a/b'); | |
| 691 expect(context.withoutExtension('a/b.c.d'), 'a/b.c'); | |
| 692 expect(context.withoutExtension('a/'), 'a/'); | |
| 693 expect(context.withoutExtension('a/b/'), 'a/b/'); | |
| 694 expect(context.withoutExtension('a/.'), 'a/.'); | |
| 695 expect(context.withoutExtension('a/.b'), 'a/.b'); | |
| 696 expect(context.withoutExtension('a.b/c'), 'a.b/c'); | |
| 697 expect(context.withoutExtension(r'a.b\c'), r'a'); | |
| 698 expect(context.withoutExtension(r'a/b\c'), r'a/b\c'); | |
| 699 expect(context.withoutExtension(r'a/b\c.d'), r'a/b\c'); | |
| 700 expect(context.withoutExtension('a/b.c/'), 'a/b/'); | |
| 701 expect(context.withoutExtension('a/b.c//'), 'a/b//'); | |
| 702 }); | |
| 703 | |
| 704 group('fromUri', () { | |
| 705 test('with a URI', () { | |
| 706 expect(context.fromUri(Uri.parse('http://dartlang.org/path/to/foo')), | |
| 707 'http://dartlang.org/path/to/foo'); | |
| 708 expect(context.fromUri(Uri.parse('http://dartlang.org/path/to/foo/')), | |
| 709 'http://dartlang.org/path/to/foo/'); | |
| 710 expect(context.fromUri(Uri.parse('file:///path/to/foo')), | |
| 711 'file:///path/to/foo'); | |
| 712 expect(context.fromUri(Uri.parse('foo/bar')), 'foo/bar'); | |
| 713 expect(context.fromUri( | |
| 714 Uri.parse('http://dartlang.org/path/to/foo%23bar')), | |
| 715 'http://dartlang.org/path/to/foo%23bar'); | |
| 716 // Since the resulting "path" is also a URL, special characters should | |
| 717 // remain percent-encoded in the result. | |
| 718 expect(context.fromUri(Uri.parse('_%7B_%7D_%60_%5E_%20_%22_%25_')), | |
| 719 r'_%7B_%7D_%60_%5E_%20_%22_%25_'); | |
| 720 }); | |
| 721 | |
| 722 test('with a string', () { | |
| 723 expect(context.fromUri('http://dartlang.org/path/to/foo'), | |
| 724 'http://dartlang.org/path/to/foo'); | |
| 725 }); | |
| 726 }); | |
| 727 | |
| 728 test('toUri', () { | |
| 729 expect(context.toUri('http://dartlang.org/path/to/foo'), | |
| 730 Uri.parse('http://dartlang.org/path/to/foo')); | |
| 731 expect(context.toUri('http://dartlang.org/path/to/foo/'), | |
| 732 Uri.parse('http://dartlang.org/path/to/foo/')); | |
| 733 expect(context.toUri('file:///path/to/foo'), | |
| 734 Uri.parse('file:///path/to/foo')); | |
| 735 expect(context.toUri('foo/bar'), Uri.parse('foo/bar')); | |
| 736 expect(context.toUri('http://dartlang.org/path/to/foo%23bar'), | |
| 737 Uri.parse('http://dartlang.org/path/to/foo%23bar')); | |
| 738 // Since the input path is also a URI, special characters should already | |
| 739 // be percent encoded there too. | |
| 740 expect(context.toUri(r'http://foo.com/_%7B_%7D_%60_%5E_%20_%22_%25_'), | |
| 741 Uri.parse('http://foo.com/_%7B_%7D_%60_%5E_%20_%22_%25_')); | |
| 742 expect(context.toUri(r'_%7B_%7D_%60_%5E_%20_%22_%25_'), | |
| 743 Uri.parse('_%7B_%7D_%60_%5E_%20_%22_%25_')); | |
| 744 }); | |
| 745 | |
| 746 group('prettyUri', () { | |
| 747 test('with a file: URI', () { | |
| 748 expect(context.prettyUri(Uri.parse('file:///root/path/a/b')), | |
| 749 'file:///root/path/a/b'); | |
| 750 }); | |
| 751 | |
| 752 test('with an http: URI', () { | |
| 753 expect(context.prettyUri('http://dartlang.org/root/path/a/b'), 'a/b'); | |
| 754 expect(context.prettyUri('http://dartlang.org/root/path/a/../b'), 'b'); | |
| 755 expect(context.prettyUri('http://dartlang.org/other/path/a/b'), | |
| 756 'http://dartlang.org/other/path/a/b'); | |
| 757 expect(context.prettyUri('http://pub.dartlang.org/root/path'), | |
| 758 'http://pub.dartlang.org/root/path'); | |
| 759 expect(context.prettyUri('http://dartlang.org/root/other'), | |
| 760 '../other'); | |
| 761 }); | |
| 762 | |
| 763 test('with a relative URI', () { | |
| 764 expect(context.prettyUri('a/b'), 'a/b'); | |
| 765 }); | |
| 766 | |
| 767 test('with a root-relative URI', () { | |
| 768 expect(context.prettyUri('/a/b'), '/a/b'); | |
| 769 }); | |
| 770 | |
| 771 test('with a Uri object', () { | |
| 772 expect(context.prettyUri(Uri.parse('a/b')), 'a/b'); | |
| 773 }); | |
| 774 }); | |
| 775 } | |
| OLD | NEW |