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.Builder, on all platforms. | |
6 | |
7 import "package:unittest/unittest.dart"; | |
8 import "package:path/path.dart" as path; | |
9 | |
10 void main() { | |
11 test("test relative", testRelativeTo); | |
12 } | |
13 | |
14 | |
Bob Nystrom
2013/07/18 20:45:08
Nit: just a single blank line between functions.
| |
15 void testRelativeTo() { | |
Bob Nystrom
2013/07/18 20:45:08
Instead of a named function, how about just inlini
Bill Hesse
2013/07/22 11:35:22
Done.
| |
16 relativeTest(new path.Builder(style: path.Style.posix, root: '.'), ''); | |
17 relativeTest(new path.Builder(style: path.Style.posix, root: '/'), ''); | |
18 relativeTest(new path.Builder(style: path.Style.windows, root: 'd:/'), 'c:'); | |
19 relativeTest(new path.Builder(style: path.Style.windows, root: '.'), 'c:'); | |
20 relativeTest(new path.Builder(style: path.Style.url, root: 'file:///'), | |
21 'http://myserver'); | |
22 relativeTest(new path.Builder(style: path.Style.url, root: '.'), | |
23 'http://myserver'); | |
24 relativeTest(new path.Builder(style: path.Style.url, root: 'file:///'), | |
25 ''); | |
Bob Nystrom
2013/07/18 20:45:08
Nit: this can go on the previous line.
| |
26 relativeTest(new path.Builder(style: path.Style.url, root: '.'), | |
27 ''); | |
Bob Nystrom
2013/07/18 20:45:08
This too.
| |
28 } | |
29 | |
30 | |
31 void relativeTest(path.Builder builder, String absolutePrefix) { | |
32 var m = absolutePrefix; | |
Bob Nystrom
2013/07/18 20:45:08
How about "prefix"? That should still keep the tes
Bill Hesse
2013/07/22 11:35:22
Done.
| |
33 var isRelative = (builder.root == '.'); | |
34 // Cases where the arguments are absolute paths. | |
35 void test(result, p, from) { | |
Bob Nystrom
2013/07/18 20:45:08
Nit: ditch the "void".
| |
36 expect(builder.normalize(result), builder.relative(p, from: from)); | |
37 } | |
38 | |
39 test('c/d', '$m/a/b/c/d', '$m/a/b'); | |
40 test('c/d', '$m/a/b/c/d', '$m/a/b/'); | |
41 test('.', '$m/a', '$m/a'); | |
42 // Trailing slashes in the inputs have no effect. | |
43 test('../../z/x/y', '$m/a/b/z/x/y', '$m/a/b/c/d/'); | |
44 test('../../z/x/y', '$m/a/b/z/x/y', '$m/a/b/c/d'); | |
45 test('../../z/x/y', '$m/a/b/z/x/y/', '$m/a/b/c/d'); | |
46 test('../../../z/x/y', '$m/z/x/y', '$m/a/b/c'); | |
47 test('../../../z/x/y', '$m/z/x/y', '$m/a/b/c/'); | |
48 // Cases where the arguments are relative paths. | |
Bob Nystrom
2013/07/18 20:45:08
Use a ":" here or a "." on the comment below.
Bill Hesse
2013/07/22 11:35:22
Done.
| |
49 test('c/d', 'a/b/c/d', 'a/b'); | |
50 test('.', 'a/b/c', 'a/b/c'); | |
51 test('.', 'a/d/../b/c', 'a/b/c/'); | |
52 test('.', '', ''); | |
53 test('.', '.', ''); | |
54 test('.', '', '.'); | |
55 test('.', '.', '.'); | |
56 test('.', '..', '..'); | |
57 if (isRelative) test('..', '..', '.'); | |
58 test('a', 'a', ''); | |
59 test('a', 'a', '.'); | |
60 test('..', '.', 'a'); | |
61 test('.', 'a/b/f/../c', 'a/e/../b/c'); | |
62 test('d', 'a/b/f/../c/d', 'a/e/../b/c'); | |
63 test('..', 'a/b/f/../c', 'a/e/../b/c/e/'); | |
64 test('../..', '', 'a/b/'); | |
65 if (isRelative) test('../../..', '..', 'a/b/'); | |
66 test('../b/c/d', 'b/c/d/', 'a/'); | |
67 test('../a/b/c', 'x/y/a//b/./f/../c', 'x//y/z'); | |
68 | |
69 // Case where base is a substring of relative: | |
70 test('a/b', '$m/x/y//a/b', '$m/x/y/'); | |
71 test('a/b', 'x/y//a/b', 'x/y/'); | |
72 test('../ya/b', '$m/x/ya/b', '$m/x/y'); | |
73 test('../ya/b', 'x/ya/b', 'x/y'); | |
74 test('../b', 'x/y/../b', 'x/y/.'); | |
75 test('a/b/c', 'x/y/a//b/./f/../c', 'x/y'); | |
76 test('.', '$m/x/y//', '$m/x/y/'); | |
77 test('.', '$m/x/y/', '$m/x/y'); | |
78 | |
79 // Should always throw - no relative path can be constructed. | |
80 if (isRelative) { | |
81 expect(() => builder.relative('.', from: '..'), throws); | |
82 expect(() => builder.relative('a/b', from: '../../d'), throws); | |
83 expect(() => builder.relative('a/b', from: '$m/a/b'), throws); | |
Bob Nystrom
2013/07/18 20:45:08
I'm still not sure about these. My intuition is th
Bill Hesse
2013/07/22 11:35:22
This is for the case where cwd is '.'. When the u
Bob Nystrom
2013/07/22 20:53:11
Ah, that makes more sense now. If there are cases
Bill Hesse
2013/07/23 16:07:00
Done.
Re the documentation, I just noticed, in lo
nweiz
2013/07/23 19:31:06
I think it's implicit that [path] may be relative
| |
84 // An absolute path relative from a relative path returns the absolute path. | |
85 test('$m/a/b', '$m/a/b', 'c/d'); | |
86 } | |
87 } | |
OLD | NEW |