diff options
| author | Igor Minar | 2012-03-27 12:44:37 -0700 | 
|---|---|---|
| committer | Igor Minar | 2012-03-28 16:57:22 -0700 | 
| commit | 35125d25137ac2da13ed1ca3e652ec8f2c945053 (patch) | |
| tree | 387623dfc398e04c17ea15917d2d8e8cbf40bf19 /test/JsonSpec.js | |
| parent | 87f5c6e5b716100e203ec59c5874c3e927f83fa0 (diff) | |
| download | angular.js-35125d25137ac2da13ed1ca3e652ec8f2c945053.tar.bz2 | |
refactor(toJson): use native JSON.stringify
Instead of using our custom serializer we now use the native one and
use the replacer function to customize the serialization to preserve
most of the previous behavior (ignore $ and $$ properties as well
as window, document and scope instances).
Diffstat (limited to 'test/JsonSpec.js')
| -rw-r--r-- | test/JsonSpec.js | 133 | 
1 files changed, 27 insertions, 106 deletions
| diff --git a/test/JsonSpec.js b/test/JsonSpec.js index e4c27455..5ca3e42e 100644 --- a/test/JsonSpec.js +++ b/test/JsonSpec.js @@ -4,7 +4,7 @@ describe('json', function() {    describe('fromJson', function() { -    it('should delegate to native parser', function() { +    it('should delegate to JSON.parse', function() {        var spy = spyOn(JSON, 'parse').andCallThrough();        expect(fromJson('{}')).toEqual({}); @@ -13,125 +13,46 @@ describe('json', function() {    }); -  it('should serialize primitives', function() { -    expect(toJson(0/0)).toEqual('null'); -    expect(toJson(null)).toEqual('null'); -    expect(toJson(true)).toEqual('true'); -    expect(toJson(false)).toEqual('false'); -    expect(toJson(123.45)).toEqual('123.45'); -    expect(toJson('abc')).toEqual('"abc"'); -    expect(toJson('a \t \n \r b \\')).toEqual('"a \\t \\n \\r b \\\\"'); -  }); - -  it('should not serialize $$properties', function() { -    expect(toJson({$$some:'value', 'this':1, '$parent':1}, false)).toEqual('{}'); -  }); - -  it('should not serialize this or $parent', function() { -    expect(toJson({'this':'value', $parent:'abc'}, false)).toEqual('{}'); -  }); +  describe('toJson', function() { -  it('should serialize strings with escaped characters', function() { -    expect(toJson("7\\\"7")).toEqual("\"7\\\\\\\"7\""); -  }); - -  it('should serialize objects', function() { -    expect(toJson({a: 1, b: 2})).toEqual('{"a":1,"b":2}'); -    expect(toJson({a: {b: 2}})).toEqual('{"a":{"b":2}}'); -    expect(toJson({a: {b: {c: 0}}})).toEqual('{"a":{"b":{"c":0}}}'); -    expect(toJson({a: {b: 0/0}})).toEqual('{"a":{"b":null}}'); -  }); - -  it('should format objects pretty', function() { -    expect(toJson({a: 1, b: 2}, true)).toEqual('{\n  "a":1,\n  "b":2}'); -    expect(toJson({a: {b: 2}}, true)).toEqual('{\n  "a":{\n    "b":2}}'); -  }); +    it('should delegate to JSON.stringify', function() { +      var spy = spyOn(JSON, 'stringify').andCallThrough(); -  it('should serialize array', function() { -    expect(toJson([])).toEqual('[]'); -    expect(toJson([1, 'b'])).toEqual('[1,"b"]'); -  }); - -  it('should serialize RegExp', function() { -    expect(toJson(/foo/)).toEqual('"/foo/"'); -    expect(toJson([1, new RegExp('foo')])).toEqual('[1,"/foo/"]'); -  }); - -  it('should ignore functions', function() { -    expect(toJson([function() {},1])).toEqual('[null,1]'); -    expect(toJson({a:function() {}})).toEqual('{}'); -  }); - -  it('should serialize array with empty items', function() { -    var a = []; -    a[1] = 'X'; -    expect(toJson(a)).toEqual('[null,"X"]'); -  }); - -  it('should escape unicode', function() { -    expect('\u00a0'.length).toEqual(1); -    expect(toJson('\u00a0').length).toEqual(8); -    expect(fromJson(toJson('\u00a0')).length).toEqual(1); -  }); - -  it('should serialize UTC dates', function() { -    var date = new angular.mock.TzDate(-1, '2009-10-09T01:02:03.027Z'); -    expect(toJson(date)).toEqual('"2009-10-09T01:02:03.027Z"'); -  }); - -  it('should prevent recursion', function() { -    var obj = {a: 'b'}; -    obj.recursion = obj; -    expect(angular.toJson(obj)).toEqual('{"a":"b","recursion":RECURSION}'); -  }); +      expect(toJson({})).toEqual('{}'); +      expect(spy).toHaveBeenCalled(); +    }); -  it('should serialize $ properties', function() { -    var obj = {$a: 'a'}; -    expect(angular.toJson(obj)).toEqual('{"$a":"a"}'); -  }); -  it('should NOT serialize inherited properties', function() { -    // This is what native Browser does -    var obj = inherit({p:'p'}); -    obj.a = 'a'; -    expect(angular.toJson(obj)).toEqual('{"a":"a"}'); -  }); +    it('should format objects pretty', function() { +      expect(toJson({a: 1, b: 2}, true)). +          toBeOneOf('{\n  "a": 1,\n  "b": 2\n}', '{\n  "a":1,\n  "b":2\n}'); +      expect(toJson({a: {b: 2}}, true)). +          toBeOneOf('{\n  "a": {\n    "b": 2\n  }\n}', '{\n  "a":{\n    "b":2\n  }\n}'); +    }); -  it('should serialize same objects multiple times', function() { -    var obj = {a:'b'}; -    expect(angular.toJson({A:obj, B:obj})).toEqual('{"A":{"a":"b"},"B":{"a":"b"}}'); -  }); -  it('should not serialize undefined values', function() { -    expect(angular.toJson({A:undefined})).toEqual('{}'); -  }); +    it('should not serialize properties starting with $', function() { +      expect(toJson({$few: 'v', $$some:'value'}, false)).toEqual('{}'); +    }); -  it('should not serialize $window object', function() { -    expect(toJson(window)).toEqual('WINDOW'); -  }); -  it('should not serialize $document object', function() { -    expect(toJson(document)).toEqual('DOCUMENT'); -  }); +    it('should not serialize undefined values', function() { +      expect(angular.toJson({A:undefined})).toEqual('{}'); +    }); -  describe('string', function() { -    it('should quote', function() { -      expect(quoteUnicode('a')).toBe('"a"'); -      expect(quoteUnicode('\\')).toBe('"\\\\"'); -      expect(quoteUnicode("'a'")).toBe('"\'a\'"'); -      expect(quoteUnicode('"a"')).toBe('"\\"a\\""'); -      expect(quoteUnicode('\n\f\r\t')).toBe('"\\n\\f\\r\\t"'); +    it('should not serialize $window object', function() { +      expect(toJson(window)).toEqual('"$WINDOW"');      }); -    it('should quote slashes', function() { -      expect(quoteUnicode("7\\\"7")).toBe('"7\\\\\\\"7"'); -    }); -    it('should quote unicode', function() { -      expect(quoteUnicode('abc\u00A0def')).toBe('"abc\\u00a0def"'); +    it('should not serialize $document object', function() { +      expect(toJson(document)).toEqual('"$DOCUMENT"');      }); -  }); +    it('should not serialize scope instances', inject(function($rootScope) { +      expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}'); +    })); +  });  }); | 
