diff options
author | Nathan Woltman <nwoltman@outlook.com> | 2015-05-23 00:42:12 -0400 |
---|---|---|
committer | Roman Reiss <me@silverwind.io> | 2015-07-04 13:24:59 +0200 |
commit | bca53dce76f98aba9bcc3b4cc2ba0f004bfc6aae (patch) | |
tree | df7f7e919515efc158c937af4926c4f229155372 /test/parallel/test-path-parse-format.js | |
parent | 46140334cd68e56e7ab2c5125b90ca52dd6b46f2 (diff) | |
download | android-node-v8-bca53dce76f98aba9bcc3b4cc2ba0f004bfc6aae.tar.gz android-node-v8-bca53dce76f98aba9bcc3b4cc2ba0f004bfc6aae.tar.bz2 android-node-v8-bca53dce76f98aba9bcc3b4cc2ba0f004bfc6aae.zip |
path: refactor for performance and consistency
Improve performance by:
+ Not leaking the `arguments` object!
+ Getting the last character of a string by index, instead of
with `.substr()` or `.slice()`
Improve code consistency by:
+ Using `[]` instead of `.charAt()` where possible
+ Using a function declaration instead of a var declaration
+ Using `.slice()` with clearer arguments
+ Checking if `dir` is truthy in `win32.format`
(added tests for this)
Improve both by:
+ Making the reusable `trimArray()` function
+ Standardizing getting certain path statistics with
the new `win32StatPath()` function
PR-URL: https://github.com/nodejs/io.js/pull/1778
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Diffstat (limited to 'test/parallel/test-path-parse-format.js')
-rw-r--r-- | test/parallel/test-path-parse-format.js | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/test/parallel/test-path-parse-format.js b/test/parallel/test-path-parse-format.js index 37f37fc9b5..677bf3241f 100644 --- a/test/parallel/test-path-parse-format.js +++ b/test/parallel/test-path-parse-format.js @@ -15,7 +15,12 @@ var winPaths = [ '\\\\server two\\shared folder\\file path.zip', '\\\\teela\\admin$\\system32', '\\\\?\\UNC\\server\\share' +]; +var winSpecialCaseFormatTests = [ + [{dir: 'some\\dir'}, 'some\\dir\\'], + [{base: 'index.html'}, 'index.html'], + [{}, ''] ]; var unixPaths = [ @@ -30,6 +35,12 @@ var unixPaths = [ 'C:\\foo' ]; +var unixSpecialCaseFormatTests = [ + [{dir: 'some/dir'}, 'some/dir/'], + [{base: 'index.html'}, 'index.html'], + [{}, ''] +]; + var errors = [ {method: 'parse', input: [null], message: /Path must be a string. Received null/}, @@ -57,10 +68,12 @@ var errors = [ message: /'pathObject.root' must be a string or undefined, not number/}, ]; -check(path.win32, winPaths); -check(path.posix, unixPaths); +checkParseFormat(path.win32, winPaths); +checkParseFormat(path.posix, unixPaths); checkErrors(path.win32); checkErrors(path.posix); +checkFormat(path.win32, winSpecialCaseFormatTests); +checkFormat(path.posix, unixSpecialCaseFormatTests); function checkErrors(path) { errors.forEach(function(errorCase) { @@ -79,8 +92,7 @@ function checkErrors(path) { }); } - -function check(path, paths) { +function checkParseFormat(path, paths) { paths.forEach(function(element, index, array) { var output = path.parse(element); assert.strictEqual(path.format(output), element); @@ -89,3 +101,9 @@ function check(path, paths) { assert.strictEqual(output.ext, path.extname(element)); }); } + +function checkFormat(path, testCases) { + testCases.forEach(function(testCase) { + assert.strictEqual(path.format(testCase[0]), testCase[1]); + }); +} |