|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | describe('json', function() {
|
| 4 | + |
| 5 | + describe('fromJson', function() { |
| 6 | + |
| 7 | + it('should delegate to native parser', function() { |
| 8 | + var spy = spyOn(JSON, 'parse').andCallThrough(); |
| 9 | + |
| 10 | + expect(fromJson('{}')).toEqual({}); |
| 11 | + expect(spy).toHaveBeenCalled(); |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + |
4 | 16 | it('should serialize primitives', function() {
|
5 | 17 | expect(toJson(0/0)).toEqual('null');
|
6 | 18 | expect(toJson(null)).toEqual('null');
|
@@ -50,15 +62,6 @@ describe('json', function() {
|
50 | 62 | expect(toJson({a:function() {}})).toEqual('{}');
|
51 | 63 | });
|
52 | 64 |
|
53 |
| - it('should parse null', function() { |
54 |
| - expect(fromJson('null')).toBeNull(); |
55 |
| - }); |
56 |
| - |
57 |
| - it('should parse boolean', function() { |
58 |
| - expect(fromJson('true')).toBeTruthy(); |
59 |
| - expect(fromJson('false')).toBeFalsy(); |
60 |
| - }); |
61 |
| - |
62 | 65 | it('should serialize array with empty items', function() {
|
63 | 66 | var a = [];
|
64 | 67 | a[1] = 'X';
|
@@ -111,103 +114,6 @@ describe('json', function() {
|
111 | 114 | expect(toJson(document)).toEqual('DOCUMENT');
|
112 | 115 | });
|
113 | 116 |
|
114 |
| - it('should parse floats', function() { |
115 |
| - expect(fromJson("{value:2.55, name:'misko'}")).toEqual({value:2.55, name:'misko'}); |
116 |
| - }); |
117 |
| - |
118 |
| - it('should parse negative / possitve numbers', function() { |
119 |
| - expect(fromJson("{neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]}")).toEqual({neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]}); |
120 |
| - }); |
121 |
| - |
122 |
| - it('should parse exponents', function() { |
123 |
| - expect(fromJson("{exp:1.2E10}")).toEqual({exp:1.2E10}); |
124 |
| - expect(fromJson("{exp:1.2E-10}")).toEqual({exp:1.2E-10}); |
125 |
| - expect(fromJson("{exp:1.2e+10}")).toEqual({exp:1.2E10}); |
126 |
| - expect(fromJson("{exp:1.2e-10}")).toEqual({exp:1.2E-10}); |
127 |
| - }); |
128 |
| - |
129 |
| - it('should ignore non-strings', function() { |
130 |
| - expect(fromJson([])).toEqual([]); |
131 |
| - expect(fromJson({})).toEqual({}); |
132 |
| - expect(fromJson(null)).toEqual(null); |
133 |
| - expect(fromJson(undefined)).toEqual(undefined); |
134 |
| - }); |
135 |
| - |
136 |
| - |
137 |
| - //run these tests only in browsers that have native JSON parser |
138 |
| - if (JSON && JSON.parse) { |
139 |
| - |
140 |
| - describe('native parser', function() { |
141 |
| - |
142 |
| - var nativeParser = JSON.parse; |
143 |
| - |
144 |
| - afterEach(function() { |
145 |
| - JSON.parse = nativeParser; |
146 |
| - }); |
147 |
| - |
148 |
| - |
149 |
| - it('should delegate to native parser if available and boolean flag is passed', function() { |
150 |
| - var spy = this.spyOn(JSON, 'parse').andCallThrough(); |
151 |
| - |
152 |
| - expect(fromJson('{}')).toEqual({}); |
153 |
| - expect(spy).not.toHaveBeenCalled(); |
154 |
| - |
155 |
| - expect(fromJson('{}', true)).toEqual({}); |
156 |
| - expect(spy).toHaveBeenCalled(); |
157 |
| - }); |
158 |
| - }); |
159 |
| - } |
160 |
| - |
161 |
| - |
162 |
| - describe('security', function() { |
163 |
| - it('should not allow naked expressions', function() { |
164 |
| - expect(function() {fromJson('1+2');}). |
165 |
| - toThrow(new Error("Syntax Error: Token '+' is an unexpected token at column 2 of the expression [1+2] starting at [+2].")); |
166 |
| - }); |
167 |
| - |
168 |
| - it('should not allow naked expressions group', function() { |
169 |
| - expect(function() {fromJson('(1+2)');}). |
170 |
| - toThrow(new Error("Syntax Error: Token '(' is not valid json at column 1 of the expression [(1+2)] starting at [(1+2)].")); |
171 |
| - }); |
172 |
| - |
173 |
| - it('should not allow expressions in objects', function() { |
174 |
| - expect(function() {fromJson('{a:abc()}');}). |
175 |
| - toThrow(new Error("Syntax Error: Token 'abc' is not valid json at column 4 of the expression [{a:abc()}] starting at [abc()}].")); |
176 |
| - }); |
177 |
| - |
178 |
| - it('should not allow expressions in arrays', function() { |
179 |
| - expect(function() {fromJson('[1+2]');}). |
180 |
| - toThrow(new Error("Syntax Error: Token '+' is not valid json at column 3 of the expression [[1+2]] starting at [+2]].")); |
181 |
| - }); |
182 |
| - |
183 |
| - it('should not allow vars', function() { |
184 |
| - expect(function() {fromJson('[1, x]');}). |
185 |
| - toThrow(new Error("Syntax Error: Token 'x' is not valid json at column 5 of the expression [[1, x]] starting at [x]].")); |
186 |
| - }); |
187 |
| - |
188 |
| - it('should not allow dereference', function() { |
189 |
| - expect(function() {fromJson('["".constructor]');}). |
190 |
| - toThrow(new Error("Syntax Error: Token '.' is not valid json at column 4 of the expression [[\"\".constructor]] starting at [.constructor]].")); |
191 |
| - }); |
192 |
| - |
193 |
| - it('should not allow expressions ofter valid json', function() { |
194 |
| - expect(function() {fromJson('[].constructor');}). |
195 |
| - toThrow(new Error("Syntax Error: Token '.' is not valid json at column 3 of the expression [[].constructor] starting at [.constructor].")); |
196 |
| - }); |
197 |
| - |
198 |
| - it('should not allow object dereference', function() { |
199 |
| - expect(function() {fromJson('{a:1, b: $location, c:1}');}).toThrow(); |
200 |
| - expect(function() {fromJson("{a:1, b:[1]['__parent__']['location'], c:1}");}).toThrow(); |
201 |
| - }); |
202 |
| - |
203 |
| - it('should not allow assignments', function() { |
204 |
| - expect(function() {fromJson('{a:1, b:[1]=1, c:1}');}).toThrow(); |
205 |
| - expect(function() {fromJson('{a:1, b:=1, c:1}');}).toThrow(); |
206 |
| - expect(function() {fromJson('{a:1, b:x=1, c:1}');}).toThrow(); |
207 |
| - }); |
208 |
| - |
209 |
| - }); |
210 |
| - |
211 | 117 |
|
212 | 118 | describe('string', function() {
|
213 | 119 | it('should quote', function() {
|
|
0 commit comments