aboutsummaryrefslogtreecommitdiffstats
path: root/test/JsonSpec.js
diff options
context:
space:
mode:
authorIgor Minar2012-03-27 10:21:49 -0700
committerIgor Minar2012-03-28 16:30:38 -0700
commit87f5c6e5b716100e203ec59c5874c3e927f83fa0 (patch)
tree84f2953b17d9ef5b9ddc4bde298adea9f503b3bd /test/JsonSpec.js
parenta8a750ab05bdff73ba3af0b98f3f284ff8d1e743 (diff)
downloadangular.js-87f5c6e5b716100e203ec59c5874c3e927f83fa0.tar.bz2
refactor(fromJson): always use native JSON.parse
This breaks IE7 for which you can use polyfill: https://github.com/douglascrockford/JSON-js <!--[if lt IE 8]> <script src="json2.min.js"></script> <![endif]--> or http://bestiejs.github.com/json3/ <!--[if lt IE 8]> <script src="json3.min.js"></script> <![endif]-->
Diffstat (limited to 'test/JsonSpec.js')
-rw-r--r--test/JsonSpec.js118
1 files changed, 12 insertions, 106 deletions
diff --git a/test/JsonSpec.js b/test/JsonSpec.js
index ad0cb415..e4c27455 100644
--- a/test/JsonSpec.js
+++ b/test/JsonSpec.js
@@ -1,6 +1,18 @@
'use strict';
describe('json', function() {
+
+ describe('fromJson', function() {
+
+ it('should delegate to native parser', function() {
+ var spy = spyOn(JSON, 'parse').andCallThrough();
+
+ expect(fromJson('{}')).toEqual({});
+ expect(spy).toHaveBeenCalled();
+ });
+ });
+
+
it('should serialize primitives', function() {
expect(toJson(0/0)).toEqual('null');
expect(toJson(null)).toEqual('null');
@@ -50,15 +62,6 @@ describe('json', function() {
expect(toJson({a:function() {}})).toEqual('{}');
});
- it('should parse null', function() {
- expect(fromJson('null')).toBeNull();
- });
-
- it('should parse boolean', function() {
- expect(fromJson('true')).toBeTruthy();
- expect(fromJson('false')).toBeFalsy();
- });
-
it('should serialize array with empty items', function() {
var a = [];
a[1] = 'X';
@@ -111,103 +114,6 @@ describe('json', function() {
expect(toJson(document)).toEqual('DOCUMENT');
});
- it('should parse floats', function() {
- expect(fromJson("{value:2.55, name:'misko'}")).toEqual({value:2.55, name:'misko'});
- });
-
- it('should parse negative / possitve numbers', function() {
- expect(fromJson("{neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]}")).toEqual({neg:-2.55, pos:+.3, a:[-2, +.1, -.2, +.3]});
- });
-
- it('should parse exponents', function() {
- expect(fromJson("{exp:1.2E10}")).toEqual({exp:1.2E10});
- expect(fromJson("{exp:1.2E-10}")).toEqual({exp:1.2E-10});
- expect(fromJson("{exp:1.2e+10}")).toEqual({exp:1.2E10});
- expect(fromJson("{exp:1.2e-10}")).toEqual({exp:1.2E-10});
- });
-
- it('should ignore non-strings', function() {
- expect(fromJson([])).toEqual([]);
- expect(fromJson({})).toEqual({});
- expect(fromJson(null)).toEqual(null);
- expect(fromJson(undefined)).toEqual(undefined);
- });
-
-
- //run these tests only in browsers that have native JSON parser
- if (JSON && JSON.parse) {
-
- describe('native parser', function() {
-
- var nativeParser = JSON.parse;
-
- afterEach(function() {
- JSON.parse = nativeParser;
- });
-
-
- it('should delegate to native parser if available and boolean flag is passed', function() {
- var spy = this.spyOn(JSON, 'parse').andCallThrough();
-
- expect(fromJson('{}')).toEqual({});
- expect(spy).not.toHaveBeenCalled();
-
- expect(fromJson('{}', true)).toEqual({});
- expect(spy).toHaveBeenCalled();
- });
- });
- }
-
-
- describe('security', function() {
- it('should not allow naked expressions', function() {
- expect(function() {fromJson('1+2');}).
- toThrow(new Error("Syntax Error: Token '+' is an unexpected token at column 2 of the expression [1+2] starting at [+2]."));
- });
-
- it('should not allow naked expressions group', function() {
- expect(function() {fromJson('(1+2)');}).
- toThrow(new Error("Syntax Error: Token '(' is not valid json at column 1 of the expression [(1+2)] starting at [(1+2)]."));
- });
-
- it('should not allow expressions in objects', function() {
- expect(function() {fromJson('{a:abc()}');}).
- toThrow(new Error("Syntax Error: Token 'abc' is not valid json at column 4 of the expression [{a:abc()}] starting at [abc()}]."));
- });
-
- it('should not allow expressions in arrays', function() {
- expect(function() {fromJson('[1+2]');}).
- toThrow(new Error("Syntax Error: Token '+' is not valid json at column 3 of the expression [[1+2]] starting at [+2]]."));
- });
-
- it('should not allow vars', function() {
- expect(function() {fromJson('[1, x]');}).
- toThrow(new Error("Syntax Error: Token 'x' is not valid json at column 5 of the expression [[1, x]] starting at [x]]."));
- });
-
- it('should not allow dereference', function() {
- expect(function() {fromJson('["".constructor]');}).
- toThrow(new Error("Syntax Error: Token '.' is not valid json at column 4 of the expression [[\"\".constructor]] starting at [.constructor]]."));
- });
-
- it('should not allow expressions ofter valid json', function() {
- expect(function() {fromJson('[].constructor');}).
- toThrow(new Error("Syntax Error: Token '.' is not valid json at column 3 of the expression [[].constructor] starting at [.constructor]."));
- });
-
- it('should not allow object dereference', function() {
- expect(function() {fromJson('{a:1, b: $location, c:1}');}).toThrow();
- expect(function() {fromJson("{a:1, b:[1]['__parent__']['location'], c:1}");}).toThrow();
- });
-
- it('should not allow assignments', function() {
- expect(function() {fromJson('{a:1, b:[1]=1, c:1}');}).toThrow();
- expect(function() {fromJson('{a:1, b:=1, c:1}');}).toThrow();
- expect(function() {fromJson('{a:1, b:x=1, c:1}');}).toThrow();
- });
-
- });
-
describe('string', function() {
it('should quote', function() {