From 9dd633a2e5b4b363df76f3556b70f92732f941cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szewczak?= Date: Tue, 25 Apr 2017 20:03:58 +0200 Subject: [PATCH 1/2] test: change URIError constructor to regular expression in assert.throws --- test/parallel/test-querystring.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-querystring.js b/test/parallel/test-querystring.js index 6d8dd2a7f262ef..4715f40d77fb1b 100644 --- a/test/parallel/test-querystring.js +++ b/test/parallel/test-querystring.js @@ -254,7 +254,7 @@ qsWeirdObjects.forEach(function(testCase) { // invalid surrogate pair throws URIError assert.throws(function() { qs.stringify({ foo: '\udc00' }); -}, URIError); +}, /^URIError: URI malformed$/); // coerce numbers to string assert.strictEqual('foo=0', qs.stringify({ foo: 0 })); From b8393d047d495a760d5174d99eddb95f7f7e655d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szewczak?= Date: Tue, 25 Apr 2017 22:43:37 +0200 Subject: [PATCH 2/2] test: use block-scope for tests that spans multiple statements --- test/parallel/test-querystring.js | 122 +++++++++++++++++------------- 1 file changed, 69 insertions(+), 53 deletions(-) diff --git a/test/parallel/test-querystring.js b/test/parallel/test-querystring.js index 4715f40d77fb1b..82f253fa8b43b6 100644 --- a/test/parallel/test-querystring.js +++ b/test/parallel/test-querystring.js @@ -318,43 +318,47 @@ assert.strictEqual( 0); // Test removing limit -function testUnlimitedKeys() { - const query = {}; +{ + const testUnlimitedKeys = function() { + const query = {}; - for (let i = 0; i < 2000; i++) query[i] = i; + for (let i = 0; i < 2000; i++) query[i] = i; - const url = qs.stringify(query); + const url = qs.stringify(query); - assert.strictEqual( - Object.keys(qs.parse(url, null, null, { maxKeys: 0 })).length, + assert.strictEqual( + Object.keys(qs.parse(url, null, null, {maxKeys: 0})).length, 2000); -} -testUnlimitedKeys(); + }; + testUnlimitedKeys(); +} -const b = qs.unescapeBuffer('%d3%f2Ug%1f6v%24%5e%98%cb' + - '%0d%ac%a2%2f%9d%eb%d8%a2%e6'); +{ + const b = qs.unescapeBuffer('%d3%f2Ug%1f6v%24%5e%98%cb' + + '%0d%ac%a2%2f%9d%eb%d8%a2%e6'); // -assert.strictEqual(0xd3, b[0]); -assert.strictEqual(0xf2, b[1]); -assert.strictEqual(0x55, b[2]); -assert.strictEqual(0x67, b[3]); -assert.strictEqual(0x1f, b[4]); -assert.strictEqual(0x36, b[5]); -assert.strictEqual(0x76, b[6]); -assert.strictEqual(0x24, b[7]); -assert.strictEqual(0x5e, b[8]); -assert.strictEqual(0x98, b[9]); -assert.strictEqual(0xcb, b[10]); -assert.strictEqual(0x0d, b[11]); -assert.strictEqual(0xac, b[12]); -assert.strictEqual(0xa2, b[13]); -assert.strictEqual(0x2f, b[14]); -assert.strictEqual(0x9d, b[15]); -assert.strictEqual(0xeb, b[16]); -assert.strictEqual(0xd8, b[17]); -assert.strictEqual(0xa2, b[18]); -assert.strictEqual(0xe6, b[19]); + assert.strictEqual(0xd3, b[0]); + assert.strictEqual(0xf2, b[1]); + assert.strictEqual(0x55, b[2]); + assert.strictEqual(0x67, b[3]); + assert.strictEqual(0x1f, b[4]); + assert.strictEqual(0x36, b[5]); + assert.strictEqual(0x76, b[6]); + assert.strictEqual(0x24, b[7]); + assert.strictEqual(0x5e, b[8]); + assert.strictEqual(0x98, b[9]); + assert.strictEqual(0xcb, b[10]); + assert.strictEqual(0x0d, b[11]); + assert.strictEqual(0xac, b[12]); + assert.strictEqual(0xa2, b[13]); + assert.strictEqual(0x2f, b[14]); + assert.strictEqual(0x9d, b[15]); + assert.strictEqual(0xeb, b[16]); + assert.strictEqual(0xd8, b[17]); + assert.strictEqual(0xa2, b[18]); + assert.strictEqual(0xe6, b[19]); +} assert.strictEqual(qs.unescapeBuffer('a+b', true).toString(), 'a b'); assert.strictEqual(qs.unescapeBuffer('a+b').toString(), 'a+b'); @@ -368,29 +372,38 @@ assert.strictEqual(qs.unescapeBuffer('a%%').toString(), 'a%%'); check(qs.parse('%\u0100=%\u0101'), { '%Ā': '%ā' }); // Test custom decode -function demoDecode(str) { - return str + str; +{ + const demoDecode = function(str) { + return str + str; + }; + + check(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, '==', {decodeURIComponent: (str) => str}), + {'a=a': '', 'b=b': '', 'c=c': ''}); } -check(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, '==', { decodeURIComponent: (str) => str }), - { 'a=a': '', 'b=b': '', 'c=c': '' }); // Test QueryString.unescape -function errDecode(str) { - throw new Error('To jump to the catch scope'); +{ + const errDecode = function(str) { + throw new Error('To jump to the catch scope'); + }; + + check(qs.parse('a=a', null, null, {decodeURIComponent: errDecode}), + {a: 'a'}); } -check(qs.parse('a=a', null, null, { decodeURIComponent: errDecode }), - { a: 'a' }); // Test custom encode -function demoEncode(str) { - return str[0]; +{ + const demoEncode = function(str) { + return str[0]; + }; + + const obj = {aa: 'aa', bb: 'bb', cc: 'cc'}; + assert.strictEqual( + qs.stringify(obj, null, null, {encodeURIComponent: demoEncode}), + 'a=a&b=b&c=c'); } -const obj = { aa: 'aa', bb: 'bb', cc: 'cc' }; -assert.strictEqual( - qs.stringify(obj, null, null, { encodeURIComponent: demoEncode }), - 'a=a&b=b&c=c'); // Test QueryString.unescapeBuffer qsUnescapeTestCases.forEach(function(testCase) { @@ -399,12 +412,15 @@ qsUnescapeTestCases.forEach(function(testCase) { }); // test overriding .unescape -const prevUnescape = qs.unescape; -qs.unescape = function(str) { - return str.replace(/o/g, '_'); -}; -check(qs.parse('foo=bor'), createWithNoPrototype([{key: 'f__', value: 'b_r'}])); -qs.unescape = prevUnescape; - +{ + const prevUnescape = qs.unescape; + qs.unescape = function(str) { + return str.replace(/o/g, '_'); + }; + check( + qs.parse('foo=bor'), + createWithNoPrototype([{key: 'f__', value: 'b_r'}])); + qs.unescape = prevUnescape; +} // test separator and "equals" parsing order check(qs.parse('foo&bar', '&', '&'), { foo: '', bar: '' });