Skip to content

Commit

Permalink
[Tests] Turn on eslint for test files
Browse files Browse the repository at this point in the history
  • Loading branch information
sigorilla authored and ljharb committed Oct 15, 2016
1 parent 387afd7 commit d4fb8e2
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 60 deletions.
30 changes: 15 additions & 15 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"root": true,
"root": true,

"extends": "@ljharb",
"extends": "@ljharb",

"rules": {
"complexity": [2, 25],
"consistent-return": [1],
"id-length": [2, { "min": 1, "max": 25, "properties": "never" }],
"indent": [2, 4],
"max-params": [2, 11],
"max-statements": [2, 42],
"no-extra-parens": [1],
"no-continue": [1],
"no-magic-numbers": 0,
"no-restricted-syntax": [2, "BreakStatement", "DebuggerStatement", "ForInStatement", "LabeledStatement", "WithStatement"],
"operator-linebreak": 1
}
"rules": {
"complexity": [2, 25],
"consistent-return": [1],
"id-length": [2, { "min": 1, "max": 25, "properties": "never" }],
"indent": [2, 4],
"max-params": [2, 11],
"max-statements": [2, 42],
"no-extra-parens": [1],
"no-continue": [1],
"no-magic-numbers": 0,
"no-restricted-syntax": [2, "BreakStatement", "DebuggerStatement", "ForInStatement", "LabeledStatement", "WithStatement"],
"operator-linebreak": 1
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"test": "npm run --silent coverage",
"tests-only": "node test",
"readme": "evalmd README.md",
"lint": "eslint lib/*.js text/*.js",
"lint": "eslint lib/*.js test/*.js",
"coverage": "covert test",
"dist": "mkdirp dist && browserify --standalone Qs lib/index.js > dist/qs.js"
},
Expand Down
9 changes: 9 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"rules": {
"max-lines": 0,
"max-nested-callbacks": [2, 3],
"max-statements": 0,
"no-extend-native": 0,
"sort-keys": 1
}
}
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

require('./parse');

require('./stringify');
Expand Down
67 changes: 33 additions & 34 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var iconv = require('iconv-lite');

test('parse()', function (t) {
t.test('parses a simple string', function (st) {
st.deepEqual(qs.parse('0=foo'), { '0': 'foo' });
st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
Expand Down Expand Up @@ -93,7 +93,7 @@ test('parse()', function (t) {

t.test('limits specific array indices to arrayLimit', function (st) {
st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: ['a'] });
st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { '21': 'a' } });
st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { 21: 'a' } });
st.end();
});

Expand Down Expand Up @@ -124,17 +124,17 @@ test('parse()', function (t) {
});

t.test('transforms arrays to objects', function (st) {
st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
st.deepEqual(qs.parse('foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb'), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });

st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { '0': 'b', c: true, t: 'u' } });
st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { '0': 'b', t: 'u', hasOwnProperty: 'c' } });
st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { '0': 'b', '1': 'c', x: 'y' } });
st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { '0': 'b', hasOwnProperty: 'c', x: 'y' } });
st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', c: true, t: 'u' } });
st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', 1: 'c', x: 'y' } });
st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
st.end();
});

Expand All @@ -144,18 +144,18 @@ test('parse()', function (t) {
st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15', '16'], bar: '2' }] });
st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { '0': 'bar', bad: 'baz' } });
st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
st.deepEqual(qs.parse('foo[0].a=a&foo[0].b=b&foo[1].a=aa&foo[1].b=bb', { allowDots: true }), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
st.end();
});

t.deepEqual(qs.parse('a[b]=c&a=d'), { a: { b: 'c', d: true } }, 'can add keys to objects');

t.test('correctly prunes undefined values when converting an array to an object', function (st) {
st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { '2': 'b', '99999999': 'c' } });
st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
st.end();
});

Expand All @@ -167,7 +167,7 @@ test('parse()', function (t) {
});

t.test('doesn\'t produce empty keys', function (st) {
st.deepEqual(qs.parse('_r=1&'), { '_r': '1' });
st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
st.end();
});

Expand Down Expand Up @@ -238,8 +238,8 @@ test('parse()', function (t) {
});

t.test('continues parsing when no parent is found', function (st) {
st.deepEqual(qs.parse('[]=&a=b'), { '0': '', a: 'b' });
st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { '0': null, a: 'b' });
st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
st.end();
});
Expand All @@ -250,7 +250,9 @@ test('parse()', function (t) {
str = str + '&' + str;
}

st.doesNotThrow(function () { qs.parse(str); });
st.doesNotThrow(function () {
qs.parse(str);
});

st.end();
});
Expand Down Expand Up @@ -293,14 +295,14 @@ test('parse()', function (t) {
});

