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