Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6562516

Browse files
BridgeARBethGriggs
authored andcommittedApr 4, 2019
test: refactor path parse test
Use destructuring and arrow functions and make one test stricter. Also inline the error object as there's only a sinlge error that can currently be thrown in the path module. PR-URL: #26912 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent e7dab5b commit 6562516

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed
 

‎test/parallel/test-path-parse-format.js

+23-30
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const winPaths = [
5151
];
5252

5353
const winSpecialCaseParseTests = [
54-
['/foo/bar', { root: '/' }],
54+
['/foo/bar', { root: '/', dir: '/foo', base: 'bar', ext: '', name: 'bar' }],
5555
];
5656

5757
const winSpecialCaseFormatTests = [
@@ -98,21 +98,16 @@ const unixSpecialCaseFormatTests = [
9898
[{}, '']
9999
];
100100

101-
const expectedMessage = common.expectsError({
102-
code: 'ERR_INVALID_ARG_TYPE',
103-
type: TypeError
104-
}, 18);
105-
106101
const errors = [
107-
{ method: 'parse', input: [null], message: expectedMessage },
108-
{ method: 'parse', input: [{}], message: expectedMessage },
109-
{ method: 'parse', input: [true], message: expectedMessage },
110-
{ method: 'parse', input: [1], message: expectedMessage },
111-
{ method: 'parse', input: [], message: expectedMessage },
112-
{ method: 'format', input: [null], message: expectedMessage },
113-
{ method: 'format', input: [''], message: expectedMessage },
114-
{ method: 'format', input: [true], message: expectedMessage },
115-
{ method: 'format', input: [1], message: expectedMessage },
102+
{ method: 'parse', input: [null] },
103+
{ method: 'parse', input: [{}] },
104+
{ method: 'parse', input: [true] },
105+
{ method: 'parse', input: [1] },
106+
{ method: 'parse', input: [] },
107+
{ method: 'format', input: [null] },
108+
{ method: 'format', input: [''] },
109+
{ method: 'format', input: [true] },
110+
{ method: 'format', input: [1] },
116111
];
117112

118113
checkParseFormat(path.win32, winPaths);
@@ -153,10 +148,10 @@ const trailingTests = [
153148
]
154149
];
155150
const failures = [];
156-
trailingTests.forEach(function(test) {
151+
trailingTests.forEach((test) => {
157152
const parse = test[0];
158153
const os = parse === path.win32.parse ? 'win32' : 'posix';
159-
test[1].forEach(function(test) {
154+
test[1].forEach((test) => {
160155
const actual = parse(test[0]);
161156
const expected = test[1];
162157
const message = `path.${os}.parse(${JSON.stringify(test[0])})\n expect=${
@@ -180,15 +175,18 @@ trailingTests.forEach(function(test) {
180175
assert.strictEqual(failures.length, 0, failures.join(''));
181176

182177
function checkErrors(path) {
183-
errors.forEach(function(errorCase) {
178+
errors.forEach(({ method, input }) => {
184179
assert.throws(() => {
185-
path[errorCase.method].apply(path, errorCase.input);
186-
}, errorCase.message);
180+
path[method].apply(path, input);
181+
}, {
182+
code: 'ERR_INVALID_ARG_TYPE',
183+
name: 'TypeError'
184+
});
187185
});
188186
}
189187

190188
function checkParseFormat(path, paths) {
191-
paths.forEach(function([element, root]) {
189+
paths.forEach(([element, root]) => {
192190
const output = path.parse(element);
193191
assert.strictEqual(typeof output.root, 'string');
194192
assert.strictEqual(typeof output.dir, 'string');
@@ -205,19 +203,14 @@ function checkParseFormat(path, paths) {
205203
}
206204

207205
function checkSpecialCaseParseFormat(path, testCases) {
208-
testCases.forEach(function(testCase) {
209-
const element = testCase[0];
210-
const expect = testCase[1];
211-
const output = path.parse(element);
212-
Object.keys(expect).forEach(function(key) {
213-
assert.strictEqual(output[key], expect[key]);
214-
});
206+
testCases.forEach(([element, expect]) => {
207+
assert.deepStrictEqual(path.parse(element), expect);
215208
});
216209
}
217210

218211
function checkFormat(path, testCases) {
219-
testCases.forEach(function(testCase) {
220-
assert.strictEqual(path.format(testCase[0]), testCase[1]);
212+
testCases.forEach(([element, expect]) => {
213+
assert.strictEqual(path.format(element), expect);
221214
});
222215

223216
[null, undefined, 1, true, false, 'string'].forEach((pathObject) => {

0 commit comments

Comments
 (0)
Please sign in to comment.