Skip to content
This repository
Browse code

new stringify tests

  • Loading branch information...
commit 1e5ce17ced5c1505b92a4181d1d6e3158510af78 1 parent 51485a5
TJ Holowaychuk authored February 29, 2012
1  Makefile
... ...
@@ -1,6 +1,7 @@
1 1
 
2 2
 test:
3 3
 	@./node_modules/.bin/mocha \
  4
+		--require should \
4 5
 		--ui bdd
5 6
 
6 7
 .PHONY: test
4  test/parse.js
... ...
@@ -1,8 +1,4 @@
1 1
 
2  
-/**
3  
- * Module dependencies.
4  
- */
5  
-
6 2
 var qs = require('../');
7 3
 
8 4
 describe('qs.parse()', function(){
176  test/stringify.js
... ...
@@ -1,103 +1,73 @@
1  
-// 
2  
-// /**
3  
-//  * Module dependencies.
4  
-//  */
5  
-// 
6  
-// var qs = require('../')
7  
-//   , should = require('should')
8  
-//   , str_identities = {
9  
-//     'basics': [
10  
-//       { str: 'foo=bar', obj: {'foo' : 'bar'}},
11  
-//       { str: 'foo=%22bar%22', obj: {'foo' : '\"bar\"'}},
12  
-//       { str: 'foo=', obj: {'foo': ''}},
13  
-//       { str: 'foo=1&bar=2', obj: {'foo' : '1', 'bar' : '2'}},
14  
-//       { str: 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', obj: {'my weird field': "q1!2\"'w$5&7/z8)?"}},
15  
-//       { str: 'foo%3Dbaz=bar', obj: {'foo=baz': 'bar'}},
16  
-//       { str: 'foo=bar&bar=baz', obj: {foo: 'bar', bar: 'baz'}}
17  
-//     ],
18  
-//     'escaping': [
19  
-//       { str: 'foo=foo%20bar', obj: {foo: 'foo bar'}},
20  
-//       { str: 'cht=p3&chd=t%3A60%2C40&chs=250x100&chl=Hello%7CWorld', obj: {
21  
-//           cht: 'p3'
22  
-//         , chd: 't:60,40'
23  
-//         , chs: '250x100'
24  
-//         , chl: 'Hello|World'
25  
-//       }}
26  
-//     ],
27  
-//     'nested': [
28  
-//       { str: 'foo[0]=bar&foo[1]=quux', obj: {'foo' : ['bar', 'quux']}},
29  
-//       { str: 'foo[0]=bar', obj: {foo: ['bar']}},
30  
-//       { str: 'foo[0]=1&foo[1]=2', obj: {'foo' : ['1', '2']}},
31  
-//       { str: 'foo=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : 'bar', 'baz' : ['1', '2', '3']}},
32  
-//       { str: 'foo[0]=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : ['bar'], 'baz' : ['1', '2', '3']}},
33  
-//       { str: 'x[y][z]=1', obj: {'x' : {'y' : {'z' : '1'}}}},
34  
-//       { str: 'x[y][z][0]=1', obj: {'x' : {'y' : {'z' : ['1']}}}},
35  
-//       { str: 'x[y][z]=2', obj: {'x' : {'y' : {'z' : '2'}}}},
36  
-//       { str: 'x[y][z][0]=1&x[y][z][1]=2', obj: {'x' : {'y' : {'z' : ['1', '2']}}}},
37  
-//       { str: 'x[y][0][z]=1', obj: {'x' : {'y' : [{'z' : '1'}]}}},
38  
-//       { str: 'x[y][0][z][0]=1', obj: {'x' : {'y' : [{'z' : ['1']}]}}},
39  
-//       { str: 'x[y][0][z]=1&x[y][0][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'w' : '2'}]}}},
40  
-//       { str: 'x[y][0][v][w]=1', obj: {'x' : {'y' : [{'v' : {'w' : '1'}}]}}},
41  
-//       { str: 'x[y][0][z]=1&x[y][0][v][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'v' : {'w' : '2'}}]}}},
42  
-//       { str: 'x[y][0][z]=1&x[y][1][z]=2', obj: {'x' : {'y' : [{'z' : '1'}, {'z' : '2'}]}}},
43  
-//       { 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'}]}}},
44  
-//       { str: 'user[name][first]=tj&user[name][last]=holowaychuk', obj: { user: { name: { first: 'tj', last: 'holowaychuk' }}}}
45  
-//     ],
46  
-//     'errors': [
47  
-//       { obj: 'foo=bar',     message: 'stringify expects an object' },
48  
-//       { obj: ['foo', 'bar'], message: 'stringify expects an object' }
49  
-//     ],
50  
-//     'numbers': [
51  
-//       { str: 'limit[0]=1&limit[1]=2&limit[2]=3', obj: { limit: [1, 2, '3'] }},
52  
-//       { str: 'limit=1', obj: { limit: 1 }}
53  
-//     ]
54  
-//   };
55  
-//   
56  
-// 
57  
-// // Assert error
58  
-// function err(fn, msg){
59  
-//   var err;
60  
-//   try {
61  
-//     fn();
62  
-//   } catch (e) {
63  
-//     should.equal(e.message, msg);
64  
-//     return;
65  
-//   }
66  
-//   throw new Error('no exception thrown, expected "' + msg + '"');
67  
-// }
68  
-// 
69  
-// function test(type) {
70  
-//   var str, obj;
71  
-//   for (var i = 0; i < str_identities[type].length; i++) {
72  
-//     str = str_identities[type][i].str;
73  
-//     obj = str_identities[type][i].obj;
74  
-//     qs.stringify(obj).should.eql(str);
75  
-//   }
76  
-// }
77  
-// 
78  
-// module.exports = {
79  
-//   'test basics': function() {
80  
-//     test('basics');
81  
-//   },
82  
-// 
83  
-//   'test escaping': function() {
84  
-//     test('escaping');
85  
-//   },
86  
-// 
87  
-//   'test nested': function() {
88  
-//     test('nested');
89  
-//   },
90  
-//   
91  
-//   'test numbers': function(){
92  
-//     test('numbers');
93  
-//   },
94  
-// 
95  
-//   'test errors': function() {
96  
-//     var obj, message;
97  
-//     for (var i = 0; i < str_identities['errors'].length; i++) {
98  
-//       message = str_identities['errors'][i].message;
99  
-//       obj = str_identities['errors'][i].obj;
100  
-//       err(function(){ qs.stringify(obj) }, message);
101  
-//     }
102  
-//   }
103  
-// };
  1
+
  2
+/**
  3
+ * Module dependencies.
  4
+ */
  5
+
  6
+var qs = require('../');
  7
+
  8
+
  9
+var str_identities = {
  10
+  'basics': [
  11
+    { str: 'foo=bar', obj: {'foo' : 'bar'}},
  12
+    { str: 'foo=%22bar%22', obj: {'foo' : '\"bar\"'}},
  13
+    { str: 'foo=', obj: {'foo': ''}},
  14
+    { str: 'foo=1&bar=2', obj: {'foo' : '1', 'bar' : '2'}},
  15
+    { str: 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', obj: {'my weird field': "q1!2\"'w$5&7/z8)?"}},
  16
+    { str: 'foo%3Dbaz=bar', obj: {'foo=baz': 'bar'}},
  17
+    { str: 'foo=bar&bar=baz', obj: {foo: 'bar', bar: 'baz'}}
  18
+  ],
  19
+  'escaping': [
  20
+    { str: 'foo=foo%20bar', obj: {foo: 'foo bar'}},
  21
+    { str: 'cht=p3&chd=t%3A60%2C40&chs=250x100&chl=Hello%7CWorld', obj: {
  22
+        cht: 'p3'
  23
+      , chd: 't:60,40'
  24
+      , chs: '250x100'
  25
+      , chl: 'Hello|World'
  26
+    }}
  27
+  ],
  28
+  'nested': [
  29
+    { str: 'foo[0]=bar&foo[1]=quux', obj: {'foo' : ['bar', 'quux']}},
  30
+    { str: 'foo[0]=bar', obj: {foo: ['bar']}},
  31
+    { str: 'foo[0]=1&foo[1]=2', obj: {'foo' : ['1', '2']}},
  32
+    { str: 'foo=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : 'bar', 'baz' : ['1', '2', '3']}},
  33
+    { str: 'foo[0]=bar&baz[0]=1&baz[1]=2&baz[2]=3', obj: {'foo' : ['bar'], 'baz' : ['1', '2', '3']}},
  34
+    { str: 'x[y][z]=1', obj: {'x' : {'y' : {'z' : '1'}}}},
  35
+    { str: 'x[y][z][0]=1', obj: {'x' : {'y' : {'z' : ['1']}}}},
  36
+    { str: 'x[y][z]=2', obj: {'x' : {'y' : {'z' : '2'}}}},
  37
+    { str: 'x[y][z][0]=1&x[y][z][1]=2', obj: {'x' : {'y' : {'z' : ['1', '2']}}}},
  38
+    { str: 'x[y][0][z]=1', obj: {'x' : {'y' : [{'z' : '1'}]}}},
  39
+    { str: 'x[y][0][z][0]=1', obj: {'x' : {'y' : [{'z' : ['1']}]}}},
  40
+    { str: 'x[y][0][z]=1&x[y][0][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'w' : '2'}]}}},
  41
+    { str: 'x[y][0][v][w]=1', obj: {'x' : {'y' : [{'v' : {'w' : '1'}}]}}},
  42
+    { str: 'x[y][0][z]=1&x[y][0][v][w]=2', obj: {'x' : {'y' : [{'z' : '1', 'v' : {'w' : '2'}}]}}},
  43
+    { str: 'x[y][0][z]=1&x[y][1][z]=2', obj: {'x' : {'y' : [{'z' : '1'}, {'z' : '2'}]}}},
  44
+    { 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'}]}}},
  45
+    { str: 'user[name][first]=tj&user[name][last]=holowaychuk', obj: { user: { name: { first: 'tj', last: 'holowaychuk' }}}}
  46
+  ],
  47
+  'errors': [
  48
+    { obj: 'foo=bar',     message: 'stringify expects an object' },
  49
+    { obj: ['foo', 'bar'], message: 'stringify expects an object' }
  50
+  ],
  51
+  'numbers': [
  52
+    { str: 'limit[0]=1&limit[1]=2&limit[2]=3', obj: { limit: [1, 2, '3'] }},
  53
+    { str: 'limit=1', obj: { limit: 1 }}
  54
+  ]
  55
+};
  56
+
  57
+function test(type) {
  58
+  return function(){
  59
+    var str, obj;
  60
+    for (var i = 0; i < str_identities[type].length; i++) {
  61
+      str = str_identities[type][i].str;
  62
+      obj = str_identities[type][i].obj;
  63
+      qs.stringify(obj).should.eql(str);
  64
+    }
  65
+  }
  66
+}
  67
+
  68
+describe('qs.stringify()', function(){
  69
+  it('should support the basics', test('basics'))
  70
+  it('should support escapes', test('escaping'))
  71
+  it('should support nesting', test('nested'))
  72
+  it('should support numbers', test('numbers'))
  73
+})

0 notes on commit 1e5ce17

Please sign in to comment.
Something went wrong with that request. Please try again.