OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library json_tests; | 5 library json_tests; |
6 import 'dart:json'; | 6 import 'dart:json' as json; |
7 import 'dart:html'; | 7 import 'dart:html'; |
8 import '../../pkg/unittest/lib/unittest.dart'; | 8 import '../../pkg/unittest/lib/unittest.dart'; |
9 import '../../pkg/unittest/lib/html_config.dart'; | 9 import '../../pkg/unittest/lib/html_config.dart'; |
10 | 10 |
11 main() { | 11 main() { |
12 useHtmlConfiguration(); | 12 useHtmlConfiguration(); |
13 test('Parse', () { | 13 test('Parse', () { |
14 // Scalars. | 14 // Scalars. |
15 expect(JSON.parse(' 5 '), equals(5)); | 15 expect(json.parse(' 5 '), equals(5)); |
16 expect(JSON.parse(' -42 '), equals(-42)); | 16 expect(json.parse(' -42 '), equals(-42)); |
17 expect(JSON.parse(' 3e0 '), equals(3)); | 17 expect(json.parse(' 3e0 '), equals(3)); |
18 expect(JSON.parse(' 3.14 '), equals(3.14)); | 18 expect(json.parse(' 3.14 '), equals(3.14)); |
19 expect(JSON.parse('true '), isTrue); | 19 expect(json.parse('true '), isTrue); |
20 expect(JSON.parse(' false'), isFalse); | 20 expect(json.parse(' false'), isFalse); |
21 expect(JSON.parse(' null '), isNull); | 21 expect(json.parse(' null '), isNull); |
22 expect(JSON.parse('\n\rnull\t'), isNull); | 22 expect(json.parse('\n\rnull\t'), isNull); |
23 expect(JSON.parse(' "hi there\\" bob" '), equals('hi there" bob')); | 23 expect(json.parse(' "hi there\\" bob" '), equals('hi there" bob')); |
24 expect(JSON.parse(' "" '), isEmpty); | 24 expect(json.parse(' "" '), isEmpty); |
25 | 25 |
26 // Lists. | 26 // Lists. |
27 expect(JSON.parse(' [] '), isEmpty); | 27 expect(json.parse(' [] '), isEmpty); |
28 expect(JSON.parse('[ ]'), isEmpty); | 28 expect(json.parse('[ ]'), isEmpty); |
29 expect(JSON.parse(' [3, -4.5, true, "hi", false] '), | 29 expect(json.parse(' [3, -4.5, true, "hi", false] '), |
30 equals([3, -4.5, true, 'hi', false])); | 30 equals([3, -4.5, true, 'hi', false])); |
31 // Nulls are tricky. | 31 // Nulls are tricky. |
32 expect(JSON.parse('[null]'), orderedEquals([null])); | 32 expect(json.parse('[null]'), orderedEquals([null])); |
33 expect(JSON.parse(' [3, -4.5, null, true, "hi", false] '), | 33 expect(json.parse(' [3, -4.5, null, true, "hi", false] '), |
34 equals([3, -4.5, null, true, 'hi', false])); | 34 equals([3, -4.5, null, true, 'hi', false])); |
35 expect(JSON.parse('[[null]]'), equals([[null]])); | 35 expect(json.parse('[[null]]'), equals([[null]])); |
36 expect(JSON.parse(' [ [3], [], [null], ["hi", true]] '), | 36 expect(json.parse(' [ [3], [], [null], ["hi", true]] '), |
37 equals([[3], [], [null], ['hi', true]])); | 37 equals([[3], [], [null], ['hi', true]])); |
38 | 38 |
39 // Maps. | 39 // Maps. |
40 expect(JSON.parse(' {} '), isEmpty); | 40 expect(json.parse(' {} '), isEmpty); |
41 expect(JSON.parse('{ }'), isEmpty); | 41 expect(json.parse('{ }'), isEmpty); |
42 | 42 |
43 expect(JSON.parse( | 43 expect(json.parse( |
44 ' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } '), | 44 ' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } '), |
45 equals({"x":3, "y": -4.5, "z" : "hi", "u" : true, "v": false })); | 45 equals({"x":3, "y": -4.5, "z" : "hi", "u" : true, "v": false })); |
46 | 46 |
47 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi" } '), | 47 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi" } '), |
48 equals({"x":3, "y": -4.5, "z" : "hi" })); | 48 equals({"x":3, "y": -4.5, "z" : "hi" })); |
49 | 49 |
50 expect(JSON.parse(' {"y": -4.5, "z" : "hi" ,"x":3 } '), | 50 expect(json.parse(' {"y": -4.5, "z" : "hi" ,"x":3 } '), |
51 equals({"y": -4.5, "z" : "hi" ,"x":3 })); | 51 equals({"y": -4.5, "z" : "hi" ,"x":3 })); |
52 | 52 |
53 expect(JSON.parse('{ " hi bob " :3, "": 4.5}'), | 53 expect(json.parse('{ " hi bob " :3, "": 4.5}'), |
54 equals({ " hi bob " :3, "": 4.5})); | 54 equals({ " hi bob " :3, "": 4.5})); |
55 | 55 |
56 expect(JSON.parse(' { "x" : { } } '), equals({ 'x' : {}})); | 56 expect(json.parse(' { "x" : { } } '), equals({ 'x' : {}})); |
57 expect(JSON.parse('{"x":{}}'), equals({ 'x' : {}})); | 57 expect(json.parse('{"x":{}}'), equals({ 'x' : {}})); |
58 | 58 |
59 // Nulls are tricky. | 59 // Nulls are tricky. |
60 expect(JSON.parse('{"w":null}'), equals({ 'w' : null})); | 60 expect(json.parse('{"w":null}'), equals({ 'w' : null})); |
61 | 61 |
62 expect(JSON.parse('{"x":{"w":null}}'), equals({"x":{"w":null}})); | 62 expect(json.parse('{"x":{"w":null}}'), equals({"x":{"w":null}})); |
63 | 63 |
64 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi",' | 64 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi",' |
65 '"w":null, "u" : true, "v": false } '), | 65 '"w":null, "u" : true, "v": false } '), |
66 equals({"x":3, "y": -4.5, "z" : "hi", | 66 equals({"x":3, "y": -4.5, "z" : "hi", |
67 "w":null, "u" : true, "v": false })); | 67 "w":null, "u" : true, "v": false })); |
68 | 68 |
69 expect(JSON.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' | 69 expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' |
70 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), | 70 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), |
71 equals({"x": {"a":3, "b": -4.5}, "y":[{}], | 71 equals({"x": {"a":3, "b": -4.5}, "y":[{}], |
72 "z":"hi","w":{"c":null,"d":true}, "v":null})); | 72 "z":"hi","w":{"c":null,"d":true}, "v":null})); |
73 | 73 |
74 test('stringify', () { | 74 test('stringify', () { |
75 // Scalars. | 75 // Scalars. |
76 expect(JSON.stringify(5), equals('5')); | 76 expect(json.stringify(5), equals('5')); |
77 expect(JSON.stringify(-42), equals('-42')); | 77 expect(json.stringify(-42), equals('-42')); |
78 // Dart does not guarantee a formatting for doubles, | 78 // Dart does not guarantee a formatting for doubles, |
79 // so reparse and compare to the original. | 79 // so reparse and compare to the original. |
80 validateRoundTrip(3.14); | 80 validateRoundTrip(3.14); |
81 expect(JSON.stringify(true), equals('true')); | 81 expect(json.stringify(true), equals('true')); |
82 expect(JSON.stringify(false), equals('false')); | 82 expect(json.stringify(false), equals('false')); |
83 expect(JSON.stringify(null), equals('null')); | 83 expect(json.stringify(null), equals('null')); |
84 expect(JSON.stringify(' hi there" bob '), equals('" hi there\\" bob "')); | 84 expect(json.stringify(' hi there" bob '), equals('" hi there\\" bob "')); |
85 expect(JSON.stringify('hi\\there'), equals('"hi\\\\there"')); | 85 expect(json.stringify('hi\\there'), equals('"hi\\\\there"')); |
86 // TODO(devoncarew): these tests break the dartium build | 86 // TODO(devoncarew): these tests break the dartium build |
87 //expect(JSON.stringify('hi\nthere'), equals('"hi\\nthere"')); | 87 //expect(json.stringify('hi\nthere'), equals('"hi\\nthere"')); |
88 //expect(JSON.stringify('hi\r\nthere'), equals('"hi\\r\\nthere"')); | 88 //expect(json.stringify('hi\r\nthere'), equals('"hi\\r\\nthere"')); |
89 expect(JSON.stringify(''), equals('""')); | 89 expect(json.stringify(''), equals('""')); |
90 | 90 |
91 // Lists. | 91 // Lists. |
92 expect(JSON.stringify([]), equals('[]')); | 92 expect(json.stringify([]), equals('[]')); |
93 expect(JSON.stringify(new List(0)), equals('[]')); | 93 expect(json.stringify(new List.fixedLength(0)), equals('[]')); |
94 expect(JSON.stringify(new List(3)), equals('[null,null,null]')); | 94 expect(json.stringify(new List.fixedLength(3)), equals('[null,null,null]')); |
95 validateRoundTrip([3, -4.5, null, true, 'hi', false]); | 95 validateRoundTrip([3, -4.5, null, true, 'hi', false]); |
96 expect(JSON.stringify([[3], [], [null], ['hi', true]]), | 96 expect(json.stringify([[3], [], [null], ['hi', true]]), |
97 equals('[[3],[],[null],["hi",true]]')); | 97 equals('[[3],[],[null],["hi",true]]')); |
98 | 98 |
99 // Maps. | 99 // Maps. |
100 expect(JSON.stringify({}), equals('{}')); | 100 expect(json.stringify({}), equals('{}')); |
101 expect(JSON.stringify(new Map()), equals('{}')); | 101 expect(json.stringify(new Map()), equals('{}')); |
102 expect(JSON.stringify({'x':{}}), equals('{"x":{}}')); | 102 expect(json.stringify({'x':{}}), equals('{"x":{}}')); |
103 expect(JSON.stringify({'x':{'a':3}}), equals('{"x":{"a":3}}')); | 103 expect(json.stringify({'x':{'a':3}}), equals('{"x":{"a":3}}')); |
104 | 104 |
105 // Dart does not guarantee an order on the keys | 105 // Dart does not guarantee an order on the keys |
106 // of a map literal, so reparse and compare to the original Map. | 106 // of a map literal, so reparse and compare to the original Map. |
107 validateRoundTrip( | 107 validateRoundTrip( |
108 {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false}); | 108 {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false}); |
109 validateRoundTrip({"x":3, "y":-4.5, "z":'hi'}); | 109 validateRoundTrip({"x":3, "y":-4.5, "z":'hi'}); |
110 validateRoundTrip({' hi bob ':3, '':4.5}); | 110 validateRoundTrip({' hi bob ':3, '':4.5}); |
111 validateRoundTrip( | 111 validateRoundTrip( |
112 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, | 112 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, |
113 'v':null}); | 113 'v':null}); |
114 | 114 |
115 expect(JSON.stringify(new ToJson(4)), "4"); | 115 expect(json.stringify(new ToJson(4)), "4"); |
116 expect(JSON.stringify(new ToJson([4, "a"])), '[4,"a"]'); | 116 expect(json.stringify(new ToJson([4, "a"])), '[4,"a"]'); |
117 expect(JSON.stringify(new ToJson([4, new ToJson({"x":42})])), | 117 expect(json.stringify(new ToJson([4, new ToJson({"x":42})])), |
118 '[4,{"x":42}]'); | 118 '[4,{"x":42}]'); |
119 | 119 |
120 Expect.throws(() { | 120 Expect.throws(() { |
121 JSON.stringify([new ToJson(new ToJson(4))]); | 121 json.stringify([new ToJson(new ToJson(4))]); |
122 }); | 122 }); |
123 | 123 |
124 Expect.throws(() { | 124 Expect.throws(() { |
125 JSON.stringify([new Object()]); | 125 json.stringify([new Object()]); |
126 }); | 126 }); |
127 | 127 |
128 }); | 128 }); |
129 | 129 |
130 test('stringify throws if argument cannot be converted', () { | 130 test('stringify throws if argument cannot be converted', () { |
131 /** | 131 /** |
132 * Checks that we get an exception (rather than silently returning null) if | 132 * Checks that we get an exception (rather than silently returning null) if |
133 * we try to stringify something that cannot be converted to json. | 133 * we try to stringify something that cannot be converted to json. |
134 */ | 134 */ |
135 expect(() => JSON.stringify(new TestClass()), throws); | 135 expect(() => json.stringify(new TestClass()), throws); |
136 }); | 136 }); |
137 }); | 137 }); |
138 } | 138 } |
139 | 139 |
140 class TestClass { | 140 class TestClass { |
141 int x; | 141 int x; |
142 String y; | 142 String y; |
143 | 143 |
144 TestClass() : x = 3, y = 'joe' { } | 144 TestClass() : x = 3, y = 'joe' { } |
145 } | 145 } |
146 | 146 |
147 class ToJson { | 147 class ToJson { |
148 final object; | 148 final object; |
149 const ToJson(this.object); | 149 const ToJson(this.object); |
150 toJson() => object; | 150 toJson() => object; |
151 } | 151 } |
152 | 152 |
153 /** | 153 /** |
154 * Checks that the argument can be converted to a JSON string and | 154 * Checks that the argument can be converted to a JSON string and |
155 * back, and produce something equivalent to the argument. | 155 * back, and produce something equivalent to the argument. |
156 */ | 156 */ |
157 validateRoundTrip(expected) { | 157 validateRoundTrip(expected) { |
158 expect(JSON.parse(JSON.stringify(expected)), equals(expected)); | 158 expect(json.parse(json.stringify(expected)), equals(expected)); |
159 } | 159 } |
160 | 160 |
161 | 161 |
OLD | NEW |