0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-28 22:46:31 +01:00
nodejs/test/parallel/test-path-parse-format.js
isaacs 3e1b1dd4a9 Remove excessive copyright/license boilerplate
The copyright and license notice is already in the LICENSE file.  There
is no justifiable reason to also require that it be included in every
file, since the individual files are not individually distributed except
as part of the entire package.
2015-01-12 15:30:28 -08:00

79 lines
2.7 KiB
JavaScript

var assert = require('assert');
var path = require('path');
var winPaths = [
'C:\\path\\dir\\index.html',
'C:\\another_path\\DIR\\1\\2\\33\\index',
'another_path\\DIR with spaces\\1\\2\\33\\index',
'\\foo\\C:',
'file',
'.\\file',
// unc
'\\\\server\\share\\file_path',
'\\\\server two\\shared folder\\file path.zip',
'\\\\teela\\admin$\\system32',
'\\\\?\\UNC\\server\\share'
];
var unixPaths = [
'/home/user/dir/file.txt',
'/home/user/a dir/another File.zip',
'/home/user/a dir//another&File.',
'/home/user/a$$$dir//another File.zip',
'user/dir/another File.zip',
'file',
'.\\file',
'./file',
'C:\\foo'
];
var errors = [
{method: 'parse', input: [null], message: /Parameter 'pathString' must be a string, not/},
{method: 'parse', input: [{}], message: /Parameter 'pathString' must be a string, not object/},
{method: 'parse', input: [true], message: /Parameter 'pathString' must be a string, not boolean/},
{method: 'parse', input: [1], message: /Parameter 'pathString' must be a string, not number/},
{method: 'parse', input: [], message: /Parameter 'pathString' must be a string, not undefined/},
// {method: 'parse', input: [''], message: /Invalid path/}, // omitted because it's hard to trigger!
{method: 'format', input: [null], message: /Parameter 'pathObject' must be an object, not/},
{method: 'format', input: [''], message: /Parameter 'pathObject' must be an object, not string/},
{method: 'format', input: [true], message: /Parameter 'pathObject' must be an object, not boolean/},
{method: 'format', input: [1], message: /Parameter 'pathObject' must be an object, not number/},
{method: 'format', input: [{root: true}], message: /'pathObject.root' must be a string or undefined, not boolean/},
{method: 'format', input: [{root: 12}], message: /'pathObject.root' must be a string or undefined, not number/},
];
check(path.win32, winPaths);
check(path.posix, unixPaths);
checkErrors(path.win32);
checkErrors(path.posix);
function checkErrors(path) {
errors.forEach(function(errorCase) {
try {
path[errorCase.method].apply(path, errorCase.input);
} catch(err) {
assert.ok(err instanceof TypeError);
assert.ok(
errorCase.message.test(err.message),
'expected ' + errorCase.message + ' to match ' + err.message
);
return;
}
assert.fail('should have thrown');
});
}
function check(path, paths) {
paths.forEach(function(element, index, array) {
var output = path.parse(element);
assert.strictEqual(path.format(output), element);
assert.strictEqual(output.dir, output.dir ? path.dirname(element) : '');
assert.strictEqual(output.base, path.basename(element));
assert.strictEqual(output.ext, path.extname(element));
});
}