| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, 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 // Test "relative" on all styles of path.Context, on all platforms. | |
| 6 | |
| 7 import "package:unittest/unittest.dart"; | |
| 8 import "package:path/path.dart" as path; | |
| 9 | |
| 10 import "utils.dart"; | |
| 11 | |
| 12 void main() { | |
| 13 test("test relative", () { | |
| 14 relativeTest(new path.Context(style: path.Style.posix, current: '.'), '/'); | |
| 15 relativeTest(new path.Context(style: path.Style.posix, current: '/'), '/'); | |
| 16 relativeTest(new path.Context(style: path.Style.windows, current: r'd:\'), | |
| 17 r'c:\'); | |
| 18 relativeTest(new path.Context(style: path.Style.windows, current: '.'), | |
| 19 r'c:\'); | |
| 20 relativeTest(new path.Context(style: path.Style.url, current: 'file:///'), | |
| 21 'http://myserver/'); | |
| 22 relativeTest(new path.Context(style: path.Style.url, current: '.'), | |
| 23 'http://myserver/'); | |
| 24 relativeTest(new path.Context(style: path.Style.url, current: 'file:///'), | |
| 25 '/'); | |
| 26 relativeTest(new path.Context(style: path.Style.url, current: '.'), '/'); | |
| 27 }); | |
| 28 } | |
| 29 | |
| 30 void relativeTest(path.Context context, String prefix) { | |
| 31 var isRelative = (context.current == '.'); | |
| 32 // Cases where the arguments are absolute paths. | |
| 33 expectRelative(result, pathArg, fromArg) { | |
| 34 expect(context.normalize(result), context.relative(pathArg, from: fromArg)); | |
| 35 } | |
| 36 | |
| 37 expectRelative('c/d', '${prefix}a/b/c/d', '${prefix}a/b'); | |
| 38 expectRelative('c/d', '${prefix}a/b/c/d', '${prefix}a/b/'); | |
| 39 expectRelative('.', '${prefix}a', '${prefix}a'); | |
| 40 // Trailing slashes in the inputs have no effect. | |
| 41 expectRelative('../../z/x/y', '${prefix}a/b/z/x/y', '${prefix}a/b/c/d/'); | |
| 42 expectRelative('../../z/x/y', '${prefix}a/b/z/x/y', '${prefix}a/b/c/d'); | |
| 43 expectRelative('../../z/x/y', '${prefix}a/b/z/x/y/', '${prefix}a/b/c/d'); | |
| 44 expectRelative('../../../z/x/y', '${prefix}z/x/y', '${prefix}a/b/c'); | |
| 45 expectRelative('../../../z/x/y', '${prefix}z/x/y', '${prefix}a/b/c/'); | |
| 46 | |
| 47 // Cases where the arguments are relative paths. | |
| 48 expectRelative('c/d', 'a/b/c/d', 'a/b'); | |
| 49 expectRelative('.', 'a/b/c', 'a/b/c'); | |
| 50 expectRelative('.', 'a/d/../b/c', 'a/b/c/'); | |
| 51 expectRelative('.', '', ''); | |
| 52 expectRelative('.', '.', ''); | |
| 53 expectRelative('.', '', '.'); | |
| 54 expectRelative('.', '.', '.'); | |
| 55 expectRelative('.', '..', '..'); | |
| 56 if (isRelative) expectRelative('..', '..', '.'); | |
| 57 expectRelative('a', 'a', ''); | |
| 58 expectRelative('a', 'a', '.'); | |
| 59 expectRelative('..', '.', 'a'); | |
| 60 expectRelative('.', 'a/b/f/../c', 'a/e/../b/c'); | |
| 61 expectRelative('d', 'a/b/f/../c/d', 'a/e/../b/c'); | |
| 62 expectRelative('..', 'a/b/f/../c', 'a/e/../b/c/e/'); | |
| 63 expectRelative('../..', '', 'a/b/'); | |
| 64 if (isRelative) expectRelative('../../..', '..', 'a/b/'); | |
| 65 expectRelative('../b/c/d', 'b/c/d/', 'a/'); | |
| 66 expectRelative('../a/b/c', 'x/y/a//b/./f/../c', 'x//y/z'); | |
| 67 | |
| 68 // Case where from is an exact substring of path. | |
| 69 expectRelative('a/b', '${prefix}x/y//a/b', '${prefix}x/y/'); | |
| 70 expectRelative('a/b', 'x/y//a/b', 'x/y/'); | |
| 71 expectRelative('../ya/b', '${prefix}x/ya/b', '${prefix}x/y'); | |
| 72 expectRelative('../ya/b', 'x/ya/b', 'x/y'); | |
| 73 expectRelative('../b', 'x/y/../b', 'x/y/.'); | |
| 74 expectRelative('a/b/c', 'x/y/a//b/./f/../c', 'x/y'); | |
| 75 expectRelative('.', '${prefix}x/y//', '${prefix}x/y/'); | |
| 76 expectRelative('.', '${prefix}x/y/', '${prefix}x/y'); | |
| 77 | |
| 78 // Should always throw - no relative path can be constructed. | |
| 79 if (isRelative) { | |
| 80 expect(() => context.relative('.', from: '..'), throwsPathException); | |
| 81 expect(() => context.relative('a/b', from: '../../d'), | |
| 82 throwsPathException); | |
| 83 expect(() => context.relative('a/b', from: '${prefix}a/b'), | |
| 84 throwsPathException); | |
| 85 // An absolute path relative from a relative path returns the absolute path. | |
| 86 expectRelative('${prefix}a/b', '${prefix}a/b', 'c/d'); | |
| 87 } | |
| 88 } | |
| OLD | NEW |