diff options
author | Rich Trott <rtrott@gmail.com> | 2016-04-19 15:37:45 -0700 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2016-04-22 14:38:09 -0700 |
commit | a7335bd1f048f6592f609eec0c87c007e98d754c (patch) | |
tree | 1f888003dea7c3fb67366ac1a94f418eb7acb168 /test/parallel/test-querystring.js | |
parent | 5eb4ec090d70a848e2ae98bd526634bf9d1bf08c (diff) | |
download | android-node-v8-a7335bd1f048f6592f609eec0c87c007e98d754c.tar.gz android-node-v8-a7335bd1f048f6592f609eec0c87c007e98d754c.tar.bz2 android-node-v8-a7335bd1f048f6592f609eec0c87c007e98d754c.zip |
test,benchmark: use deepStrictEqual()
In preparation for a lint rule that will enforce
assert.deepStrictEqual() over assert.deepEqual(), change tests and
benchmarks accordingly. For tests and benchmarks that are testing or
benchmarking assert.deepEqual() itself, apply a comment to ignore the
upcoming rule.
PR-URL: https://github.com/nodejs/node/pull/6213
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'test/parallel/test-querystring.js')
-rw-r--r-- | test/parallel/test-querystring.js | 63 |
1 files changed, 48 insertions, 15 deletions
diff --git a/test/parallel/test-querystring.js b/test/parallel/test-querystring.js index 019d922a37..5d52fe6544 100644 --- a/test/parallel/test-querystring.js +++ b/test/parallel/test-querystring.js @@ -5,13 +5,20 @@ var assert = require('assert'); // test using assert var qs = require('querystring'); +function createWithNoPrototype(properties) { + const noProto = Object.create(null); + properties.forEach((property) => { + noProto[property.key] = property.value; + }); + return noProto; +} // folding block, commented to pass gjslint // {{{ // [ wonkyQS, canonicalQS, obj ] var qsTestCases = [ ['__proto__=1', '__proto__=1', - JSON.parse('{"__proto__":"1"}')], + createWithNoPrototype([{key: '__proto__', value: '1'}])], ['__defineGetter__=asdf', '__defineGetter__=asdf', JSON.parse('{"__defineGetter__":"asdf"}')], @@ -97,37 +104,64 @@ var qsNoMungeTestCases = [ assert.strictEqual('918854443121279438895193', qs.parse('id=918854443121279438895193').id); + +function check(actual, expected) { + assert(!(actual instanceof Object)); + assert.deepStrictEqual(Object.keys(actual).sort(), + Object.keys(expected).sort()); + Object.keys(expected).forEach(function(key) { + assert.deepStrictEqual(actual[key], expected[key]); + }); +} + // test that the canonical qs is parsed properly. qsTestCases.forEach(function(testCase) { - assert.deepEqual(testCase[2], qs.parse(testCase[0])); + check(qs.parse(testCase[0]), testCase[2]); }); // test that the colon test cases can do the same qsColonTestCases.forEach(function(testCase) { - assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':')); + check(qs.parse(testCase[0], ';', ':'), testCase[2]); }); // test the weird objects, that they get parsed properly qsWeirdObjects.forEach(function(testCase) { - assert.deepEqual(testCase[2], qs.parse(testCase[1])); + check(qs.parse(testCase[1]), testCase[2]); }); qsNoMungeTestCases.forEach(function(testCase) { - assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=')); + assert.deepStrictEqual(testCase[0], qs.stringify(testCase[1], '&', '=')); }); // test the nested qs-in-qs case (function() { - var f = qs.parse('a=b&q=x%3Dy%26y%3Dz'); + const f = qs.parse('a=b&q=x%3Dy%26y%3Dz'); + check(f, createWithNoPrototype([ + { key: 'a', value: 'b'}, + {key: 'q', value: 'x=y&y=z'} + ])); + f.q = qs.parse(f.q); - assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } }); + const expectedInternal = createWithNoPrototype([ + { key: 'x', value: 'y'}, + {key: 'y', value: 'z' } + ]); + check(f.q, expectedInternal); })(); // nested in colon (function() { - var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':'); + const f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':'); + check(f, createWithNoPrototype([ + {key: 'a', value: 'b'}, + {key: 'q', value: 'x:y;y:z'} + ])); f.q = qs.parse(f.q, ';', ':'); - assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } }); + const expectedInternal = createWithNoPrototype([ + { key: 'x', value: 'y'}, + {key: 'y', value: 'z' } + ]); + check(f.q, expectedInternal); })(); // now test stringifying @@ -186,7 +220,7 @@ assert.doesNotThrow(function() { assert.equal(f, 'a:b;q:x%3Ay%3By%3Az'); } -assert.deepEqual({}, qs.parse()); +check(qs.parse(), {}); // Test limiting @@ -238,9 +272,8 @@ assert.equal(0xe6, b[19]); function demoDecode(str) { return str + str; } -assert.deepEqual( - qs.parse('a=a&b=b&c=c', null, null, { decodeURIComponent: demoDecode }), - { aa: 'aa', bb: 'bb', cc: 'cc' }); +check(qs.parse('a=a&b=b&c=c', null, null, { decodeURIComponent: demoDecode }), + { aa: 'aa', bb: 'bb', cc: 'cc' }); // Test custom encode @@ -257,8 +290,8 @@ var prevUnescape = qs.unescape; qs.unescape = function(str) { return str.replace(/o/g, '_'); }; -assert.deepEqual(qs.parse('foo=bor'), {f__: 'b_r'}); +check(qs.parse('foo=bor'), createWithNoPrototype([{key: 'f__', value: 'b_r'}])); qs.unescape = prevUnescape; // test separator and "equals" parsing order -assert.deepEqual(qs.parse('foo&bar', '&', '&'), { foo: '', bar: '' }); +check(qs.parse('foo&bar', '&', '&'), { foo: '', bar: '' }); |