Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / connect / node_modules / qs / test / stringify.js
1
2 if (require.register) {
3   var qs = require('querystring');
4 } else {
5   var qs = require('../')
6     , expect = require('expect.js');
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       expect(qs.stringify(obj)).to.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 })