t.test('allows overriding array limit', function (st) {
st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { '0': 'b' } });
st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { '0': 'b', '1': 'c' } });
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
st.end();
});

t.test('allows disabling array parsing', function (st) {
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { '0': 'b', '1': 'c' } });
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { 0: 'b', 1: 'c' } });
st.end();
});

Expand Down Expand Up @@ -344,13 +346,13 @@ test('parse()', function (t) {

t.test('parses an object and not child values', function (st) {
var input = {
'user[name]': { 'pop[bob]': { 'test': 3 } },
'user[name]': { 'pop[bob]': { test: 3 } },
'user[email]': null
};

var expected = {
user: {
name: { 'pop[bob]': { 'test': 3 } },
name: { 'pop[bob]': { test: 3 } },
email: null
}
};
Expand Down Expand Up @@ -426,7 +428,7 @@ test('parse()', function (t) {
st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
var expectedArray = Object.create(null);
expectedArray.a = Object.create(null);
expectedArray.a['0'] = 'b';
expectedArray.a[0] = 'b';
expectedArray.a.c = 'd';
st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
st.end();
Expand All @@ -435,13 +437,12 @@ test('parse()', function (t) {
t.test('can parse with custom encoding', function (st) {
st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
decoder: function (str) {
var reg = /\%([0-9A-F]{2})/ig;
var reg = /%([0-9A-F]{2})/ig;
var result = [];
var parts;
var last = 0;
while (parts = reg.exec(str)) {
var parts = reg.exec(str);
while (parts) {
result.push(parseInt(parts[1], 16));
last = parts.index + parts[0].length;
parts = reg.exec(str);
}
return iconv.decode(new Buffer(result), 'shift_jis').toString();
}
Expand All @@ -451,9 +452,7 @@ test('parse()', function (t) {

t.test('throws error with wrong decoder', function (st) {
st.throws(function () {
qs.parse({}, {
decoder: 'string'
});
qs.parse({}, { decoder: 'string' });
}, new TypeError('Decoder has to be a function.'));
st.end();
});
Expand Down
22 changes: 12 additions & 10 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ test('stringify()', function (t) {
var calls = 0;
var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
var filterFunc = function (prefix, value) {
calls++;
calls += 1;
if (calls === 1) {
st.equal(prefix, '', 'prefix is empty');
st.equal(value, obj);
Expand All @@ -406,14 +406,18 @@ test('stringify()', function (t) {
});

t.test('can sort the keys', function (st) {
var sort = function (a, b) { return a.localeCompare(b); };
var sort = function (a, b) {
return a.localeCompare(b);
};
st.equal(qs.stringify({ a: 'c', z: 'y', b: 'f' }, { sort: sort }), 'a=c&b=f&z=y');
st.equal(qs.stringify({ a: 'c', z: { j: 'a', i: 'b' }, b: 'f' }, { sort: sort }), 'a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a');
st.end();
});

t.test('can sort the keys at depth 3 or more too', function (st) {
var sort = function (a, b) { return a.localeCompare(b); };
var sort = function (a, b) {
return a.localeCompare(b);
};
st.equal(
qs.stringify(
{ a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
Expand All @@ -432,14 +436,14 @@ test('stringify()', function (t) {
});

t.test('can stringify with custom encoding', function (st) {
st.equal(qs.stringify({ : '大阪府', '': ''}, {
st.equal(qs.stringify({ : '大阪府', '': '' }, {
encoder: function (str) {
if (str.length === 0) {
return '';
}
var buf = iconv.encode(str, 'shiftjis');
var result = [];
for (var i=0; i < buf.length; ++i) {
for (var i = 0; i < buf.length; ++i) {
result.push(buf.readUInt8(i).toString(16));
}
return '%' + result.join('%');
Expand All @@ -455,9 +459,7 @@ test('stringify()', function (t) {
st.end();
});

t.test('can use custom encoder for a buffer object', {
skip: typeof Buffer === 'undefined'
}, function (st) {
t.test('can use custom encoder for a buffer object', { skip: typeof Buffer === 'undefined' }, function (st) {
st.equal(qs.stringify({ a: new Buffer([1]) }, {
encoder: function (buffer) {
if (typeof buffer === 'string') {
Expand Down Expand Up @@ -505,13 +507,13 @@ test('stringify()', function (t) {

t.test('RFC 1738 spaces serialization', function (st) {
st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
st.equal(qs.stringify({ "a b": 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
st.end();
});

t.test('RFC 3986 spaces serialization', function (st) {
st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC3986 }), 'a=b%20c');
st.equal(qs.stringify({ "a b": 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
st.end();
});

Expand Down

0 comments on commit d4fb8e2

Please sign in to comment.