Tests/Specs: Replace Chai with expect.js for assertions, because of browser support.
[mootools.git] / Specs / Utilities / JSON.js
blob2446169c9f7eb7d09aed0a7cb7a00d344bdb90a8
1 /*
2 ---
3 name: JSON
4 requires: ~
5 provides: ~
6 ...
7 */
9 describe('JSON', function(){
11         it('should encode and decode an object', function(){
12                 var object = {
13                         a: [0, 1, 2],
14                         s: "It's-me-Valerio!",
15                         u: '\x01',
16                         n: 1,
17                         f: 3.14,
18                         b: false,
19                         nil: null,
20                         o: {
21                                 a: 1,
22                                 b: [1, 2],
23                                 c: {
24                                         a: 2,
25                                         b: 3
26                                 }
27                         }
28                 };
30                 expect(JSON.decode(JSON.encode(object))).to.eql(object);
31         });
33 });
34 describe('JSON', function(){
36     var win = (typeof window === 'undefined' ? global : window);
37     var goodString = '{"name":"Jim Cowart","location":{"city":{"name":"Chattanooga","population":167674}}}';
38     var badString = 'alert("I\'m a bad string!")';
40     it('should parse a valid JSON string by default', function(){
41         expect(typeOf(JSON.decode(goodString))).to.equal("object");
42     });
44     it('should parse a valid JSON string when secure is set to false', function(){
45         expect(typeOf(JSON.decode(goodString, false))).to.equal("object");
46     });
48     it('should parse a hazarous string when secure is set to false', function(){
49         var _old_alert = win.alert;
50         win.alert = function (string) {
51             if (string == "I'm a bad string!") return true;
52             return false;
53         };
54         expect(JSON.decode(badString, false)).to.equal(true);
55         win.alert = _old_alert;
56     }); 
57     it('should parse a hazarous string when JSON.secure is set to false and secure is not defined', function(){
58         var _old_alert = win.alert;
59         win.alert = function (string) {
60             if (string == "I'm a bad string!") return true;
61             return false;
62         };
63         JSON.secure = false;
64         expect(JSON.decode(badString)).to.equal(true);
65         win.alert = _old_alert;
66         JSON.secure = true;
67     });     
68     it('should NOT parse a hazarous string by default', function(){
69         var err;
70         try {
71             JSON.decode(badString);
72         } catch (e){
73             err = !!e;
74         };
75         expect(err).to.equal(true);
76     });  
78 });