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

Side by Side Diff: utils/tests/pub/path/path_posix_test.dart

Issue 11553005: Move path-manipulation code from io.dart into path.dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes + relative fix Created 8 years 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 | « utils/pub/path.dart ('k') | utils/tests/pub/path/path_windows_test.dart » ('j') | 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 library path_test; 5 library path_test;
6 6
7 import 'dart:io' as io; 7 import 'dart:io' as io;
8 8
9 import '../../../../pkg/unittest/lib/unittest.dart'; 9 import '../../../../pkg/unittest/lib/unittest.dart';
10 import '../../../pub/path.dart' as path; 10 import '../../../pub/path.dart' as path;
(...skipping 15 matching lines...) Expand all
26 test('extension', () { 26 test('extension', () {
27 expect(builder.extension(''), ''); 27 expect(builder.extension(''), '');
28 expect(builder.extension('foo.dart'), '.dart'); 28 expect(builder.extension('foo.dart'), '.dart');
29 expect(builder.extension('foo.dart.js'), '.js'); 29 expect(builder.extension('foo.dart.js'), '.js');
30 expect(builder.extension('a.b/c'), ''); 30 expect(builder.extension('a.b/c'), '');
31 expect(builder.extension('a.b/c.d'), '.d'); 31 expect(builder.extension('a.b/c.d'), '.d');
32 expect(builder.extension('~/.bashrc'), ''); 32 expect(builder.extension('~/.bashrc'), '');
33 expect(builder.extension(r'a.b\c'), r'.b\c'); 33 expect(builder.extension(r'a.b\c'), r'.b\c');
34 }); 34 });
35 35
36 test('rootPrefix', () {
37 expect(builder.rootPrefix(''), '');
38 expect(builder.rootPrefix('a'), '');
39 expect(builder.rootPrefix('a/b'), '');
40 expect(builder.rootPrefix('/a/c'), '/');
41 expect(builder.rootPrefix('/'), '/');
42 });
43
44 test('dirname', () {
45 expect(builder.dirname(''), '.');
46 expect(builder.dirname('a'), '.');
47 expect(builder.dirname('a/b'), 'a');
48 expect(builder.dirname('a/b/c'), 'a/b');
49 expect(builder.dirname('a/b.c'), 'a');
50 expect(builder.dirname('a/'), 'a');
51 expect(builder.dirname('a/.'), 'a');
52 expect(builder.dirname(r'a\b/c'), r'a\b');
53 expect(builder.dirname('/a'), '/');
54 expect(builder.dirname('/'), '/');
55 expect(builder.dirname('a/b/'), 'a/b');
56 expect(builder.dirname(r'a/b\c'), 'a');
57 expect(builder.dirname('a//'), 'a/');
58 });
59
36 test('basename', () { 60 test('basename', () {
37 expect(builder.basename(''), ''); 61 expect(builder.basename(''), '');
38 expect(builder.basename('a'), 'a'); 62 expect(builder.basename('a'), 'a');
39 expect(builder.basename('a/b'), 'b'); 63 expect(builder.basename('a/b'), 'b');
40 expect(builder.basename('a/b/c'), 'c'); 64 expect(builder.basename('a/b/c'), 'c');
41 expect(builder.basename('a/b.c'), 'b.c'); 65 expect(builder.basename('a/b.c'), 'b.c');
42 expect(builder.basename('a/'), ''); 66 expect(builder.basename('a/'), '');
43 expect(builder.basename('a/.'), '.'); 67 expect(builder.basename('a/.'), '.');
68 expect(builder.basename(r'a\b/c'), 'c');
69 expect(builder.basename('/a'), 'a');
70 // TODO(nweiz): this should actually return '/'
71 expect(builder.basename('/'), '');
72 expect(builder.basename('a/b/'), '');
44 expect(builder.basename(r'a/b\c'), r'b\c'); 73 expect(builder.basename(r'a/b\c'), r'b\c');
74 expect(builder.basename('a//'), '');
45 }); 75 });
46 76
47 test('basenameWithoutExtension', () { 77 test('basenameWithoutExtension', () {
48 expect(builder.basenameWithoutExtension(''), ''); 78 expect(builder.basenameWithoutExtension(''), '');
49 expect(builder.basenameWithoutExtension('a'), 'a'); 79 expect(builder.basenameWithoutExtension('a'), 'a');
50 expect(builder.basenameWithoutExtension('a/b'), 'b'); 80 expect(builder.basenameWithoutExtension('a/b'), 'b');
51 expect(builder.basenameWithoutExtension('a/b/c'), 'c'); 81 expect(builder.basenameWithoutExtension('a/b/c'), 'c');
52 expect(builder.basenameWithoutExtension('a/b.c'), 'b'); 82 expect(builder.basenameWithoutExtension('a/b.c'), 'b');
53 expect(builder.basenameWithoutExtension('a/'), ''); 83 expect(builder.basenameWithoutExtension('a/'), '');
54 expect(builder.basenameWithoutExtension('a/.'), '.'); 84 expect(builder.basenameWithoutExtension('a/.'), '.');
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'), 127 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'),
98 'a/b/c/d/e/f/g/h'); 128 'a/b/c/d/e/f/g/h');
99 }); 129 });
100 130
101 test('does not add separator if a part ends in one', () { 131 test('does not add separator if a part ends in one', () {
102 expect(builder.join('a/', 'b', 'c/', 'd'), 'a/b/c/d'); 132 expect(builder.join('a/', 'b', 'c/', 'd'), 'a/b/c/d');
103 expect(builder.join('a\\', 'b'), r'a\/b'); 133 expect(builder.join('a\\', 'b'), r'a\/b');
104 }); 134 });
105 135
106 test('ignores parts before an absolute path', () { 136 test('ignores parts before an absolute path', () {
137 expect(builder.join('a', '/', 'b', 'c'), '/b/c');
107 expect(builder.join('a', '/b', '/c', 'd'), '/c/d'); 138 expect(builder.join('a', '/b', '/c', 'd'), '/c/d');
108 expect(builder.join('a', r'c:\b', 'c', 'd'), r'a/c:\b/c/d'); 139 expect(builder.join('a', r'c:\b', 'c', 'd'), r'a/c:\b/c/d');
109 expect(builder.join('a', r'\\b', 'c', 'd'), r'a/\\b/c/d'); 140 expect(builder.join('a', r'\\b', 'c', 'd'), r'a/\\b/c/d');
110 }); 141 });
142
143 test('ignores trailing nulls', () {
144 expect(builder.join('a', null), equals('a'));
145 expect(builder.join('a', 'b', 'c', null, null), equals('a/b/c'));
146 });
147
148 test('disallows intermediate nulls', () {
149 expect(() => builder.join('a', null, 'b'), throwsArgumentError);
150 expect(() => builder.join(null, 'a'), throwsArgumentError);
151 });
152 });
153
154 group('split', () {
155 test('simple cases', () {
156 expect(builder.split('foo'), equals(['foo']));
157 expect(builder.split('foo/bar'), equals(['foo', 'bar']));
158 expect(builder.split('foo/bar/baz'), equals(['foo', 'bar', 'baz']));
159 expect(builder.split('foo/../bar/./baz'),
160 equals(['foo', '..', 'bar', '.', 'baz']));
161 expect(builder.split('foo//bar///baz'), equals(['foo', 'bar', 'baz']));
162 expect(builder.split('foo/\\/baz'), equals(['foo', '\\', 'baz']));
163 expect(builder.split('.'), equals(['.']));
164 expect(builder.split(''), equals([]));
165 expect(builder.split('foo/'), equals(['foo']));
166 expect(builder.split('//'), equals(['/']));
167 });
168
169 test('includes the root for absolute paths', () {
170 expect(builder.split('/foo/bar/baz'), equals(['/', 'foo', 'bar', 'baz']));
171 expect(builder.split('/'), equals(['/']));
172 });
111 }); 173 });
112 174
113 group('normalize', () { 175 group('normalize', () {
114 test('simple cases', () { 176 test('simple cases', () {
115 expect(builder.normalize(''), ''); 177 expect(builder.normalize(''), '');
116 expect(builder.normalize('.'), '.'); 178 expect(builder.normalize('.'), '.');
117 expect(builder.normalize('..'), '..'); 179 expect(builder.normalize('..'), '..');
118 expect(builder.normalize('a'), 'a'); 180 expect(builder.normalize('a'), 'a');
119 expect(builder.normalize('/'), '/'); 181 expect(builder.normalize('/'), '/');
120 expect(builder.normalize(r'\'), r'\'); 182 expect(builder.normalize(r'\'), r'\');
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 expect(builder.relative('a'), 'a'); 266 expect(builder.relative('a'), 'a');
205 expect(builder.relative('a/b.txt'), 'a/b.txt'); 267 expect(builder.relative('a/b.txt'), 'a/b.txt');
206 expect(builder.relative('../a/b.txt'), '../a/b.txt'); 268 expect(builder.relative('../a/b.txt'), '../a/b.txt');
207 expect(builder.relative('a/./b/../c.txt'), 'a/c.txt'); 269 expect(builder.relative('a/./b/../c.txt'), 'a/c.txt');
208 }); 270 });
209 }); 271 });
210 272
211 group('from relative root', () { 273 group('from relative root', () {
212 var r = new path.Builder(style: path.Style.posix, root: 'foo/bar'); 274 var r = new path.Builder(style: path.Style.posix, root: 'foo/bar');
213 275
214 // These tests rely on the current working directory, so don't do the 276 test('given absolute path', () {
215 // right thing if you run them on the wrong platform. 277 expect(r.relative('/'), equals('/'));
216 if (io.Platform.operatingSystem != 'windows') { 278 expect(r.relative('/a/b'), equals('/a/b'));
217 test('given absolute path', () { 279 });
218 var b = new path.Builder(style: path.Style.posix);
219 expect(r.relative('/'), b.join(b.relative('/'), '../..'));
220 expect(r.relative('/a/b'), b.join(b.relative('/'), '../../a/b'));
221 });
222 }
223 280
224 test('given relative path', () { 281 test('given relative path', () {
225 // The path is considered relative to the root, so it basically just 282 // The path is considered relative to the root, so it basically just
226 // normalizes. 283 // normalizes.
227 expect(r.relative(''), '.'); 284 expect(r.relative(''), '.');
228 expect(r.relative('.'), '.'); 285 expect(r.relative('.'), '.');
229 expect(r.relative('..'), '..'); 286 expect(r.relative('..'), '..');
230 expect(r.relative('a'), 'a'); 287 expect(r.relative('a'), 'a');
231 expect(r.relative('a/b.txt'), 'a/b.txt'); 288 expect(r.relative('a/b.txt'), 'a/b.txt');
232 expect(r.relative('../a/b.txt'), '../a/b.txt'); 289 expect(r.relative('../a/b.txt'), '../a/b.txt');
233 expect(r.relative('a/./b/../c.txt'), 'a/c.txt'); 290 expect(r.relative('a/./b/../c.txt'), 'a/c.txt');
234 }); 291 });
235 }); 292 });
236 293
237 test('from a root with extension', () { 294 test('from a root with extension', () {
238 var r = new path.Builder(style: path.Style.posix, root: '/dir.ext'); 295 var r = new path.Builder(style: path.Style.posix, root: '/dir.ext');
239 expect(r.relative('/dir.ext/file'), 'file'); 296 expect(r.relative('/dir.ext/file'), 'file');
240 }); 297 });
298
299 test('with a root parameter', () {
300 expect(builder.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz'));
301 expect(builder.relative('..', from: '/foo/bar'), equals('../../root'));
302 expect(builder.relative('/foo/bar/baz', from: 'foo/bar'),
303 equals('../../../../foo/bar/baz'));
304 expect(builder.relative('..', from: 'foo/bar'), equals('../../..'));
305 });
306
307 test('with a root parameter and a relative root', () {
308 var r = new path.Builder(style: path.Style.posix, root: 'relative/root');
309 expect(r.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz'));
310 expect(() => r.relative('..', from: '/foo/bar'), throwsArgumentError);
311 expect(r.relative('/foo/bar/baz', from: 'foo/bar'),
312 equals('/foo/bar/baz'));
313 expect(r.relative('..', from: 'foo/bar'), equals('../../..'));
314 });
241 }); 315 });
242 316
243 group('resolve', () { 317 group('resolve', () {
244 test('allows up to seven parts', () { 318 test('allows up to seven parts', () {
245 expect(builder.resolve('a'), '/root/path/a'); 319 expect(builder.resolve('a'), '/root/path/a');
246 expect(builder.resolve('a', 'b'), '/root/path/a/b'); 320 expect(builder.resolve('a', 'b'), '/root/path/a/b');
247 expect(builder.resolve('a', 'b', 'c'), '/root/path/a/b/c'); 321 expect(builder.resolve('a', 'b', 'c'), '/root/path/a/b/c');
248 expect(builder.resolve('a', 'b', 'c', 'd'), '/root/path/a/b/c/d'); 322 expect(builder.resolve('a', 'b', 'c', 'd'), '/root/path/a/b/c/d');
249 expect(builder.resolve('a', 'b', 'c', 'd', 'e'), '/root/path/a/b/c/d/e'); 323 expect(builder.resolve('a', 'b', 'c', 'd', 'e'), '/root/path/a/b/c/d/e');
250 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f'), 324 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f'),
(...skipping 24 matching lines...) Expand all
275 expect(builder.withoutExtension('a/'), 'a/'); 349 expect(builder.withoutExtension('a/'), 'a/');
276 expect(builder.withoutExtension('a/b/'), 'a/b/'); 350 expect(builder.withoutExtension('a/b/'), 'a/b/');
277 expect(builder.withoutExtension('a/.'), 'a/.'); 351 expect(builder.withoutExtension('a/.'), 'a/.');
278 expect(builder.withoutExtension('a/.b'), 'a/.b'); 352 expect(builder.withoutExtension('a/.b'), 'a/.b');
279 expect(builder.withoutExtension('a.b/c'), 'a.b/c'); 353 expect(builder.withoutExtension('a.b/c'), 'a.b/c');
280 expect(builder.withoutExtension(r'a.b\c'), r'a'); 354 expect(builder.withoutExtension(r'a.b\c'), r'a');
281 expect(builder.withoutExtension(r'a/b\c'), r'a/b\c'); 355 expect(builder.withoutExtension(r'a/b\c'), r'a/b\c');
282 expect(builder.withoutExtension(r'a/b\c.d'), r'a/b\c'); 356 expect(builder.withoutExtension(r'a/b\c.d'), r'a/b\c');
283 }); 357 });
284 } 358 }
OLDNEW
« no previous file with comments | « utils/pub/path.dart ('k') | utils/tests/pub/path/path_windows_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698