| 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 "package:expect/expect.dart"; | 6 import 'package:unittest/unittest.dart'; |
| 7 import 'dart:json' as json; | 7 import 'dart:json' as json; |
| 8 import 'dart:html'; | |
| 9 import '../../pkg/unittest/lib/unittest.dart'; | |
| 10 import '../../pkg/unittest/lib/html_config.dart'; | |
| 11 | 8 |
| 12 main() { | 9 main() { |
| 13 useHtmlConfiguration(); | |
| 14 test('Parse', () { | 10 test('Parse', () { |
| 15 // Scalars. | 11 // Scalars. |
| 16 expect(json.parse(' 5 '), equals(5)); | 12 expect(json.parse(' 5 '), equals(5)); |
| 17 expect(json.parse(' -42 '), equals(-42)); | 13 expect(json.parse(' -42 '), equals(-42)); |
| 18 expect(json.parse(' 3e0 '), equals(3)); | 14 expect(json.parse(' 3e0 '), equals(3)); |
| 19 expect(json.parse(' 3.14 '), equals(3.14)); | 15 expect(json.parse(' 3.14 '), equals(3.14)); |
| 20 expect(json.parse('true '), isTrue); | 16 expect(json.parse('true '), isTrue); |
| 21 expect(json.parse(' false'), isFalse); | 17 expect(json.parse(' false'), isFalse); |
| 22 expect(json.parse(' null '), isNull); | 18 expect(json.parse(' null '), isNull); |
| 23 expect(json.parse('\n\rnull\t'), isNull); | 19 expect(json.parse('\n\rnull\t'), isNull); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 validateRoundTrip({' hi bob ':3, '':4.5}); | 107 validateRoundTrip({' hi bob ':3, '':4.5}); |
| 112 validateRoundTrip( | 108 validateRoundTrip( |
| 113 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, | 109 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, |
| 114 'v':null}); | 110 'v':null}); |
| 115 | 111 |
| 116 expect(json.stringify(new ToJson(4)), "4"); | 112 expect(json.stringify(new ToJson(4)), "4"); |
| 117 expect(json.stringify(new ToJson([4, "a"])), '[4,"a"]'); | 113 expect(json.stringify(new ToJson([4, "a"])), '[4,"a"]'); |
| 118 expect(json.stringify(new ToJson([4, new ToJson({"x":42})])), | 114 expect(json.stringify(new ToJson([4, new ToJson({"x":42})])), |
| 119 '[4,{"x":42}]'); | 115 '[4,{"x":42}]'); |
| 120 | 116 |
| 121 Expect.throws(() { | 117 expect(() { |
| 122 json.stringify([new ToJson(new ToJson(4))]); | 118 json.stringify([new ToJson(new ToJson(4))]); |
| 123 }); | 119 }, throwsJsonError); |
| 124 | 120 |
| 125 Expect.throws(() { | 121 expect(() { |
| 126 json.stringify([new Object()]); | 122 json.stringify([new Object()]); |
| 127 }); | 123 }, throwsJsonError); |
| 128 | 124 |
| 129 }); | 125 }); |
| 130 | 126 |
| 131 test('stringify throws if argument cannot be converted', () { | 127 test('stringify throws if argument cannot be converted', () { |
| 132 /** | 128 /** |
| 133 * Checks that we get an exception (rather than silently returning null) if | 129 * Checks that we get an exception (rather than silently returning null) if |
| 134 * we try to stringify something that cannot be converted to json. | 130 * we try to stringify something that cannot be converted to json. |
| 135 */ | 131 */ |
| 136 expect(() => json.stringify(new TestClass()), throws); | 132 expect(() => json.stringify(new TestClass()), throwsJsonError); |
| 137 }); | 133 }); |
| 138 }); | 134 }); |
| 135 |
| 136 test('stringify throws if toJson throws', () { |
| 137 expect(() => json.stringify(new ToJsoner("bad", throws: true)), |
| 138 throwsJsonError); |
| 139 }); |
| 140 |
| 141 test('stringify throws if toJson returns non-serializable value', () { |
| 142 expect(() => json.stringify(new ToJsoner(new TestClass())), |
| 143 throwsJsonError); |
| 144 }); |
| 145 |
| 146 test('stringify throws on cyclic values', () { |
| 147 var a = []; |
| 148 var b = a; |
| 149 for (int i = 0; i < 50; i++) { |
| 150 b = [b]; |
| 151 } |
| 152 a.add(b); |
| 153 expect(() => json.stringify(a), throwsJsonError); |
| 154 }); |
| 139 } | 155 } |
| 140 | 156 |
| 141 class TestClass { | 157 class TestClass { |
| 142 int x; | 158 int x; |
| 143 String y; | 159 String y; |
| 144 | 160 |
| 145 TestClass() : x = 3, y = 'joe' { } | 161 TestClass() : x = 3, y = 'joe' { } |
| 146 } | 162 } |
| 147 | 163 |
| 164 class ToJsoner { |
| 165 final Object returnValue; |
| 166 final bool throws; |
| 167 ToJsoner(this.returnValue, {this.throws}); |
| 168 Object toJson() { |
| 169 if (throws) throw returnValue; |
| 170 return returnValue; |
| 171 } |
| 172 } |
| 173 |
| 148 class ToJson { | 174 class ToJson { |
| 149 final object; | 175 final object; |
| 150 const ToJson(this.object); | 176 const ToJson(this.object); |
| 151 toJson() => object; | 177 toJson() => object; |
| 152 } | 178 } |
| 153 | 179 |
| 180 var throwsJsonError = |
| 181 throwsA(new isInstanceOf<json.JsonUnsupportedObjectError>()); |
| 182 |
| 154 /** | 183 /** |
| 155 * Checks that the argument can be converted to a JSON string and | 184 * Checks that the argument can be converted to a JSON string and |
| 156 * back, and produce something equivalent to the argument. | 185 * back, and produce something equivalent to the argument. |
| 157 */ | 186 */ |
| 158 validateRoundTrip(expected) { | 187 validateRoundTrip(expected) { |
| 159 expect(json.parse(json.stringify(expected)), equals(expected)); | 188 expect(json.parse(json.stringify(expected)), equals(expected)); |
| 160 } | 189 } |
| 161 | |
| 162 | |
| OLD | NEW |