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

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

Issue 11647003: Move path ("pathos") to pkg/. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Typo. 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/validator/name.dart ('k') | utils/tests/pub/path/path_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
(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 path_test;
6
7 import 'dart:io' as io;
8
9 import '../../../../pkg/unittest/lib/unittest.dart';
10 import '../../../pub/path.dart' as path;
11
12 main() {
13 var builder = new path.Builder(style: path.Style.posix, root: '/root/path');
14
15 if (new path.Builder().style == path.Style.posix) {
16 group('absolute', () {
17 expect(path.absolute('a/b.txt'), path.join(path.current, 'a/b.txt'));
18 expect(path.absolute('/a/b.txt'), '/a/b.txt');
19 });
20 }
21
22 test('separator', () {
23 expect(builder.separator, '/');
24 });
25
26 test('extension', () {
27 expect(builder.extension(''), '');
28 expect(builder.extension('foo.dart'), '.dart');
29 expect(builder.extension('foo.dart.js'), '.js');
30 expect(builder.extension('a.b/c'), '');
31 expect(builder.extension('a.b/c.d'), '.d');
32 expect(builder.extension('~/.bashrc'), '');
33 expect(builder.extension(r'a.b\c'), r'.b\c');
34 });
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
60 test('basename', () {
61 expect(builder.basename(''), '');
62 expect(builder.basename('a'), 'a');
63 expect(builder.basename('a/b'), 'b');
64 expect(builder.basename('a/b/c'), 'c');
65 expect(builder.basename('a/b.c'), 'b.c');
66 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/'), '');
73 expect(builder.basename(r'a/b\c'), r'b\c');
74 expect(builder.basename('a//'), '');
75 });
76
77 test('basenameWithoutExtension', () {
78 expect(builder.basenameWithoutExtension(''), '');
79 expect(builder.basenameWithoutExtension('a'), 'a');
80 expect(builder.basenameWithoutExtension('a/b'), 'b');
81 expect(builder.basenameWithoutExtension('a/b/c'), 'c');
82 expect(builder.basenameWithoutExtension('a/b.c'), 'b');
83 expect(builder.basenameWithoutExtension('a/'), '');
84 expect(builder.basenameWithoutExtension('a/.'), '.');
85 expect(builder.basenameWithoutExtension(r'a/b\c'), r'b\c');
86 expect(builder.basenameWithoutExtension('a/.bashrc'), '.bashrc');
87 expect(builder.basenameWithoutExtension('a/b/c.d.e'), 'c.d');
88 });
89
90 test('isAbsolute', () {
91 expect(builder.isAbsolute(''), false);
92 expect(builder.isAbsolute('a'), false);
93 expect(builder.isAbsolute('a/b'), false);
94 expect(builder.isAbsolute('/a'), true);
95 expect(builder.isAbsolute('/a/b'), true);
96 expect(builder.isAbsolute('~'), false);
97 expect(builder.isAbsolute('.'), false);
98 expect(builder.isAbsolute('../a'), false);
99 expect(builder.isAbsolute('C:/a'), false);
100 expect(builder.isAbsolute(r'C:\a'), false);
101 expect(builder.isAbsolute(r'\\a'), false);
102 });
103
104 test('isRelative', () {
105 expect(builder.isRelative(''), true);
106 expect(builder.isRelative('a'), true);
107 expect(builder.isRelative('a/b'), true);
108 expect(builder.isRelative('/a'), false);
109 expect(builder.isRelative('/a/b'), false);
110 expect(builder.isRelative('~'), true);
111 expect(builder.isRelative('.'), true);
112 expect(builder.isRelative('../a'), true);
113 expect(builder.isRelative('C:/a'), true);
114 expect(builder.isRelative(r'C:\a'), true);
115 expect(builder.isRelative(r'\\a'), true);
116 });
117
118 group('join', () {
119 test('allows up to eight parts', () {
120 expect(builder.join('a'), 'a');
121 expect(builder.join('a', 'b'), 'a/b');
122 expect(builder.join('a', 'b', 'c'), 'a/b/c');
123 expect(builder.join('a', 'b', 'c', 'd'), 'a/b/c/d');
124 expect(builder.join('a', 'b', 'c', 'd', 'e'), 'a/b/c/d/e');
125 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f'), 'a/b/c/d/e/f');
126 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g'), 'a/b/c/d/e/f/g');
127 expect(builder.join('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'),
128 'a/b/c/d/e/f/g/h');
129 });
130
131 test('does not add separator if a part ends in one', () {
132 expect(builder.join('a/', 'b', 'c/', 'd'), 'a/b/c/d');
133 expect(builder.join('a\\', 'b'), r'a\/b');
134 });
135
136 test('ignores parts before an absolute path', () {
137 expect(builder.join('a', '/', 'b', 'c'), '/b/c');
138 expect(builder.join('a', '/b', '/c', 'd'), '/c/d');
139 expect(builder.join('a', r'c:\b', 'c', 'd'), r'a/c:\b/c/d');
140 expect(builder.join('a', r'\\b', 'c', 'd'), r'a/\\b/c/d');
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(''), []);
157 expect(builder.split('.'), ['.']);
158 expect(builder.split('..'), ['..']);
159 expect(builder.split('foo'), equals(['foo']));
160 expect(builder.split('foo/bar.txt'), equals(['foo', 'bar.txt']));
161 expect(builder.split('foo/bar/baz'), equals(['foo', 'bar', 'baz']));
162 expect(builder.split('foo/../bar/./baz'),
163 equals(['foo', '..', 'bar', '.', 'baz']));
164 expect(builder.split('foo//bar///baz'), equals(['foo', 'bar', 'baz']));
165 expect(builder.split('foo/\\/baz'), equals(['foo', '\\', 'baz']));
166 expect(builder.split('.'), equals(['.']));
167 expect(builder.split(''), equals([]));
168 expect(builder.split('foo/'), equals(['foo']));
169 expect(builder.split('//'), equals(['/']));
170 });
171
172 test('includes the root for absolute paths', () {
173 expect(builder.split('/foo/bar/baz'), equals(['/', 'foo', 'bar', 'baz']));
174 expect(builder.split('/'), equals(['/']));
175 });
176 });
177
178 group('normalize', () {
179 test('simple cases', () {
180 expect(builder.normalize(''), '');
181 expect(builder.normalize('.'), '.');
182 expect(builder.normalize('..'), '..');
183 expect(builder.normalize('a'), 'a');
184 expect(builder.normalize('/'), '/');
185 expect(builder.normalize(r'\'), r'\');
186 });
187
188 test('collapses redundant separators', () {
189 expect(builder.normalize(r'a/b/c'), r'a/b/c');
190 expect(builder.normalize(r'a//b///c////d'), r'a/b/c/d');
191 });
192
193 test('does not collapse separators for other platform', () {
194 expect(builder.normalize(r'a\\b\\\c'), r'a\\b\\\c');
195 });
196
197 test('eliminates "." parts', () {
198 expect(builder.normalize('./'), '.');
199 expect(builder.normalize('/.'), '/');
200 expect(builder.normalize('/./'), '/');
201 expect(builder.normalize('./.'), '.');
202 expect(builder.normalize('a/./b'), 'a/b');
203 expect(builder.normalize('a/.b/c'), 'a/.b/c');
204 expect(builder.normalize('a/././b/./c'), 'a/b/c');
205 expect(builder.normalize('././a'), 'a');
206 expect(builder.normalize('a/./.'), 'a');
207 });
208
209 test('eliminates ".." parts', () {
210 expect(builder.normalize('..'), '..');
211 expect(builder.normalize('../'), '..');
212 expect(builder.normalize('../../..'), '../../..');
213 expect(builder.normalize('../../../'), '../../..');
214 expect(builder.normalize('/..'), '/');
215 expect(builder.normalize('/../../..'), '/');
216 expect(builder.normalize('/../../../a'), '/a');
217 expect(builder.normalize('a/..'), '.');
218 expect(builder.normalize('a/b/..'), 'a');
219 expect(builder.normalize('a/../b'), 'b');
220 expect(builder.normalize('a/./../b'), 'b');
221 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d');
222 expect(builder.normalize('a/b/../../../../c'), '../../c');
223 });
224
225 test('does not walk before root on absolute paths', () {
226 expect(builder.normalize('..'), '..');
227 expect(builder.normalize('../'), '..');
228 expect(builder.normalize('/..'), '/');
229 expect(builder.normalize('a/..'), '.');
230 expect(builder.normalize('a/b/..'), 'a');
231 expect(builder.normalize('a/../b'), 'b');
232 expect(builder.normalize('a/./../b'), 'b');
233 expect(builder.normalize('a/b/c/../../d/e/..'), 'a/d');
234 expect(builder.normalize('a/b/../../../../c'), '../../c');
235 });
236
237 test('removes trailing separators', () {
238 expect(builder.normalize('./'), '.');
239 expect(builder.normalize('.//'), '.');
240 expect(builder.normalize('a/'), 'a');
241 expect(builder.normalize('a/b/'), 'a/b');
242 expect(builder.normalize('a/b///'), 'a/b');
243 });
244 });
245
246 group('relative', () {
247 group('from absolute root', () {
248 test('given absolute path in root', () {
249 expect(builder.relative('/'), '../..');
250 expect(builder.relative('/root'), '..');
251 expect(builder.relative('/root/path'), '.');
252 expect(builder.relative('/root/path/a'), 'a');
253 expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt');
254 expect(builder.relative('/root/a/b.txt'), '../a/b.txt');
255 });
256
257 test('given absolute path outside of root', () {
258 expect(builder.relative('/a/b'), '../../a/b');
259 expect(builder.relative('/root/path/a'), 'a');
260 expect(builder.relative('/root/path/a/b.txt'), 'a/b.txt');
261 expect(builder.relative('/root/a/b.txt'), '../a/b.txt');
262 });
263
264 test('given relative path', () {
265 // The path is considered relative to the root, so it basically just
266 // normalizes.
267 expect(builder.relative(''), '.');
268 expect(builder.relative('.'), '.');
269 expect(builder.relative('a'), 'a');
270 expect(builder.relative('a/b.txt'), 'a/b.txt');
271 expect(builder.relative('../a/b.txt'), '../a/b.txt');
272 expect(builder.relative('a/./b/../c.txt'), 'a/c.txt');
273 });
274 });
275
276 group('from relative root', () {
277 var r = new path.Builder(style: path.Style.posix, root: 'foo/bar');
278
279 test('given absolute path', () {
280 expect(r.relative('/'), equals('/'));
281 expect(r.relative('/a/b'), equals('/a/b'));
282 });
283
284 test('given relative path', () {
285 // The path is considered relative to the root, so it basically just
286 // normalizes.
287 expect(r.relative(''), '.');
288 expect(r.relative('.'), '.');
289 expect(r.relative('..'), '..');
290 expect(r.relative('a'), 'a');
291 expect(r.relative('a/b.txt'), 'a/b.txt');
292 expect(r.relative('../a/b.txt'), '../a/b.txt');
293 expect(r.relative('a/./b/../c.txt'), 'a/c.txt');
294 });
295 });
296
297 test('from a root with extension', () {
298 var r = new path.Builder(style: path.Style.posix, root: '/dir.ext');
299 expect(r.relative('/dir.ext/file'), 'file');
300 });
301
302 test('with a root parameter', () {
303 expect(builder.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz'));
304 expect(builder.relative('..', from: '/foo/bar'), equals('../../root'));
305 expect(builder.relative('/foo/bar/baz', from: 'foo/bar'),
306 equals('../../../../foo/bar/baz'));
307 expect(builder.relative('..', from: 'foo/bar'), equals('../../..'));
308 });
309
310 test('with a root parameter and a relative root', () {
311 var r = new path.Builder(style: path.Style.posix, root: 'relative/root');
312 expect(r.relative('/foo/bar/baz', from: '/foo/bar'), equals('baz'));
313 expect(() => r.relative('..', from: '/foo/bar'), throwsArgumentError);
314 expect(r.relative('/foo/bar/baz', from: 'foo/bar'),
315 equals('/foo/bar/baz'));
316 expect(r.relative('..', from: 'foo/bar'), equals('../../..'));
317 });
318 });
319
320 group('resolve', () {
321 test('allows up to seven parts', () {
322 expect(builder.resolve('a'), '/root/path/a');
323 expect(builder.resolve('a', 'b'), '/root/path/a/b');
324 expect(builder.resolve('a', 'b', 'c'), '/root/path/a/b/c');
325 expect(builder.resolve('a', 'b', 'c', 'd'), '/root/path/a/b/c/d');
326 expect(builder.resolve('a', 'b', 'c', 'd', 'e'), '/root/path/a/b/c/d/e');
327 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f'),
328 '/root/path/a/b/c/d/e/f');
329 expect(builder.resolve('a', 'b', 'c', 'd', 'e', 'f', 'g'),
330 '/root/path/a/b/c/d/e/f/g');
331 });
332
333 test('does not add separator if a part ends in one', () {
334 expect(builder.resolve('a/', 'b', 'c/', 'd'), '/root/path/a/b/c/d');
335 expect(builder.resolve(r'a\', 'b'), r'/root/path/a\/b');
336 });
337
338 test('ignores parts before an absolute path', () {
339 expect(builder.resolve('a', '/b', '/c', 'd'), '/c/d');
340 expect(builder.resolve('a', r'c:\b', 'c', 'd'), r'/root/path/a/c:\b/c/d');
341 expect(builder.resolve('a', r'\\b', 'c', 'd'), r'/root/path/a/\\b/c/d');
342 });
343 });
344
345 test('withoutExtension', () {
346 expect(builder.withoutExtension(''), '');
347 expect(builder.withoutExtension('a'), 'a');
348 expect(builder.withoutExtension('.a'), '.a');
349 expect(builder.withoutExtension('a.b'), 'a');
350 expect(builder.withoutExtension('a/b.c'), 'a/b');
351 expect(builder.withoutExtension('a/b.c.d'), 'a/b.c');
352 expect(builder.withoutExtension('a/'), 'a/');
353 expect(builder.withoutExtension('a/b/'), 'a/b/');
354 expect(builder.withoutExtension('a/.'), 'a/.');
355 expect(builder.withoutExtension('a/.b'), 'a/.b');
356 expect(builder.withoutExtension('a.b/c'), 'a.b/c');
357 expect(builder.withoutExtension(r'a.b\c'), r'a');
358 expect(builder.withoutExtension(r'a/b\c'), r'a/b\c');
359 expect(builder.withoutExtension(r'a/b\c.d'), r'a/b\c');
360 });
361 }
OLDNEW
« no previous file with comments | « utils/pub/validator/name.dart ('k') | utils/tests/pub/path/path_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698