Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: tests/standalone/io/path_test.dart

Issue 10638002: Add methods to dart:io Path. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove pathWithoutExtension from path_test.dart. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/path_impl.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 // 4 //
5 // Test the Path class in dart:io. 5 // Test the Path class in dart:io.
6 6
7 #import("dart:io"); 7 #import("dart:io");
8 8
9 void main() { 9 void main() {
10 testBaseFunctions(); 10 testBaseFunctions();
11 testCanonicalize(); 11 testCanonicalize();
12 testJoinAppend();
12 } 13 }
13 14
14 void testBaseFunctions() { 15 void testBaseFunctions() {
15 testGetters(new Path("/foo/bar/fisk.hest"), 16 testGetters(new Path("/foo/bar/fisk.hest"),
16 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'], 17 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'],
17 'absolute canonical'); 18 'absolute canonical');
18 testGetters(new Path("/foo/bar/fisk.hest"),
19 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'],
20 'absolute canonical');
21 testGetters(new Path("/foo/bar/fisk.hest"),
22 ['/foo/bar', 'fisk.hest', 'fisk', 'hest'],
23 'absolute canonical');
24 testGetters(new Path(''), 19 testGetters(new Path(''),
25 ['', '', '', ''], 20 ['', '', '', ''],
26 'empty'); 21 'empty');
27 // This corner case leaves a trailing slash for the root. 22 // This corner case leaves a trailing slash for the root.
28 testGetters(new Path('/'), 23 testGetters(new Path('/'),
29 ['/', '', '', ''], 24 ['/', '', '', ''],
30 'absolute directory canonical trailing'); 25 'absolute canonical trailing');
31 testGetters(new Path('.'), 26 testGetters(new Path('.'),
32 ['', '.', '', ''], 27 ['', '.', '.', ''],
33 'canonical'); 28 'canonical');
29 testGetters(new Path('..'),
30 ['', '..', '..', ''],
31 'canonical');
34 testGetters(new Path('/ab,;- .c.d'), 32 testGetters(new Path('/ab,;- .c.d'),
35 ['/', 'ab,;- .c.d', 'ab,;- .c', 'd'], 33 ['/', 'ab,;- .c.d', 'ab,;- .c', 'd'],
36 'absolute directory canonical'); 34 'absolute canonical');
37 35
38 // Canonical and non-canonical cases 36 // Canonical and non-canonical cases
39 testGetters(new Path("a/b/../c/./d/e"), 37 testGetters(new Path("a/b/../c/./d/e"),
40 ['a/b/../c/./d', 'e', 'e', ''], 38 ['a/b/../c/./d', 'e', 'e', ''],
41 ''); 39 '');
42 testGetters(new Path("a./b../..c/.d/e"), 40 testGetters(new Path("a./b../..c/.d/e"),
43 ['a./b../..c/.d', 'e', 'e', ''], 41 ['a./b../..c/.d', 'e', 'e', ''],
44 'canonical'); 42 'canonical');
45 // .. is allowed at the beginning of a canonical relative path. 43 // .. is allowed at the beginning of a canonical relative path.
46 testGetters(new Path("../../a/b/c/d/"), 44 testGetters(new Path("../../a/b/c/d/"),
47 ['../../a/b/c/d', '', '', ''], 45 ['../../a/b/c/d', '', '', ''],
48 'canonical directory trailing'); 46 'canonical trailing');
47
48 // '.' at the end of a path is not considered an extension.
49 testGetters(new Path("a/b.c/."),
50 ['a/b.c', '.', '.', ''],
51 '');
52 // '..' at the end of a path is not considered an extension.
53 testGetters(new Path("a/bc/../.."),
54 ['a/bc/..', '..', '..', ''],
55 '');
49 56
50 // Test the special path cleaning operations on the Windows platform. 57 // Test the special path cleaning operations on the Windows platform.
51 if (Platform.operatingSystem == 'windows') { 58 if (Platform.operatingSystem == 'windows') {
52 testGetters(new Path.fromNative(@"c:\foo\bar\fisk.hest"), 59 testGetters(new Path.fromNative(@"c:\foo\bar\fisk.hest"),
53 ['/c:/foo/bar', 'fisk.hest', 'fisk', 'hest'], 60 ['/c:/foo/bar', 'fisk.hest', 'fisk', 'hest'],
54 'absolute canonical'); 61 'absolute canonical');
55 testGetters(new Path.fromNative("\\foo\\bar\\"), 62 testGetters(new Path.fromNative("\\foo\\bar\\"),
56 ['/foo/bar', '', '', ''], 63 ['/foo/bar', '', '', ''],
57 'absolute directory canonical trailing'); 64 'absolute canonical trailing');
58 testGetters(new Path.fromNative("\\foo\\bar\\hest"), 65 testGetters(new Path.fromNative("\\foo\\bar\\hest"),
59 ['/foo/bar', 'hest', 'hest', ''], 66 ['/foo/bar', 'hest', 'hest', ''],
60 'absolute canonical'); 67 'absolute canonical');
61 testGetters(new Path.fromNative(@"foo/bar\hest/.fisk"), 68 testGetters(new Path.fromNative(@"foo/bar\hest/.fisk"),
62 ['foo/bar/hest', '.fisk', '', 'fisk'], 69 ['foo/bar/hest', '.fisk', '', 'fisk'],
63 'canonical'); 70 'canonical');
64 testGetters(new Path.fromNative(@"foo//bar\\hest/\/.fisk"), 71 testGetters(new Path.fromNative(@"foo//bar\\hest/\/.fisk."),
65 ['foo//bar//hest', '.fisk', '', 'fisk'], 72 ['foo//bar//hest', '.fisk.', '.fisk', ''],
66 ''); 73 '');
67 } else { 74 } else {
68 // Make sure that backslashes are uninterpreted on other platforms. 75 // Make sure that backslashes are uninterpreted on other platforms.
69 testGetters(new Path.fromNative(@"/foo\bar/bif/fisk.hest"), 76 testGetters(new Path.fromNative(@"/foo\bar/bif/fisk.hest"),
70 [@'/foo\bar/bif', 'fisk.hest', 'fisk', 'hest'], 77 [@'/foo\bar/bif', 'fisk.hest', 'fisk', 'hest'],
71 'absolute canonical'); 78 'absolute canonical');
72 testGetters(new Path.fromNative(@"//foo\bar///bif////fisk.hest"), 79 testGetters(new Path.fromNative(@"//foo\bar///bif////fisk.hest"),
73 [@'//foo\bar///bif', 'fisk.hest', 'fisk', 'hest'], 80 [@'//foo\bar///bif', 'fisk.hest', 'fisk', 'hest'],
74 'absolute'); 81 'absolute');
75 testGetters(new Path.fromNative(@"/foo\ bar/bif/gule\ fisk.hest"), 82 testGetters(new Path.fromNative(@"/foo\ bar/bif/gule\ fisk.hest"),
76 [@'/foo\ bar/bif', @'gule\ fisk.hest', @'gule\ fisk', 'hest'], 83 [@'/foo\ bar/bif', @'gule\ fisk.hest', @'gule\ fisk', 'hest'],
77 'absolute canonical'); 84 'absolute canonical');
78 } 85 }
79 } 86 }
80 87
81 88
82 void testGetters(Path path, List components, String properties) { 89 void testGetters(Path path, List components, String properties) {
83 final int DIRNAME = 0; 90 final int DIRNAME = 0;
84 final int FILENAME = 1; 91 final int FILENAME = 1;
85 final int BASENAME = 2; 92 final int FILENAME_NO_EXTENSION = 2;
86 final int EXTENSION = 3; 93 final int EXTENSION = 3;
87 Expect.equals(components[DIRNAME], path.directoryPath.toString()); 94 Expect.equals(components[DIRNAME], path.directoryPath.toString());
88 Expect.equals(components[FILENAME], path.filename); 95 Expect.equals(components[FILENAME], path.filename);
89 Expect.equals(components[BASENAME], path.filenameWithoutExtension); 96 Expect.equals(components[FILENAME_NO_EXTENSION],
97 path.filenameWithoutExtension);
90 Expect.equals(components[EXTENSION], path.extension); 98 Expect.equals(components[EXTENSION], path.extension);
91 99
92 Expect.equals(path.isCanonical, properties.contains('canonical')); 100 Expect.equals(path.isCanonical, properties.contains('canonical'));
93 Expect.equals(path.isAbsolute, properties.contains('absolute')); 101 Expect.equals(path.isAbsolute, properties.contains('absolute'));
94 Expect.equals(path.hasTrailingSeparator, properties.contains('trailing')); 102 Expect.equals(path.hasTrailingSeparator, properties.contains('trailing'));
95 } 103 }
96 104
97 void testCanonicalize() { 105 void testCanonicalize() {
98 Function t = (input, canonicalized) { 106 Function t = (input, canonicalized) {
99 Expect.equals(canonicalized, new Path(input).canonicalize().toString()); 107 Expect.equals(canonicalized, new Path(input).canonicalize().toString());
(...skipping 15 matching lines...) Expand all
115 t('', '.'); 123 t('', '.');
116 t('/', '/'); 124 t('/', '/');
117 t('../foo/bar/..', '../foo'); 125 t('../foo/bar/..', '../foo');
118 t('/../foo/bar/..', '/foo'); 126 t('/../foo/bar/..', '/foo');
119 t('foo/bar/../../../joe/../..', '../..'); 127 t('foo/bar/../../../joe/../..', '../..');
120 t('a/b/c/../../..d/./.e/f././', 'a/..d/.e/f./'); 128 t('a/b/c/../../..d/./.e/f././', 'a/..d/.e/f./');
121 t('/%:/foo/../..', '/%:/'); 129 t('/%:/foo/../..', '/%:/');
122 t('c:/foo/../../..', '..'); 130 t('c:/foo/../../..', '..');
123 t('c:/foo/../../bad/dad/./..', 'bad'); 131 t('c:/foo/../../bad/dad/./..', 'bad');
124 } 132 }
133
134 void testJoinAppend() {
135 void testJoin(String a, String b, String c) {
136 Expect.equals(new Path(a).join(new Path(b)).toString(), c);
137 }
138
139 testJoin('/a/b', 'c/d', '/a/b/c/d');
140 testJoin('a/', 'b/c/', 'a/b/c/');
141 testJoin('a/b/./c/..//', 'd/.././..//e/f//', 'a/e/f/');
142 testJoin('a/b', 'c/../../../..', '..');
143 testJoin('a/b', 'c/../../../', '.');
144 testJoin('/a/b', 'c/../../../..', '/');
145 testJoin('/a/b', 'c/../../../', '/');
146 testJoin('a/b', 'c/../../../../../', '../../');
147 testJoin('a/b/c', '../../d', 'a/d');
148 testJoin('/a/b/c', '../../d', '/a/d');
149 testJoin('/a/b/c', '../../d/', '/a/d/');
150 testJoin('a/b/c', '../../d/', 'a/d/');
151
152 void testAppend(String a, String b, String c) {
153 Expect.equals(new Path(a).append(b).toString(), c);
154 }
155
156 testAppend('/a/b', 'c', '/a/b/c');
157 testAppend('a/b/', 'cd', 'a/b/cd');
158 testAppend('.', '..', './..');
159 testAppend('a/b', '/c/d', 'a/b//c/d');
160
161 // .join can only join a relative path to a path.
162 // It cannot join an absolute path to a path.
163 Expect.throws(() => new Path('/a/b/').join(new Path('/c/d')));
164
165 // Test Path.relativeTo.
166 Expect.equals('c/d',
167 new Path('/a/b/c/d').relativeTo(new Path('/a/b')).toString());
168 Expect.equals('c/d',
169 new Path('/a/b/c/d').relativeTo(new Path('/a/b/')).toString());
170 Expect.equals('.',
171 new Path('/a').relativeTo(new Path('/a')).toString());
172
173 // Not implemented yet. Should return new Path('../b/c/d/').
174 Expect.throws(() =>
175 new Path('b/c/d/').relativeTo(new Path('a/')));
176 // Should always throw - no relative path can be constructed.
177 Expect.throws(() =>
178 new Path('a/b').relativeTo(new Path('../../d')));
179 }
OLDNEW
« no previous file with comments | « runtime/bin/path_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698