Skip to content

Commit 4df8c2a

Browse files
committed
Port test-path-parse-format.
1 parent 153e907 commit 4df8c2a

File tree

2 files changed

+237
-2
lines changed

2 files changed

+237
-2
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ var posix = {
436436
},
437437

438438
format: function format(pathObject) {
439-
if (pathObject === null || typeof pathObject === 'undefined') {
440-
throw new TypeError('Parameter "pathObject" must be an object, not ' + (typeof pathObject));
439+
if (pathObject === null || typeof pathObject !== 'object') {
440+
throw new TypeError('The "pathObject" argument must be of type Object. Received type ' + typeof pathObject);
441441
}
442442
return _format('/', pathObject);
443443
},

test/test-path-parse-format.js

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
'use strict';
23+
var tape = require('tape');
24+
var path = require('../');
25+
26+
var winPaths = [
27+
// [path, root]
28+
['C:\\path\\dir\\index.html', 'C:\\'],
29+
['C:\\another_path\\DIR\\1\\2\\33\\\\index', 'C:\\'],
30+
['another_path\\DIR with spaces\\1\\2\\33\\index', ''],
31+
['\\', '\\'],
32+
['\\foo\\C:', '\\'],
33+
['file', ''],
34+
['file:stream', ''],
35+
['.\\file', ''],
36+
['C:', 'C:'],
37+
['C:.', 'C:'],
38+
['C:..', 'C:'],
39+
['C:abc', 'C:'],
40+
['C:\\', 'C:\\'],
41+
['C:\\abc', 'C:\\' ],
42+
['', ''],
43+
44+
// unc
45+
['\\\\server\\share\\file_path', '\\\\server\\share\\'],
46+
['\\\\server two\\shared folder\\file path.zip',
47+
'\\\\server two\\shared folder\\'],
48+
['\\\\teela\\admin$\\system32', '\\\\teela\\admin$\\'],
49+
['\\\\?\\UNC\\server\\share', '\\\\?\\UNC\\']
50+
];
51+
52+
var winSpecialCaseParseTests = [
53+
['/foo/bar', { root: '/' }],
54+
];
55+
56+
var winSpecialCaseFormatTests = [
57+
[{ dir: 'some\\dir' }, 'some\\dir\\'],
58+
[{ base: 'index.html' }, 'index.html'],
59+
[{ root: 'C:\\' }, 'C:\\'],
60+
[{ name: 'index', ext: '.html' }, 'index.html'],
61+
[{ dir: 'some\\dir', name: 'index', ext: '.html' }, 'some\\dir\\index.html'],
62+
[{ root: 'C:\\', name: 'index', ext: '.html' }, 'C:\\index.html'],
63+
[{}, '']
64+
];
65+
66+
var unixPaths = [
67+
// [path, root]
68+
['/home/user/dir/file.txt', '/'],
69+
['/home/user/a dir/another File.zip', '/'],
70+
['/home/user/a dir//another&File.', '/'],
71+
['/home/user/a$$$dir//another File.zip', '/'],
72+
['user/dir/another File.zip', ''],
73+
['file', ''],
74+
['.\\file', ''],
75+
['./file', ''],
76+
['C:\\foo', ''],
77+
['/', '/'],
78+
['', ''],
79+
['.', ''],
80+
['..', ''],
81+
['/foo', '/'],
82+
['/foo.', '/'],
83+
['/foo.bar', '/'],
84+
['/.', '/'],
85+
['/.foo', '/'],
86+
['/.foo.bar', '/'],
87+
['/foo/bar.baz', '/']
88+
];
89+
90+
var unixSpecialCaseFormatTests = [
91+
[{ dir: 'some/dir' }, 'some/dir/'],
92+
[{ base: 'index.html' }, 'index.html'],
93+
[{ root: '/' }, '/'],
94+
[{ name: 'index', ext: '.html' }, 'index.html'],
95+
[{ dir: 'some/dir', name: 'index', ext: '.html' }, 'some/dir/index.html'],
96+
[{ root: '/', name: 'index', ext: '.html' }, '/index.html'],
97+
[{}, '']
98+
];
99+
100+
var errors = [
101+
{ method: 'parse', input: [null], message: TypeError },
102+
{ method: 'parse', input: [{}], message: TypeError },
103+
{ method: 'parse', input: [true], message: TypeError },
104+
{ method: 'parse', input: [1], message: TypeError },
105+
{ method: 'parse', input: [], message: TypeError },
106+
{ method: 'format', input: [null], message: TypeError },
107+
{ method: 'format', input: [''], message: TypeError },
108+
{ method: 'format', input: [true], message: TypeError },
109+
{ method: 'format', input: [1], message: TypeError },
110+
];
111+
112+
tape('path.win32.parse', { skip: true }, function (t) {
113+
checkParseFormat(t, path.win32, winPaths);
114+
checkSpecialCaseParseFormat(t, path.win32, winSpecialCaseParseTests);
115+
t.end();
116+
});
117+
118+
tape('path.posix.parse', function (t) {
119+
checkParseFormat(t, path.posix, unixPaths);
120+
t.end();
121+
});
122+
123+
tape('path.win32.parse errors', { skip: true }, function (t) {
124+
checkErrors(t, path.win32);
125+
t.end();
126+
});
127+
128+
tape('path.posix.parse errors', function (t) {
129+
checkErrors(t, path.posix);
130+
t.end();
131+
});
132+
133+
tape('path.win32.format', { skip: true }, function (t) {
134+
checkFormat(t, path.win32, winSpecialCaseFormatTests);
135+
t.end();
136+
});
137+
138+
tape('path.posix.format', function (t) {
139+
checkFormat(t, path.posix, unixSpecialCaseFormatTests);
140+
t.end();
141+
});
142+
143+
// Test removal of trailing path separators
144+
var windowsTrailingTests =
145+
[['.\\', { root: '', dir: '', base: '.', ext: '', name: '.' }],
146+
['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
147+
['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
148+
['c:\\foo\\\\\\',
149+
{ root: 'c:\\', dir: 'c:\\', base: 'foo', ext: '', name: 'foo' }],
150+
['D:\\foo\\\\\\bar.baz',
151+
{ root: 'D:\\',
152+
dir: 'D:\\foo\\\\',
153+
base: 'bar.baz',
154+
ext: '.baz',
155+
name: 'bar'
156+
}
157+
]
158+
];
159+
var posixTrailingTests =
160+
[['./', { root: '', dir: '', base: '.', ext: '', name: '.' }],
161+
['//', { root: '/', dir: '/', base: '', ext: '', name: '' }],
162+
['///', { root: '/', dir: '/', base: '', ext: '', name: '' }],
163+
['/foo///', { root: '/', dir: '/', base: 'foo', ext: '', name: 'foo' }],
164+
['/foo///bar.baz',
165+
{ root: '/', dir: '/foo//', base: 'bar.baz', ext: '.baz', name: 'bar' }
166+
]
167+
];
168+
169+
tape('path.win32.parse trailing', { skip: true }, function (t) {
170+
windowsTrailingTests.forEach(function (p) {
171+
var actual = path.win32.parse(p[0]);
172+
var expected = p[1];
173+
t.deepEqual(actual, expected)
174+
});
175+
t.end();
176+
});
177+
178+
tape('path.posix.parse trailing', function (t) {
179+
posixTrailingTests.forEach(function (p) {
180+
var actual = path.posix.parse(p[0]);
181+
var expected = p[1];
182+
t.deepEqual(actual, expected)
183+
});
184+
t.end();
185+
});
186+
187+
function checkErrors(t, path) {
188+
errors.forEach(function(errorCase) {
189+
t.throws(function () {
190+
path[errorCase.method].apply(path, errorCase.input);
191+
}, errorCase.message);
192+
});
193+
}
194+
195+
function checkParseFormat(t, path, paths) {
196+
paths.forEach(function(p) {
197+
var element = p[0];
198+
var root = p[1];
199+
var output = path.parse(element);
200+
t.strictEqual(typeof output.root, 'string');
201+
t.strictEqual(typeof output.dir, 'string');
202+
t.strictEqual(typeof output.base, 'string');
203+
t.strictEqual(typeof output.ext, 'string');
204+
t.strictEqual(typeof output.name, 'string');
205+
t.strictEqual(path.format(output), element);
206+
t.strictEqual(output.root, root);
207+
t.ok(output.dir.startsWith(output.root));
208+
t.strictEqual(output.dir, output.dir ? path.dirname(element) : '');
209+
t.strictEqual(output.base, path.basename(element));
210+
t.strictEqual(output.ext, path.extname(element));
211+
});
212+
}
213+
214+
function checkSpecialCaseParseFormat(t, path, testCases) {
215+
testCases.forEach(function(testCase) {
216+
var element = testCase[0];
217+
var expect = testCase[1];
218+
var output = path.parse(element);
219+
Object.keys(expect).forEach(function(key) {
220+
t.strictEqual(output[key], expect[key]);
221+
});
222+
});
223+
}
224+
225+
function checkFormat(t, path, testCases) {
226+
testCases.forEach(function(testCase) {
227+
t.strictEqual(path.format(testCase[0]), testCase[1]);
228+
});
229+
230+
[null, undefined, 1, true, false, 'string'].forEach(function (pathObject) {
231+
t.throws(function() {
232+
path.format(pathObject);
233+
}, /The "pathObject" argument must be of type Object. Received type (\w+)/);
234+
});
235+
}

0 commit comments

Comments
 (0)