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

Side by Side Diff: pkg/pathos/test/pathos_posix_test.dart

Issue 18356011: Rename "pathos" package to "path". (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library pathos_posix_test;
6
7 import 'package:unittest/unittest.dart';
8 import 'package:pathos/path.dart' as path;
9
10 main() {
11 var builder = new path.Builder(style: path.Style.posix, root: '/root/path');
12
13 if (new path.Builder().style == path.Style.posix) {
14 group('absolute', () {
15 expect(path.absolute('a/b.txt'), path.join(path.current, 'a/b.txt'));
16 expect(path.absolute('/a/b.txt'), '/a/b.txt');
17 });
18 }
19
20 test('separator', () {
21 expect(builder.separator, '/');
22 });
23
24 test('extension', () {
25 expect(builder.extension(''), '');
26 expect(builder.extension('foo.dart'), '.dart');
27 expect(builder.extension('foo.dart.js'), '.js');
28 expect(builder.extension('a.b/c'), '');
29 expect(builder.extension('a.b/c.d'), '.d');
30 expect(builder.extension('~/.bashrc'), '');
31 expect(builder.extension(r'a.b\c'), r'.b\c');
32 });
33
34 test('rootPrefix', () {
35 expect(builder.rootPrefix(''), '');
36 expect(builder.rootPrefix('a'), '');
37 expect(builder.rootPrefix('a/b'), '');
38 expect(builder.rootPrefix('/a/c'), '/');
39 expect(builder.rootPrefix('/'), '/');
40 });
41
42 test('dirname', () {
43 expect(builder.dirname(''), '.');
44 expect(builder.dirname('a'), '.');
45 expect(builder.dirname('a/b'), 'a');
46 expect(builder.dirname('a/b/c'), 'a/b');
47 expect(builder.dirname('a/b.c'), 'a');
48 expect(builder.dirname('a/'), '.');
49 expect(builder.dirname('a/.'), 'a');
50 expect(builder.dirname(r'a\b/c'), r'a\b');
51 expect(builder.dirname('/a'), '/');
52 expect(builder.dirname('///a'), '/');
53 expect(builder.dirname('/'), '/');
54 expect(builder.dirname('///'), '/');
55 expect(builder.dirname('a/b/'), 'a');
56 expect(builder.dirname(r'a/b\c'), 'a');
57 expect(builder.dirname('a//'), '.');
58 expect(builder.dirname('a/b//'), 'a');
59 expect(builder.dirname('a//b'), 'a');
60 });
61
62 test('basename', () {
63 expect(builder.basename(''), '');
64 expect(builder.basename('a'), 'a');
65 expect(builder.basename('a/b'), 'b');
66 expect(builder.basename('a/b/c'), 'c');
67 expect(builder.basename('a/b.c'), 'b.c');
68 expect(builder.basename('a/'), 'a');
69 expect(builder.basename('a/.'), '.');
70 expect(builder.basename(r'a\b/c'), 'c');
71 expect(builder.basename('/a'), 'a');
72 expect(builder.basename('/'), '/');
73 expect(builder.basename('a/b/'), 'b');
74 expect(builder.basename(r'a/b\c'), r'b\c');
75 expect(builder.basename('a//'), 'a');
76 expect(builder.basename('a/b//'), 'b');
77 expect(builder.basename('a//b'), 'b');
78 });
79
80 test('basenameWithoutExtension', () {
81 expect(builder.basenameWithoutExtension(''), '');
82 expect(builder.basenameWithoutExtension('a'), 'a');
83 expect(builder.basenameWithoutExtension('a/b'), 'b');
84 expect(builder.basenameWithoutExtension('a/b/c'), 'c');
85 expect(builder.basenameWithoutExtension('a/b.c'), 'b');
86 expect(builder.basenameWithoutExtension('a/'), 'a');
87 expect(builder.basenameWithoutExtension('a/.'), '.');
88 expect(builder.basenameWithoutExtension(r'a/b\c'), r'b\c');
89 expect(builder.basenameWithoutExtension('a/.bashrc'), '.bashrc');
90 expect(builder.basenameWithoutExtension('a/b/c.d.e'), 'c.d');
91 expect(builder.basenameWithoutExtension('a//'), 'a');
92 expect(builder.basenameWithoutExtension('a/b//'), 'b');
93 expect(builder.basenameWithoutExtension('a//b'), 'b');
94 expect(builder.basenameWithoutExtension('a/b.c/'), 'b');
95 expect(builder.basenameWithoutExtension('a/b.c//'), 'b');
96 });
97
98 test('isAbsolute', () {
99 expect(builder.isAbsolute(''), false);
100 expect(builder.isAbsolute('a'), false);
101 expect(builder.isAbsolute('a/b'), false);
102 expect(builder.isAbsolute('/a'), true);
103 expect(builder.isAbsolute('/a/b'), true);
104 expect(builder.isAbsolute('~'), false);
105 expect(builder.isAbsolute('.'), false);
106 expect(builder.isAbsolute('../a'), false);
107 expect(builder.isAbsolute('C:/a'), false);
108 expect(builder.isAbsolute(r'C:\a'), false);
109 expect(builder.isAbsolute(r'\\a'), false);
110 });
111
112 test('isRelative', () {
113 expect(builder.isRelative(''), true);
114 expect(builder.isRelative('a'), true);
115 expect(builder.isRelative('a/b'), true);
116 expect(builder.isRelative('/a'), false);
117 expect(builder.isRelative('/a/b'), false);
118 expect(builder.isRelative('~'), true);
119 expect(builder.isRelative('.'), true);
120 expect(builder.isRelative('../a'), true);
121 expect(builder.isRelative('C:/a'), true);
122 expect(builder.isRelative(r'C:\a'), true);
123 expect(builder.isRelative(r'\\a'), true);
124 });
125
126 group('join', () {
127 test('allows up to eight parts', () {
128 expect(builder.join('a'), 'a');
129 expect(builder.join('a', 'b'), 'a/b');
130 expect(builder.join('a', 'b', 'c'), 'a/b/c');
131 expect(builder.join('a', 'b', 'c', 'd'), 'a/b/c/d');
132 expect(builder.join('a', 'b', 'c', 'd', 'e'), 'a/b/c/d/e');
133 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f'), 'a/b/c/d/e/f');
134 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g'), 'a/b/c/d/e/f/g');
135 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'),
136 'a/b/c/d/e/f/g/h');
137 });
138
139 test('does not add separator if a part ends in one', () {
140 expect(builder.join('a/', 'b', 'c/', 'd'), 'a/b/c/d');
141 expect(builder.join('a\\', 'b'), r'a\/b');
142 });
143
144 test('ignores parts before an absolute path', () {
145 expect(builder.join('a', '/', 'b', 'c'), '/b/c');
146 expect(builder.join('a', '/b', '/c', 'd'), '/c/d');
147 expect(builder.join('a', r'c:\b', 'c', 'd'), r'a/c:\b/c/d');
148 expect(builder.join('a', r'\\b', 'c', 'd'), r'a/\\b/c/d');
149 });
150
151 test('ignores trailing nulls', () {
152 expect(builder.join('a', null), equals('a'));
153 expect(builder.join('a', 'b', 'c', null, null), equals('a/b/c'));
154 });
155
156 test('disallows intermediate nulls', () {
157 expect(() => builder.join('a', null, 'b'), throwsArgumentError);
158 expect(() => builder.join(null, 'a'), throwsArgumentError);
159 });
160 });
161
162 group('joinAll', () {
163 test('allows more than eight parts', () {
164 expect(builder.joinAll(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']),
165 'a/b/c/d/e/f/g/h/i');
166 });
167
168 test('does not add separator if a part ends in one', () {
169 expect(builder.joinAll(['a/', 'b', 'c/', 'd']), 'a/b/c/d');
170 expect(builder.joinAll(['a\\', 'b']), r'a\/b');
171 });
172
173 test('ignores parts before an absolute path', () {
174 expect(builder.joinAll(['a', '/', 'b', 'c']), '/b/c');
175 expect(builder.joinAll(['a', '/b', '/c', 'd']), '/c/d');
176 expect(builder.joinAll(['a', r'c:\b', 'c', 'd']), r'a/c:\b/c/d');
177 expect(builder.joinAll(['a', r'\\b', 'c', 'd']), r'a/\\b/c/d');
178 });
179 });
180
181 group('split', () {
182 test('simple cases', () {
183 expect(builder.split(''), []);
184 expect(builder.split('.'), ['.']);
185 expect(builder.split('..'), ['..']);
186 expect(builder.split('foo'), equals(['foo']));
187 expect(builder.split('foo/bar.txt'), equals(['foo', 'bar.txt']));
188 expect(builder.split('foo/bar/baz'), equals(['foo', 'bar', 'baz']));
189 expect(builder.split('foo/../bar/./baz'),
190 equals(['foo', '..', 'bar', '.', 'baz']));
191 expect(builder.split('foo//bar///baz'), equals(['foo', 'bar', 'baz']));
192 expect(builder.split('foo/\\/baz'), equals(['foo', '\\', 'baz']));
193 expect(builder.split('.'), equals(['.']));
194 expect(builder.split(''), equals([]));
195 expect(builder.split('foo/'), equals(['foo']));
196 expect(builder.split('//'), equals(['/']));
197 });
198
199 test('includes the root for absolute paths', () {
200 expect(builder.split('/foo/bar/baz'), equals(['/', 'foo', 'bar', 'baz']));
201 expect(builder.split('/'), equals(['/']));
202 });
203 });
204
205 group('normalize', () {
206 test('simple cases', () {
207 expect(builder.normalize(''), '');
208 expect(builder.normalize('.'), '.');
209 expect(builder.normalize('..'), '..');
210 expect(builder.normalize('a'), 'a');
211 expect(builder.normalize('/'), '/');
212 expect(builder.normalize(r'\'), r'\');
213 });
214
215 test('collapses redundant separators', () {
216 expect(builder.normalize(r'a/b/c'), r'a/b/c');
217 expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d');
218 });
219
220 test('does not collapse separators for other platform', () {
221 expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c');
222 });
223
224 test('eliminates "." parts', () {
225 expect(builder.normalize('./'), '.');
226 expect(builder.normalize('/.'), '/');
227 expect(builder.normalize('/./'), '/');
228 expect(builder.normalize('./.'), '.');
229 expect(builder.normalize('a/./b'), 'a/b');
230 expect(builder.normalize('a/.b/c'), 'a/.b/c');
231 expect(builder.normalize('a/././b/./c'), 'a/b/c');
232 expect(builder.normalize('././a'), 'a');
233 expect(builder.normalize('a/./.'), 'a');
234 });
235
236 test('eliminates ".." parts', () {
237 expect(builder.normalize('..'), '..');
238 expect(builder.normalize('../'), '..');
239 expect(builder.normalize('../../..'), '../../..');
240 expect(builder.normalize('../../../'), '../../..');
241 expect(builder.normalize('/..'), '/');
242 expect(builder.normalize('/../../..'), '/');
243 expect(builder.normalize('/../../../a'), '/a');
244 expect(builder.normalize('a/..'), '.');
245 expect(builder.normalize('a/b/..'), 'a');
246 expect(builder.normalize('a/../b'), 'b');
247 expect(builder.normalize('a/./../b'), 'b');
248 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d');
249 expect(builder.normalize('a/b/../../../../c'), '../../c');
250 });
251
252 test('does not walk before root on absolute paths', () {
253 expect(builder.normalize('..'), '..');
254 expect(builder.normalize('../'), '..');
255 expect(builder.normalize('/..'), '/');
256 expect(builder.normalize('a/..'), '.');
257 expect(builder.normalize('a/b/..'), 'a');
258 expect(builder.normalize('a/../b'), 'b');
259 expect(builder.normalize('a/./../b'), 'b');
260 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d');
261 expect(builder.normalize('a/b/../../../../c'), '../../c');
262 });
263
264 test('removes trailing separators', () {
265 expect(builder.normalize('./'), '.');
266 expect(builder.normalize('.//'), '.');
267 expect(builder.normalize('a/'), 'a');
268 expect(builder.normalize('a/b/'), 'a/b');
269 expect(builder.normalize('a/b///'), 'a/b');
270 });
271 });
272
273 group('relative', () {
274 group('from absolute root', () {
275 test('given absolute path in root', () {
276 expect(builder.relative('/'), '../..');
277 expect(builder.relative('/root'), '..');
278 expect(builder.relative('/root/path'), '.');
279 expect(builder.relative('/root/path/a'), 'a');
280 expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt');
281 expect(builder.relative('/root/a/b.txt'), '../a/b.txt');
282 });
283
284 test('given absolute path outside of root', () {
285 expect(builder.relative('/a/b'), '../../a/b');
286 expect(builder.relative('/root/path/a'), 'a');
287 expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt');
288 expect(builder.relative('/root/a/b.txt'), '../a/b.txt');
289 });
290
291 test('given relative path', () {
292 // The path is considered relative to the root, so it basically just
293 // normalizes.
294 expect(builder.relative(''), '.');
295 expect(builder.relative('.'), '.');
296 expect(builder.relative('a'), 'a');
297 expect(builder.relative('a/b.txt'), 'a/b.txt');
298 expect(builder.relative('../a/b.txt'), '../a/b.txt');
299 expect(builder.relative('a/./b/../c.txt'), 'a/c.txt');
300 });
301
302 // Regression
303 test('from root-only path', () {
304 expect(builder.relative('/', from: '/'), '.');
305 expect(builder.relative('/root/path', from: '/'), 'root/path');
306 });
307 });
308
309 group('from relative root', () {
310 var r = new path.Builder(style: path.Style.posix, root: 'foo/bar');
311
312 test('given absolute path', () {
313 expect(r.relative('/'), equals('/'));
314 expect(r.relative('/a/b'), equals('/a/b'));
315 });
316
317 test('given relative path', () {
318 // The path is considered relative to the root, so it basically just
319 // normalizes.
320 expect(r.relative(''), '.');
321 expect(r.relative('.'), '.');
322 expect(r.relative('..'), '..');
323 expect(r.relative('a'), 'a');
324 expect(r.relative('a/b.txt'), 'a/b.txt');
325 expect(r.relative('../a/b.txt'), '../a/b.txt');
326 expect(r.relative('a/./b/../c.txt'), 'a/c.txt');
327 });
328 });
329
330 test('from a root with extension', () {
331 var r = new path.Builder(style: path.Style.posix, root: '/dir.ext');
332 expect(r.relative('/dir.ext/file'), 'file');
333 });
334
335 test('with a root parameter', () {
336 expect(builder.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz'));
337 expect(builder.relative('..', from: '/foo/bar'), equals('../../root'));
338 expect(builder.relative('/foo/bar/baz', from: 'foo/bar'),
339 equals('../../../../foo/bar/baz'));
340 expect(builder.relative('..', from: 'foo/bar'), equals('../../..'));
341 });
342
343 test('with a root parameter and a relative root', () {
344 var r = new path.Builder(style: path.Style.posix, root: 'relative/root');
345 expect(r.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz'));
346 expect(() => r.relative('..', from: '/foo/bar'), throwsArgumentError);
347 expect(r.relative('/foo/bar/baz', from: 'foo/bar'),
348 equals('/foo/bar/baz'));
349 expect(r.relative('..', from: 'foo/bar'), equals('../../..'));
350 });
351
352 test('from a . root', () {
353 var r = new path.Builder(style: path.Style.posix, root: '.');
354 expect(r.relative('/foo/bar/baz'), equals('/foo/bar/baz'));
355 expect(r.relative('foo/bar/baz'), equals('foo/bar/baz'));
356 });
357 });
358
359 group('resolve', () {
360 test('allows up to seven parts', () {
361 expect(builder.resolve('a'), '/root/path/a');
362 expect(builder.resolve('a', 'b'), '/root/path/a/b');
363 expect(builder.resolve('a', 'b', 'c'), '/root/path/a/b/c');
364 expect(builder.resolve('a', 'b', 'c', 'd'), '/root/path/a/b/c/d');
365 expect(builder.resolve('a', 'b', 'c', 'd', 'e'), '/root/path/a/b/c/d/e');
366 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f'),
367 '/root/path/a/b/c/d/e/f');
368 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f', 'g'),
369 '/root/path/a/b/c/d/e/f/g');
370 });
371
372 test('does not add separator if a part ends in one', () {
373 expect(builder.resolve('a/', 'b', 'c/', 'd'), '/root/path/a/b/c/d');
374 expect(builder.resolve(r'a\', 'b'), r'/root/path/a\/b');
375 });
376
377 test('ignores parts before an absolute path', () {
378 expect(builder.resolve('a', '/b', '/c', 'd'), '/c/d');
379 expect(builder.resolve('a', r'c:\b', 'c', 'd'), r'/root/path/a/c:\b/c/d');
380 expect(builder.resolve('a', r'\\b', 'c', 'd'), r'/root/path/a/\\b/c/d');
381 });
382 });
383
384 test('withoutExtension', () {
385 expect(builder.withoutExtension(''), '');
386 expect(builder.withoutExtension('a'), 'a');
387 expect(builder.withoutExtension('.a'), '.a');
388 expect(builder.withoutExtension('a.b'), 'a');
389 expect(builder.withoutExtension('a/b.c'), 'a/b');
390 expect(builder.withoutExtension('a/b.c.d'), 'a/b.c');
391 expect(builder.withoutExtension('a/'), 'a/');
392 expect(builder.withoutExtension('a/b/'), 'a/b/');
393 expect(builder.withoutExtension('a/.'), 'a/.');
394 expect(builder.withoutExtension('a/.b'), 'a/.b');
395 expect(builder.withoutExtension('a.b/c'), 'a.b/c');
396 expect(builder.withoutExtension(r'a.b\c'), r'a');
397 expect(builder.withoutExtension(r'a/b\c'), r'a/b\c');
398 expect(builder.withoutExtension(r'a/b\c.d'), r'a/b\c');
399 expect(builder.withoutExtension('a/b.c/'), 'a/b/');
400 expect(builder.withoutExtension('a/b.c//'), 'a/b//');
401 });
402
403 test('fromUri', () {
404 expect(builder.fromUri(Uri.parse('file:///path/to/foo')), '/path/to/foo');
405 expect(builder.fromUri(Uri.parse('file:///path/to/foo/')), '/path/to/foo/');
406 expect(builder.fromUri(Uri.parse('file:///')), '/');
407 expect(builder.fromUri(Uri.parse('foo/bar')), 'foo/bar');
408 expect(builder.fromUri(Uri.parse('/path/to/foo')), '/path/to/foo');
409 expect(builder.fromUri(Uri.parse('///path/to/foo')), '/path/to/foo');
410 expect(builder.fromUri(Uri.parse('file:///path/to/foo%23bar')),
411 '/path/to/foo#bar');
412 expect(() => builder.fromUri(Uri.parse('http://dartlang.org')),
413 throwsArgumentError);
414 });
415
416 test('toUri', () {
417 expect(builder.toUri('/path/to/foo'), Uri.parse('file:///path/to/foo'));
418 expect(builder.toUri('/path/to/foo/'), Uri.parse('file:///path/to/foo/'));
419 expect(builder.toUri('/'), Uri.parse('file:///'));
420 expect(builder.toUri('foo/bar'), Uri.parse('foo/bar'));
421 expect(builder.toUri('/path/to/foo#bar'),
422 Uri.parse('file:///path/to/foo%23bar'));
423 });
424 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698