| 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:expect/expect.dart"; |
| 7 import 'dart:json' as json; | 7 import 'dart:json' as json; |
| 8 import 'dart:html'; | 8 import 'dart:html'; |
| 9 import '../../pkg/unittest/lib/unittest.dart'; | 9 import '../../pkg/unittest/lib/unittest.dart'; |
| 10 import '../../pkg/unittest/lib/html_config.dart'; | 10 import '../../pkg/unittest/lib/html_config.dart'; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi",' | 65 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi",' |
| 66 '"w":null, "u" : true, "v": false } '), | 66 '"w":null, "u" : true, "v": false } '), |
| 67 equals({"x":3, "y": -4.5, "z" : "hi", | 67 equals({"x":3, "y": -4.5, "z" : "hi", |
| 68 "w":null, "u" : true, "v": false })); | 68 "w":null, "u" : true, "v": false })); |
| 69 | 69 |
| 70 expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' | 70 expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' |
| 71 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), | 71 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), |
| 72 equals({"x": {"a":3, "b": -4.5}, "y":[{}], | 72 equals({"x": {"a":3, "b": -4.5}, "y":[{}], |
| 73 "z":"hi","w":{"c":null,"d":true}, "v":null})); | 73 "z":"hi","w":{"c":null,"d":true}, "v":null})); |
| 74 }); |
| 74 | 75 |
| 75 test('stringify', () { | 76 test('stringify', () { |
| 76 // Scalars. | 77 // Scalars. |
| 77 expect(json.stringify(5), equals('5')); | 78 expect(json.stringify(5), equals('5')); |
| 78 expect(json.stringify(-42), equals('-42')); | 79 expect(json.stringify(-42), equals('-42')); |
| 79 // Dart does not guarantee a formatting for doubles, | 80 // Dart does not guarantee a formatting for doubles, |
| 80 // so reparse and compare to the original. | 81 // so reparse and compare to the original. |
| 81 validateRoundTrip(3.14); | 82 validateRoundTrip(3.14); |
| 82 expect(json.stringify(true), equals('true')); | 83 expect(json.stringify(true), equals('true')); |
| 83 expect(json.stringify(false), equals('false')); | 84 expect(json.stringify(false), equals('false')); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 }); | 128 }); |
| 128 | 129 |
| 129 }); | 130 }); |
| 130 | 131 |
| 131 test('stringify throws if argument cannot be converted', () { | 132 test('stringify throws if argument cannot be converted', () { |
| 132 /** | 133 /** |
| 133 * Checks that we get an exception (rather than silently returning null) if | 134 * Checks that we get an exception (rather than silently returning null) if |
| 134 * we try to stringify something that cannot be converted to json. | 135 * we try to stringify something that cannot be converted to json. |
| 135 */ | 136 */ |
| 136 expect(() => json.stringify(new TestClass()), throws); | 137 expect(() => json.stringify(new TestClass()), throws); |
| 137 }); | |
| 138 }); | 138 }); |
| 139 } | 139 } |
| 140 | 140 |
| 141 class TestClass { | 141 class TestClass { |
| 142 int x; | 142 int x; |
| 143 String y; | 143 String y; |
| 144 | 144 |
| 145 TestClass() : x = 3, y = 'joe' { } | 145 TestClass() : x = 3, y = 'joe' { } |
| 146 } | 146 } |
| 147 | 147 |
| 148 class ToJson { | 148 class ToJson { |
| 149 final object; | 149 final object; |
| 150 const ToJson(this.object); | 150 const ToJson(this.object); |
| 151 toJson() => object; | 151 toJson() => object; |
| 152 } | 152 } |
| 153 | 153 |
| 154 /** | 154 /** |
| 155 * Checks that the argument can be converted to a JSON string and | 155 * Checks that the argument can be converted to a JSON string and |
| 156 * back, and produce something equivalent to the argument. | 156 * back, and produce something equivalent to the argument. |
| 157 */ | 157 */ |
| 158 validateRoundTrip(expected) { | 158 validateRoundTrip(expected) { |
| 159 expect(json.parse(json.stringify(expected)), equals(expected)); | 159 expect(json.parse(json.stringify(expected)), equals(expected)); |
| 160 } | 160 } |
| 161 | 161 |
| 162 | 162 |
| OLD | NEW |