From a0274ddc47598ce92f708d185a2e4d746a6f328d Mon Sep 17 00:00:00 2001 From: Denis Sokolov Date: Fri, 5 Jun 2015 13:56:09 +0300 Subject: [PATCH] Add a failing test for reliance on Object.keys --- package.json | 1 + test/named-parameter-order-test.js | 36 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 test/named-parameter-order-test.js diff --git a/package.json b/package.json index 4e15b49..6ab5ded 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ }, "homepage": "https://github.com/jo/docuri", "devDependencies": { + "shuffle-array": "^0.1.0", "tap": "^0.4.8" } } diff --git a/test/named-parameter-order-test.js b/test/named-parameter-order-test.js new file mode 100644 index 0000000..bc23bb4 --- /dev/null +++ b/test/named-parameter-order-test.js @@ -0,0 +1,36 @@ +var shuffle = require('shuffle-array'); +var test = require('tap').test; +var docuri = require('..'); + +var simulateWrongOrderObjectKeys = function(cb){ + var orig = Object.keys; + Object.keys = function(obj){ + var keys = orig(obj); + var real = keys.toString(); + while (real === keys.toString()) + shuffle(keys); + return keys; + }; + try { + cb(); + } catch (err) { + Object.keys = orig; + throw err; + } + Object.keys = orig; +}; + +test('named parameters are not reliant on Object.keys order', function(t) { + var page = docuri.route('page/:foo/:bar/:quux/:baz'); + + simulateWrongOrderObjectKeys(function(){ + t.deepEqual( + page('page/1/2/3/4'), + { foo: '1', bar: '2', quux: '3', baz: '4' }, + 'does not rely on Object.keys order', + { todo: true } + ); + }); + + t.end(); +});