Permalink
Browse files

new tests

  • Loading branch information...
1 parent 6a13a3a commit 51485a5efd051ffa4d725d54d59134eaefe929af @tj committed Feb 29, 2012
Showing with 137 additions and 159 deletions.
  1. +2 −1 Makefile
  2. +32 −55 test/parse.js
  3. +103 −103 test/stringify.js
View
@@ -1,5 +1,6 @@
test:
- @./node_modules/.bin/mocha
+ @./node_modules/.bin/mocha \
+ --ui bdd
.PHONY: test
View
@@ -5,8 +5,8 @@
var qs = require('../');
-module.exports = {
- 'test basics': function(){
+describe('qs.parse()', function(){
+ it('should support the basics', function(){
qs.parse('0=foo').should.eql({ '0': 'foo' });
qs.parse('foo=c++')
@@ -49,9 +49,9 @@ module.exports = {
, chs: '250x100'
, chl: 'Hello|World'
});
- },
-
- 'test nesting': function(){
+ })
+
+ it('should support nesting', function(){
qs.parse('ops[>=]=25')
.should.eql({ ops: { '>=': '25' }});
@@ -60,14 +60,9 @@ module.exports = {
qs.parse('user[name][first]=tj&user[name][last]=holowaychuk')
.should.eql({ user: { name: { first: 'tj', last: 'holowaychuk' }}});
- },
-
- 'test escaping': function(){
- qs.parse('foo=foo%20bar')
- .should.eql({ foo: 'foo bar' });
- },
-
- 'test arrays': function(){
+ })
+
+ it('should support array notation', function(){
qs.parse('images[]')
.should.eql({ images: [] });
@@ -95,9 +90,21 @@ module.exports = {
var o = qs.parse('existing[fcbaebfecc][name][last]=tj')
o.should.eql({ existing: { 'fcbaebfecc': { name: { last: 'tj' }}}})
Array.isArray(o.existing).should.be.false;
- },
+ })
+
+ it('should support arrays with indexes', function(){
+ qs.parse('foo[0]=bar&foo[1]=baz').should.eql({ foo: ['bar', 'baz'] });
+ qs.parse('foo[1]=bar&foo[0]=baz').should.eql({ foo: ['baz', 'bar'] });
+ qs.parse('foo[base64]=RAWR').should.eql({ foo: { base64: 'RAWR' }});
+ qs.parse('foo[64base]=RAWR').should.eql({ foo: { '64base': 'RAWR' }});
+ })
+
+ it('should expand to an array when dupliate keys are present', function(){
+ qs.parse('items=bar&items=baz&items=raz')
+ .should.eql({ items: ['bar', 'baz', 'raz'] });
+ })
- 'test right-hand brackets': function(){
+ it('should support right-hand side brackets', function(){
qs.parse('pets=["tobi"]')
.should.eql({ pets: '["tobi"]' });
@@ -109,59 +116,29 @@ module.exports = {
qs.parse('op[>=]=[1,2,3]&op[=]=[[[[1]]]]')
.should.eql({ op: { '>=': '[1,2,3]', '=': '[[[[1]]]]' }});
- },
-
- 'test duplicates': function(){
- qs.parse('items=bar&items=baz&items=raz')
- .should.eql({ items: ['bar', 'baz', 'raz'] });
- },
+ })
- 'test empty': function(){
+ it('should support empty values', function(){
qs.parse('').should.eql({});
qs.parse(undefined).should.eql({});
qs.parse(null).should.eql({});
- },
+ })
- 'test arrays with indexes': function(){
- qs.parse('foo[0]=bar&foo[1]=baz').should.eql({ foo: ['bar', 'baz'] });
- qs.parse('foo[1]=bar&foo[0]=baz').should.eql({ foo: ['baz', 'bar'] });
- qs.parse('foo[base64]=RAWR').should.eql({ foo: { base64: 'RAWR' }});
- qs.parse('foo[64base]=RAWR').should.eql({ foo: { '64base': 'RAWR' }});
- },
-
- 'test arrays becoming objects': function(){
+ it('should transform arrays to objects', function(){
qs.parse('foo[0]=bar&foo[bad]=baz').should.eql({ foo: { 0: "bar", bad: "baz" }});
qs.parse('foo[bad]=baz&foo[0]=bar').should.eql({ foo: { 0: "bar", bad: "baz" }});
- },
-
- 'test bleed-through of Array native properties/methods': function(){
- Array.prototype.protoProperty = true;
- Array.prototype.protoFunction = function () {};
- qs.parse('foo=bar').should.eql({ foo: 'bar' });
- },
+ })
- 'test malformed uri': function(){
+ it('should support malformed uri chars', function(){
qs.parse('{%:%}').should.eql({ '{%:%}': '' });
qs.parse('foo=%:%}').should.eql({ 'foo': '%:%}' });
- },
+ })
- 'test semi-parsed': function(){
+ it('should support semi-parsed strings', function(){
qs.parse({ 'user[name]': 'tobi' })
.should.eql({ user: { name: 'tobi' }});
qs.parse({ 'user[name]': 'tobi', 'user[email][main]': 'tobi@lb.com' })
.should.eql({ user: { name: 'tobi', email: { main: 'tobi@lb.com' } }});
- }
-
- // 'test complex': function(){
- // qs.parse('users[][name][first]=tj&users[foo]=bar')
- // .should.eql({
- // users: [ { name: 'tj' }, { name: 'tobi' }, { foo: 'bar' }]
- // });
- //
- // qs.parse('users[][name][first]=tj&users[][name][first]=tobi')
- // .should.eql({
- // users: [ { name: 'tj' }, { name: 'tobi' }]
- // });
- // }
-};
+ })
+})
View
@@ -1,103 +1,103 @@
-
-/**
- * Module dependencies.
- */
-
-var qs = require('../')
- , should = require('should')
- , str_identities = {
- 'basics': [
- { str: 'foo=bar', obj: {'foo' : 'bar'}},
- { str: 'foo=%22bar%22', obj: {'foo' : '\"bar\"'}},
- { str: 'foo=', obj: {'foo': ''}},
- { str: 'foo=1&bar=2', obj: {'foo' : '1', 'bar' : '2'}},
- { str: 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', obj: {'my weird field': "q1!2\"'w$5&7/z8)?"}},
- { str: 'foo%3Dbaz=bar', obj: {'foo=baz': 'bar'}},
- { str: 'foo=bar&bar=baz', obj: {foo: 'bar', bar: 'baz'}}
- ],
- 'escaping': [
- { str: 'foo=foo%20bar', obj: {foo: 'foo bar'}},
- { str: 'cht=p3&chd=t%3A60%2C40&chs=250x100&chl=Hello%7CWorld', obj: {
- cht: 'p3'
- , chd: 't:60,40'
- , chs: '250x100'
- , chl: 'Hello|World'
- }}
- ],
- 'nested': [
- { str: 'foo[0]=bar&foo[1]=quux', obj: {'foo' : ['bar', 'quux']}},
- { str: 'foo[0]=bar', obj: {foo: ['bar']}},
- { str: 'foo[0]=1&foo[1]=2', obj: {'foo' : ['1', '2']}},
- { str: 'foo=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : 'bar', 'baz' : ['1', '2', '3']}},
- { str: 'foo[0]=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : ['bar'], 'baz' : ['1', '2', '3']}},
- { str: 'x[y][z]=1', obj: {'x' : {'y' : {'z' : '1'}}}},
- { str: 'x[y][z][0]=1', obj: {'x' : {'y' : {'z' : ['1']}}}},
- { str: 'x[y][z]=2', obj: {'x' : {'y' : {'z' : '2'}}}},
- { str: 'x[y][z][0]=1&x[y][z][1]=2', obj: {'x' : {'y' : {'z' : ['1', '2']}}}},
- { str: 'x[y][0][z]=1', obj: {'x' : {'y' : [{'z' : '1'}]}}},
- { str: 'x[y][0][z][0]=1', obj: {'x' : {'y' : [{'z' : ['1']}]}}},
- { str: 'x[y][0][z]=1&x[y][0][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'w' : '2'}]}}},
- { str: 'x[y][0][v][w]=1', obj: {'x' : {'y' : [{'v' : {'w' : '1'}}]}}},
- { str: 'x[y][0][z]=1&x[y][0][v][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'v' : {'w' : '2'}}]}}},
- { str: 'x[y][0][z]=1&x[y][1][z]=2', obj: {'x' : {'y' : [{'z' : '1'}, {'z' : '2'}]}}},
- { str: 'x[y][0][z]=1&x[y][0][w]=a&x[y][1][z]=2&x[y][1][w]=3', obj: {'x' : {'y' : [{'z' : '1', 'w' : 'a'}, {'z' : '2', 'w' : '3'}]}}},
- { str: 'user[name][first]=tj&user[name][last]=holowaychuk', obj: { user: { name: { first: 'tj', last: 'holowaychuk' }}}}
- ],
- 'errors': [
- { obj: 'foo=bar', message: 'stringify expects an object' },
- { obj: ['foo', 'bar'], message: 'stringify expects an object' }
- ],
- 'numbers': [
- { str: 'limit[0]=1&limit[1]=2&limit[2]=3', obj: { limit: [1, 2, '3'] }},
- { str: 'limit=1', obj: { limit: 1 }}
- ]
- };
-
-
-// Assert error
-function err(fn, msg){
- var err;
- try {
- fn();
- } catch (e) {
- should.equal(e.message, msg);
- return;
- }
- throw new Error('no exception thrown, expected "' + msg + '"');
-}
-
-function test(type) {
- var str, obj;
- for (var i = 0; i < str_identities[type].length; i++) {
- str = str_identities[type][i].str;
- obj = str_identities[type][i].obj;
- qs.stringify(obj).should.eql(str);
- }
-}
-
-module.exports = {
- 'test basics': function() {
- test('basics');
- },
-
- 'test escaping': function() {
- test('escaping');
- },
-
- 'test nested': function() {
- test('nested');
- },
-
- 'test numbers': function(){
- test('numbers');
- },
-
- 'test errors': function() {
- var obj, message;
- for (var i = 0; i < str_identities['errors'].length; i++) {
- message = str_identities['errors'][i].message;
- obj = str_identities['errors'][i].obj;
- err(function(){ qs.stringify(obj) }, message);
- }
- }
-};
+//
+// /**
+// * Module dependencies.
+// */
+//
+// var qs = require('../')
+// , should = require('should')
+// , str_identities = {
+// 'basics': [
+// { str: 'foo=bar', obj: {'foo' : 'bar'}},
+// { str: 'foo=%22bar%22', obj: {'foo' : '\"bar\"'}},
+// { str: 'foo=', obj: {'foo': ''}},
+// { str: 'foo=1&bar=2', obj: {'foo' : '1', 'bar' : '2'}},
+// { str: 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', obj: {'my weird field': "q1!2\"'w$5&7/z8)?"}},
+// { str: 'foo%3Dbaz=bar', obj: {'foo=baz': 'bar'}},
+// { str: 'foo=bar&bar=baz', obj: {foo: 'bar', bar: 'baz'}}
+// ],
+// 'escaping': [
+// { str: 'foo=foo%20bar', obj: {foo: 'foo bar'}},
+// { str: 'cht=p3&chd=t%3A60%2C40&chs=250x100&chl=Hello%7CWorld', obj: {
+// cht: 'p3'
+// , chd: 't:60,40'
+// , chs: '250x100'
+// , chl: 'Hello|World'
+// }}
+// ],
+// 'nested': [
+// { str: 'foo[0]=bar&foo[1]=quux', obj: {'foo' : ['bar', 'quux']}},
+// { str: 'foo[0]=bar', obj: {foo: ['bar']}},
+// { str: 'foo[0]=1&foo[1]=2', obj: {'foo' : ['1', '2']}},
+// { str: 'foo=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : 'bar', 'baz' : ['1', '2', '3']}},
+// { str: 'foo[0]=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : ['bar'], 'baz' : ['1', '2', '3']}},
+// { str: 'x[y][z]=1', obj: {'x' : {'y' : {'z' : '1'}}}},
+// { str: 'x[y][z][0]=1', obj: {'x' : {'y' : {'z' : ['1']}}}},
+// { str: 'x[y][z]=2', obj: {'x' : {'y' : {'z' : '2'}}}},
+// { str: 'x[y][z][0]=1&x[y][z][1]=2', obj: {'x' : {'y' : {'z' : ['1', '2']}}}},
+// { str: 'x[y][0][z]=1', obj: {'x' : {'y' : [{'z' : '1'}]}}},
+// { str: 'x[y][0][z][0]=1', obj: {'x' : {'y' : [{'z' : ['1']}]}}},
+// { str: 'x[y][0][z]=1&x[y][0][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'w' : '2'}]}}},
+// { str: 'x[y][0][v][w]=1', obj: {'x' : {'y' : [{'v' : {'w' : '1'}}]}}},
+// { str: 'x[y][0][z]=1&x[y][0][v][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'v' : {'w' : '2'}}]}}},
+// { str: 'x[y][0][z]=1&x[y][1][z]=2', obj: {'x' : {'y' : [{'z' : '1'}, {'z' : '2'}]}}},
+// { str: 'x[y][0][z]=1&x[y][0][w]=a&x[y][1][z]=2&x[y][1][w]=3', obj: {'x' : {'y' : [{'z' : '1', 'w' : 'a'}, {'z' : '2', 'w' : '3'}]}}},
+// { str: 'user[name][first]=tj&user[name][last]=holowaychuk', obj: { user: { name: { first: 'tj', last: 'holowaychuk' }}}}
+// ],
+// 'errors': [
+// { obj: 'foo=bar', message: 'stringify expects an object' },
+// { obj: ['foo', 'bar'], message: 'stringify expects an object' }
+// ],
+// 'numbers': [
+// { str: 'limit[0]=1&limit[1]=2&limit[2]=3', obj: { limit: [1, 2, '3'] }},
+// { str: 'limit=1', obj: { limit: 1 }}
+// ]
+// };
+//
+//
+// // Assert error
+// function err(fn, msg){
+// var err;
+// try {
+// fn();
+// } catch (e) {
+// should.equal(e.message, msg);
+// return;
+// }
+// throw new Error('no exception thrown, expected "' + msg + '"');
+// }
+//
+// function test(type) {
+// var str, obj;
+// for (var i = 0; i < str_identities[type].length; i++) {
+// str = str_identities[type][i].str;
+// obj = str_identities[type][i].obj;
+// qs.stringify(obj).should.eql(str);
+// }
+// }
+//
+// module.exports = {
+// 'test basics': function() {
+// test('basics');
+// },
+//
+// 'test escaping': function() {
+// test('escaping');
+// },
+//
+// 'test nested': function() {
+// test('nested');
+// },
+//
+// 'test numbers': function(){
+// test('numbers');
+// },
+//
+// 'test errors': function() {
+// var obj, message;
+// for (var i = 0; i < str_identities['errors'].length; i++) {
+// message = str_identities['errors'][i].message;
+// obj = str_identities['errors'][i].obj;
+// err(function(){ qs.stringify(obj) }, message);
+// }
+// }
+// };

0 comments on commit 51485a5

Please sign in to comment.