diff options
| author | Igor Minar | 2012-03-27 10:21:49 -0700 |
|---|---|---|
| committer | Igor Minar | 2012-03-28 16:30:38 -0700 |
| commit | 87f5c6e5b716100e203ec59c5874c3e927f83fa0 (patch) | |
| tree | 84f2953b17d9ef5b9ddc4bde298adea9f503b3bd /test | |
| parent | a8a750ab05bdff73ba3af0b98f3f284ff8d1e743 (diff) | |
| download | angular.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')
| -rw-r--r-- | test/JsonSpec.js | 118 | ||||
| -rw-r--r-- | test/ngScenario/FutureSpec.js | 2 |
2 files changed, 13 insertions, 107 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() { diff --git a/test/ngScenario/FutureSpec.js b/test/ngScenario/FutureSpec.js index 2a75f275..9ae284dc 100644 --- a/test/ngScenario/FutureSpec.js +++ b/test/ngScenario/FutureSpec.js @@ -42,7 +42,7 @@ describe('angular.scenario.Future', function() { it('should parse json with fromJson', function() { var future = new angular.scenario.Future('test name', function(done) { - done(null, "{test: 'foo'}"); + done(null, '{"test": "foo"}'); }); future.fromJson().execute(angular.noop); expect(future.value).toEqual({test: 'foo'}); |
