| 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:unittest/unittest.dart'; | 6 import 'package:unittest/unittest.dart'; |
| 7 import 'dart:json' as json; | 7 import 'dart:json' as json; |
| 8 | 8 |
| 9 main() { | 9 main() { |
| 10 test('Parse', () { | 10 test('Parse', () { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 60 |
| 61 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi",' | 61 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi",' |
| 62 '"w":null, "u" : true, "v": false } '), | 62 '"w":null, "u" : true, "v": false } '), |
| 63 equals({"x":3, "y": -4.5, "z" : "hi", | 63 equals({"x":3, "y": -4.5, "z" : "hi", |
| 64 "w":null, "u" : true, "v": false })); | 64 "w":null, "u" : true, "v": false })); |
| 65 | 65 |
| 66 expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' | 66 expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' |
| 67 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), | 67 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), |
| 68 equals({"x": {"a":3, "b": -4.5}, "y":[{}], | 68 equals({"x": {"a":3, "b": -4.5}, "y":[{}], |
| 69 "z":"hi","w":{"c":null,"d":true}, "v":null})); | 69 "z":"hi","w":{"c":null,"d":true}, "v":null})); |
| 70 }); |
| 70 | 71 |
| 71 test('stringify', () { | 72 test('stringify', () { |
| 72 // Scalars. | 73 // Scalars. |
| 73 expect(json.stringify(5), equals('5')); | 74 expect(json.stringify(5), equals('5')); |
| 74 expect(json.stringify(-42), equals('-42')); | 75 expect(json.stringify(-42), equals('-42')); |
| 75 // Dart does not guarantee a formatting for doubles, | 76 // Dart does not guarantee a formatting for doubles, |
| 76 // so reparse and compare to the original. | 77 // so reparse and compare to the original. |
| 77 validateRoundTrip(3.14); | 78 validateRoundTrip(3.14); |
| 78 expect(json.stringify(true), equals('true')); | 79 expect(json.stringify(true), equals('true')); |
| 79 expect(json.stringify(false), equals('false')); | 80 expect(json.stringify(false), equals('false')); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 }, throwsJsonError); | 124 }, throwsJsonError); |
| 124 | 125 |
| 125 }); | 126 }); |
| 126 | 127 |
| 127 test('stringify throws if argument cannot be converted', () { | 128 test('stringify throws if argument cannot be converted', () { |
| 128 /** | 129 /** |
| 129 * Checks that we get an exception (rather than silently returning null) if | 130 * Checks that we get an exception (rather than silently returning null) if |
| 130 * we try to stringify something that cannot be converted to json. | 131 * we try to stringify something that cannot be converted to json. |
| 131 */ | 132 */ |
| 132 expect(() => json.stringify(new TestClass()), throwsJsonError); | 133 expect(() => json.stringify(new TestClass()), throwsJsonError); |
| 133 }); | |
| 134 }); | 134 }); |
| 135 | 135 |
| 136 test('stringify throws if toJson throws', () { | 136 test('stringify throws if toJson throws', () { |
| 137 expect(() => json.stringify(new ToJsoner("bad", throws: true)), | 137 expect(() => json.stringify(new ToJsoner("bad", throws: true)), |
| 138 throwsJsonError); | 138 throwsJsonError); |
| 139 }); | 139 }); |
| 140 | 140 |
| 141 test('stringify throws if toJson returns non-serializable value', () { | 141 test('stringify throws if toJson returns non-serializable value', () { |
| 142 expect(() => json.stringify(new ToJsoner(new TestClass())), | 142 expect(() => json.stringify(new ToJsoner(new TestClass())), |
| 143 throwsJsonError); | 143 throwsJsonError); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 var throwsJsonError = | 180 var throwsJsonError = |
| 181 throwsA(new isInstanceOf<json.JsonUnsupportedObjectError>()); | 181 throwsA(new isInstanceOf<json.JsonUnsupportedObjectError>()); |
| 182 | 182 |
| 183 /** | 183 /** |
| 184 * 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 |
| 185 * back, and produce something equivalent to the argument. | 185 * back, and produce something equivalent to the argument. |
| 186 */ | 186 */ |
| 187 validateRoundTrip(expected) { | 187 validateRoundTrip(expected) { |
| 188 expect(json.parse(json.stringify(expected)), equals(expected)); | 188 expect(json.parse(json.stringify(expected)), equals(expected)); |
| 189 } | 189 } |
| OLD | NEW |