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