Skip to content

Commit

Permalink
test: improve path tests
Browse files Browse the repository at this point in the history
This commit adds new tests, executes tests for other platforms
instead of limiting platform-specific tests to those platforms,
and fixes a few style/formatting inconsistencies.

PR-URL: #5123
Reviewed-By: Roman Reiss <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
mscdex authored and rvagg committed Feb 10, 2016
1 parent edf8f8a commit a8f4db2
Show file tree
Hide file tree
Showing 3 changed files with 356 additions and 183 deletions.
73 changes: 72 additions & 1 deletion test/parallel/test-path-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ var unixPaths = [
'.\\file',
'./file',
'C:\\foo',
''
'/',
'',
'.',
'..',
'/foo',
'/foo.',
'/foo.bar',
'/.',
'/.foo',
'/.foo.bar',
'/foo/bar.baz',
];

var unixSpecialCaseFormatTests = [
Expand Down Expand Up @@ -76,6 +86,67 @@ checkErrors(path.posix);
checkFormat(path.win32, winSpecialCaseFormatTests);
checkFormat(path.posix, unixSpecialCaseFormatTests);

// Test removal of trailing path separators
const trailingTests = [
[ path.win32.parse,
[['.\\', { root: '', dir: '', base: '.', ext: '', name: '.' }],
['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
['c:\\foo\\\\\\',
{ root: 'c:\\', dir: 'c:\\', base: 'foo', ext: '', name: 'foo' }],
['D:\\foo\\\\\\bar.baz',
{ root: 'D:\\',
dir: 'D:\\foo\\\\',
base: 'bar.baz',
ext: '.baz',
name: 'bar'
}
]
]
],
[ path.posix.parse,
[['./', { root: '', dir: '', base: '.', ext: '', name: '.' }],
['//', { root: '/', dir: '/', base: '', ext: '', name: '' }],
['///', { root: '/', dir: '/', base: '', ext: '', name: '' }],
['/foo///', { root: '/', dir: '/', base: 'foo', ext: '', name: 'foo' }],
['/foo///bar.baz',
{ root: '/', dir: '/foo//', base: 'bar.baz', ext: '.baz', name: 'bar' }
]
]
]
];
const failures = [];
trailingTests.forEach(function(test) {
const parse = test[0];
test[1].forEach(function(test) {
const actual = parse(test[0]);
const expected = test[1];
const fn = 'path.' +
(parse === path.win32.parse ? 'win32' : 'posix') +
'.parse(';
const message = fn +
JSON.stringify(test[0]) +
')' +
'\n expect=' + JSON.stringify(expected) +
'\n actual=' + JSON.stringify(actual);
const actualKeys = Object.keys(actual);
const expectedKeys = Object.keys(expected);
let failed = (actualKeys.length !== expectedKeys.length);
if (!failed) {
for (let i = 0; i < actualKeys.length; ++i) {
const key = actualKeys[i];
if (expectedKeys.indexOf(key) === -1 || actual[key] !== expected[key]) {
failed = true;
break;
}
}
}
if (failed)
failures.push('\n' + message);
});
});
assert.equal(failures.length, 0, failures.join(''));

function checkErrors(path) {
errors.forEach(function(errorCase) {
try {
Expand Down
12 changes: 8 additions & 4 deletions test/parallel/test-path-zero-length-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ const pwd = process.cwd();

// join will internally ignore all the zero-length strings and it will return
// '.' if the joined string is a zero-length string.
assert.equal(path.join(''), '.');
assert.equal(path.join('', ''), '.');
assert.equal(path.posix.join(''), '.');
assert.equal(path.posix.join('', ''), '.');
assert.equal(path.win32.join(''), '.');
assert.equal(path.win32.join('', ''), '.');
assert.equal(path.join(pwd), pwd);
assert.equal(path.join(pwd, ''), pwd);

// normalize will return '.' if the input is a zero-length string
assert.equal(path.normalize(''), '.');
assert.equal(path.posix.normalize(''), '.');
assert.equal(path.win32.normalize(''), '.');
assert.equal(path.normalize(pwd), pwd);

// Since '' is not a valid path in any of the common environments, return false
assert.equal(path.isAbsolute(''), false);
assert.equal(path.posix.isAbsolute(''), false);
assert.equal(path.win32.isAbsolute(''), false);

// resolve, internally ignores all the zero-length strings and returns the
// current working directory
Expand Down
Loading

0 comments on commit a8f4db2

Please sign in to comment.