Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / test / unit / spec / misc / JSON.js
1 describe("Ext.JSON", function() {
2     var nativeJson;
3
4     beforeEach(function() {
5         nativeJson = Ext.USE_NATIVE_JSON;
6         Ext.USE_NATIVE_JSON = false;
7
8     });
9
10     afterEach(function() {
11         Ext.USE_NATIVE_JSON = nativeJson;
12     });
13
14     describe("encode", function() {
15         var encode = Ext.JSON.encode;
16
17         describe("numbers encoding", function() {
18             it("should convert integer to string", function() {
19                 expect(encode(15)).toEqual("15");
20             });
21
22             it("should convert float to string", function() {
23                 expect(encode(14.7)).toEqual("14.7");
24             });
25
26             it("should convert Infinity to null string", function() {
27                 expect(encode(Infinity)).toEqual("null");
28             });
29
30             it("should convert NaN to null string", function() {
31                 expect(encode(NaN)).toEqual("null");
32             });
33         });
34
35         describe("encoding of not defined values", function() {
36             it("should convert undefined to null string", function() {
37                 expect(encode(undefined)).toEqual("null");
38             });
39
40             it("should convert null to null string", function() {
41                 expect(encode(null)).toEqual("null");
42             });
43         });
44
45         describe("encoding function", function() {
46             it("should convert function to null string", function() {
47                 expect(encode(Ext.emptyFn)).toEqual("null");
48             });
49         });
50
51         describe("boolean encoding", function() {
52             it("should convert true to 'true'' string", function() {
53                 expect(encode(true)).toEqual("true");
54             });
55
56             it("should convert null to 'false' string", function() {
57                 expect(encode(false)).toEqual("false");
58             });
59         });
60
61         describe("array encoding", function() {
62             it("should convert empty array", function() {
63                 expect(encode([])).toEqual("[]");
64             });
65             
66             it("should convert array of numbers to string", function() {
67                 expect(encode([1, 2, 3])).toEqual("[1,2,3]");
68             });
69
70             it("should convert array of strings to string", function() {
71                 expect(encode(["a", "b", "c"])).toEqual("[\"a\",\"b\",\"c\"]");
72             });
73
74             it("should encode array including function member to string", function() {
75                 expect(encode([1, Ext.emptyFn, 3])).toEqual("[1,null,3]");
76             });
77
78             it("should convert array including undefined member to string", function() {
79                 expect(encode([1, undefined, 3])).toEqual("[1,null,3]");
80             });
81
82             it("should convert array including null member to string", function() {
83                 expect(encode([1, null, 3])).toEqual("[1,null,3]");
84             });
85         });
86
87         describe("string encoding", function() {
88             it("should convert string", function() {
89                 expect(encode("You're fired!")).toEqual("\"You're fired!\"");
90             });
91
92             it("should convert string with international character", function() {
93                 expect(encode("You're fired!")).toEqual("\"You're fired!\"");
94             });
95
96             it("should convert string with tab character", function() {
97                 expect(encode("a\tb")).toEqual("\"a\\tb\"");
98             });
99
100             it("should convert string with carriage return character", function() {
101                 expect(encode("a\rb")).toEqual("\"a\\rb\"");
102             });
103
104             it("should convert string with form feed character", function() {
105                 expect(encode("a\fb")).toEqual("\"a\\fb\"");
106             });
107
108             it("should convert string with new line character", function() {
109                 expect(encode("a\nb")).toEqual("\"a\\nb\"");
110             });
111
112             it("should convert string with vertical tab character", function() {
113                 expect(encode("a\x0bb")).toEqual("\"a\\u000bb\"");
114             });
115
116             it("should convert string with backslash character", function() {
117                 expect(encode("a\\b")).toEqual("\"a\\\\b\"");
118             });
119         });
120
121         describe("object encoding", function() {
122             it("should convert empty object", function() {
123                 expect(encode({})).toEqual("{}");
124             });
125             
126             it("should convert empty object with undefined property", function() {
127                 expect(encode({
128                     foo: "bar",
129                     bar: undefined
130                 })).toEqual("{\"foo\":\"bar\",\"bar\":null}");
131             });
132             
133             it("should convert empty object with null property", function() {
134                 expect(encode({
135                     foo: "bar",
136                     bar: null
137                 })).toEqual("{\"foo\":\"bar\",\"bar\":null}");
138             });
139             
140             it("should convert empty object with function property", function() {
141                 expect(encode({
142                     foo: "bar",
143                     bar: Ext.emptyFn
144                 })).toEqual("{\"foo\":\"bar\",\"bar\":null}");
145             });
146             
147             it("should not encode dom object", function() {
148                expect(encode(Ext.getBody().dom)).toBe('undefined');
149             });
150             
151             it("should handle encoding unknown child objects", function(){
152                 expect(encode({
153                     prop: Ext.getBody().dom
154                 })).toBe('{"prop":undefined}');
155             });
156         });
157
158         describe('encodeDate', function() {
159             var date;
160             
161             it("should encode a date object", function() {
162                 date = new Date("October 13, 1983 04:04:00");
163     
164                 expect(encode(date)).toEqual("\"1983-10-13T04:04:00\"");
165             });
166             
167             it("should format integers to have at least two digits", function() {
168                 date = new Date("August 9, 1983 06:03:02");
169                 
170                 expect(encode(date)).toEqual("\"1983-08-09T06:03:02\"");            
171             });
172         });
173         
174         describe("mix all possibilities", function() {
175             it("should encode data", function() {
176                  expect(encode({
177                     arr: [1, Ext.emptyFn, undefined, 2, [1, 2, 3], {a: 1, b: null}],
178                     foo: "bar",
179                     woo: {
180                         chu: "a\tb"
181                     }
182                  })).toEqual("{\"arr\":[1,null,null,2,[1,2,3],{\"a\":1,\"b\":null}],\"foo\":\"bar\",\"woo\":{\"chu\":\"a\\tb\"}}");
183             });
184         });
185     });
186
187     describe("decode", function() {
188         it("should decode data", function() {
189             expect(Ext.decode("{\"arr\":[1,null,null,2,[1,2,3],{\"a\":1,\"b\":null}],\"foo\":\"bar\",\"woo\":{\"chu\":\"a\\tb\"}}")).toEqual({
190                     arr: [1, null, null, 2, [1, 2, 3], {a: 1, b: null}],
191                     foo: "bar",
192                     woo: {
193                         chu: "a\tb"
194                     }            
195             });
196         });
197         
198         it("should raise an Ext.Error with invalid data", function() {
199             expect(function(){
200                 Ext.decode('{foo:"bar", x}');
201             }).toRaiseExtError();
202         });
203             
204         describe("with safe param", function(){
205             it("should decode valid data", function() {
206                 expect(Ext.decode("{\"foo\":\"bar\"}", true)).toEqual({
207                     foo: "bar"        
208                 });
209             });
210             
211             it("should return null with invalid data", function() {
212                 expect(Ext.decode('{foo+"bar"}', true)).toBeNull();
213             });
214         });
215     });
216     
217     it('should encode and decode an object', function(){
218         var object = {
219             a: [0, 1, 2],
220             s: "It's-me-Jacky!!",
221             ss: "!@#$%^&*()~=_-+][{};:?/.,<>'\"",
222             u: '\x01',
223             i: 1,
224             f: 3.14,
225             b: false,
226             n: null,
227             tree: {
228                 sub: {
229                     subMore: {
230                         subEvenMore: {
231                             arr: [5,6,7, {
232                                 complex: true
233                             }]
234                         }
235                     }
236                 }
237             }
238         };
239
240         expect(Ext.JSON.decode(Ext.JSON.encode(object))).toEqual(object);
241     });
242     
243     describe("aliases", function() {
244         it("should alias Ext.JSON.decode with Ext.decode", function() {
245             expect(Ext.decode).toBe(Ext.JSON.decode);
246         });
247
248         it("should alias Ext.JSON.encode with Ext.encode", function() {
249             expect(Ext.encode).toBe(Ext.JSON.encode);
250         });
251     });
252 });