test: add path.join's test
Add test when the argument is empty. Adjust the position of the comment. Make use of Arrow Function, Template Literals and `Array.from`. PR-URL: https://github.com/nodejs/node/pull/11063 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
6ca1bdfeb1
commit
81d15594d7
@ -59,8 +59,8 @@ assert.strictEqual(path.posix.basename('foo'), 'foo');
|
|||||||
|
|
||||||
// POSIX filenames may include control characters
|
// POSIX filenames may include control characters
|
||||||
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
|
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
|
||||||
const controlCharFilename = 'Icon' + String.fromCharCode(13);
|
const controlCharFilename = `Icon${String.fromCharCode(13)}`;
|
||||||
assert.strictEqual(path.posix.basename('/a/b/' + controlCharFilename),
|
assert.strictEqual(path.posix.basename(`/a/b/${controlCharFilename}`),
|
||||||
controlCharFilename);
|
controlCharFilename);
|
||||||
|
|
||||||
|
|
||||||
@ -160,8 +160,8 @@ assert.strictEqual(path.win32.dirname('foo'), '.');
|
|||||||
['file//', ''],
|
['file//', ''],
|
||||||
['file./', '.'],
|
['file./', '.'],
|
||||||
['file.//', '.'],
|
['file.//', '.'],
|
||||||
].forEach(function(test) {
|
].forEach((test) => {
|
||||||
[path.posix.extname, path.win32.extname].forEach(function(extname) {
|
[path.posix.extname, path.win32.extname].forEach((extname) => {
|
||||||
let input = test[0];
|
let input = test[0];
|
||||||
let os;
|
let os;
|
||||||
if (extname === path.win32.extname) {
|
if (extname === path.win32.extname) {
|
||||||
@ -208,6 +208,7 @@ const joinTests = [
|
|||||||
[ [path.posix.join, path.win32.join],
|
[ [path.posix.join, path.win32.join],
|
||||||
// arguments result
|
// arguments result
|
||||||
[[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
|
[[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
|
||||||
|
[[], '.'],
|
||||||
[['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
|
[['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
|
||||||
[['/foo', '../../../bar'], '/bar'],
|
[['/foo', '../../../bar'], '/bar'],
|
||||||
[['foo', '../../../bar'], '../../bar'],
|
[['foo', '../../../bar'], '../../bar'],
|
||||||
@ -310,11 +311,11 @@ joinTests.push([
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
]);
|
]);
|
||||||
joinTests.forEach(function(test) {
|
joinTests.forEach((test) => {
|
||||||
if (!Array.isArray(test[0]))
|
if (!Array.isArray(test[0]))
|
||||||
test[0] = [test[0]];
|
test[0] = [test[0]];
|
||||||
test[0].forEach(function(join) {
|
test[0].forEach((join) => {
|
||||||
test[1].forEach(function(test) {
|
test[1].forEach((test) => {
|
||||||
const actual = join.apply(null, test[0]);
|
const actual = join.apply(null, test[0]);
|
||||||
const expected = test[1];
|
const expected = test[1];
|
||||||
// For non-Windows specific tests with the Windows join(), we need to try
|
// For non-Windows specific tests with the Windows join(), we need to try
|
||||||
@ -333,7 +334,7 @@ joinTests.forEach(function(test) {
|
|||||||
'\n expect=' + JSON.stringify(expected) +
|
'\n expect=' + JSON.stringify(expected) +
|
||||||
'\n actual=' + JSON.stringify(actual);
|
'\n actual=' + JSON.stringify(actual);
|
||||||
if (actual !== expected && actualAlt !== expected)
|
if (actual !== expected && actualAlt !== expected)
|
||||||
failures.push('\n' + message);
|
failures.push(`\n${message}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -344,15 +345,15 @@ assert.strictEqual(failures.length, 0, failures.join(''));
|
|||||||
const typeErrorTests = [true, false, 7, null, {}, undefined, [], NaN];
|
const typeErrorTests = [true, false, 7, null, {}, undefined, [], NaN];
|
||||||
|
|
||||||
function fail(fn) {
|
function fail(fn) {
|
||||||
const args = Array.prototype.slice.call(arguments, 1);
|
const args = Array.from(arguments).slice(1);
|
||||||
|
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
fn.apply(null, args);
|
fn.apply(null, args);
|
||||||
}, TypeError);
|
}, TypeError);
|
||||||
}
|
}
|
||||||
|
|
||||||
typeErrorTests.forEach(function(test) {
|
typeErrorTests.forEach((test) => {
|
||||||
[path.posix, path.win32].forEach(function(namespace) {
|
[path.posix, path.win32].forEach((namespace) => {
|
||||||
fail(namespace.join, test);
|
fail(namespace.join, test);
|
||||||
fail(namespace.resolve, test);
|
fail(namespace.resolve, test);
|
||||||
fail(namespace.normalize, test);
|
fail(namespace.normalize, test);
|
||||||
@ -396,7 +397,7 @@ assert.strictEqual(path.posix.normalize('///..//./foo/.//bar'), '/foo/bar');
|
|||||||
// path.resolve tests
|
// path.resolve tests
|
||||||
const resolveTests = [
|
const resolveTests = [
|
||||||
[ path.win32.resolve,
|
[ path.win32.resolve,
|
||||||
// arguments result
|
// arguments result
|
||||||
[[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
|
[[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
|
||||||
[['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
|
[['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
|
||||||
[['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
|
[['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
|
||||||
@ -413,7 +414,7 @@ const resolveTests = [
|
|||||||
]
|
]
|
||||||
],
|
],
|
||||||
[ path.posix.resolve,
|
[ path.posix.resolve,
|
||||||
// arguments result
|
// arguments result
|
||||||
[[['/var/lib', '../', 'file/'], '/var/file'],
|
[[['/var/lib', '../', 'file/'], '/var/file'],
|
||||||
[['/var/lib', '/../', 'file/'], '/file'],
|
[['/var/lib', '/../', 'file/'], '/file'],
|
||||||
[['a/b/c/', '../../..'], process.cwd()],
|
[['a/b/c/', '../../..'], process.cwd()],
|
||||||
@ -423,9 +424,9 @@ const resolveTests = [
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
resolveTests.forEach(function(test) {
|
resolveTests.forEach((test) => {
|
||||||
const resolve = test[0];
|
const resolve = test[0];
|
||||||
test[1].forEach(function(test) {
|
test[1].forEach((test) => {
|
||||||
const actual = resolve.apply(null, test[0]);
|
const actual = resolve.apply(null, test[0]);
|
||||||
let actualAlt;
|
let actualAlt;
|
||||||
const os = resolve === path.win32.resolve ? 'win32' : 'posix';
|
const os = resolve === path.win32.resolve ? 'win32' : 'posix';
|
||||||
@ -514,7 +515,7 @@ const relativeTests = [
|
|||||||
]
|
]
|
||||||
],
|
],
|
||||||
[ path.posix.relative,
|
[ path.posix.relative,
|
||||||
// arguments result
|
// arguments result
|
||||||
[['/var/lib', '/var', '..'],
|
[['/var/lib', '/var', '..'],
|
||||||
['/var/lib', '/bin', '../../bin'],
|
['/var/lib', '/bin', '../../bin'],
|
||||||
['/var/lib', '/var/lib', ''],
|
['/var/lib', '/var/lib', ''],
|
||||||
@ -530,9 +531,9 @@ const relativeTests = [
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
relativeTests.forEach(function(test) {
|
relativeTests.forEach((test) => {
|
||||||
const relative = test[0];
|
const relative = test[0];
|
||||||
test[1].forEach(function(test) {
|
test[1].forEach((test) => {
|
||||||
const actual = relative(test[0], test[1]);
|
const actual = relative(test[0], test[1]);
|
||||||
const expected = test[2];
|
const expected = test[2];
|
||||||
const os = relative === path.win32.relative ? 'win32' : 'posix';
|
const os = relative === path.win32.relative ? 'win32' : 'posix';
|
||||||
@ -543,7 +544,7 @@ relativeTests.forEach(function(test) {
|
|||||||
'\n expect=' + JSON.stringify(expected) +
|
'\n expect=' + JSON.stringify(expected) +
|
||||||
'\n actual=' + JSON.stringify(actual);
|
'\n actual=' + JSON.stringify(actual);
|
||||||
if (actual !== expected)
|
if (actual !== expected)
|
||||||
failures.push('\n' + message);
|
failures.push(`\n${message}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
assert.strictEqual(failures.length, 0, failures.join(''));
|
assert.strictEqual(failures.length, 0, failures.join(''));
|
||||||
@ -575,14 +576,14 @@ if (common.isWindows) {
|
|||||||
// These tests cause resolve() to insert the cwd, so we cannot test them from
|
// These tests cause resolve() to insert the cwd, so we cannot test them from
|
||||||
// non-Windows platforms (easily)
|
// non-Windows platforms (easily)
|
||||||
assert.strictEqual(path.win32._makeLong('foo\\bar').toLowerCase(),
|
assert.strictEqual(path.win32._makeLong('foo\\bar').toLowerCase(),
|
||||||
'\\\\?\\' + process.cwd().toLowerCase() + '\\foo\\bar');
|
`\\\\?\\${process.cwd().toLowerCase()}\\foo\\bar`);
|
||||||
assert.strictEqual(path.win32._makeLong('foo/bar').toLowerCase(),
|
assert.strictEqual(path.win32._makeLong('foo/bar').toLowerCase(),
|
||||||
'\\\\?\\' + process.cwd().toLowerCase() + '\\foo\\bar');
|
`\\\\?\\${process.cwd().toLowerCase()}\\foo\\bar`);
|
||||||
const currentDeviceLetter = path.parse(process.cwd()).root.substring(0, 2);
|
const currentDeviceLetter = path.parse(process.cwd()).root.substring(0, 2);
|
||||||
assert.strictEqual(path.win32._makeLong(currentDeviceLetter).toLowerCase(),
|
assert.strictEqual(path.win32._makeLong(currentDeviceLetter).toLowerCase(),
|
||||||
'\\\\?\\' + process.cwd().toLowerCase());
|
`\\\\?\\${process.cwd().toLowerCase()}`);
|
||||||
assert.strictEqual(path.win32._makeLong('C').toLowerCase(),
|
assert.strictEqual(path.win32._makeLong('C').toLowerCase(),
|
||||||
'\\\\?\\' + process.cwd().toLowerCase() + '\\c');
|
`\\\\?\\${process.cwd().toLowerCase()}\\c`);
|
||||||
}
|
}
|
||||||
assert.strictEqual(path.win32._makeLong('C:\\foo'), '\\\\?\\C:\\foo');
|
assert.strictEqual(path.win32._makeLong('C:\\foo'), '\\\\?\\C:\\foo');
|
||||||
assert.strictEqual(path.win32._makeLong('C:/foo'), '\\\\?\\C:\\foo');
|
assert.strictEqual(path.win32._makeLong('C:/foo'), '\\\\?\\C:\\foo');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user