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:convert"; | 7 import "dart:convert"; |
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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 test('stringify throws if argument cannot be converted', () { | 132 test('stringify throws if argument cannot be converted', () { |
133 /** | 133 /** |
134 * 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 |
135 * we try to stringify something that cannot be converted to json. | 135 * we try to stringify something that cannot be converted to json. |
136 */ | 136 */ |
137 expect(() => JSON.encode(new TestClass()), throws); | 137 expect(() => JSON.encode(new TestClass()), throws); |
138 }); | 138 }); |
139 | 139 |
140 test('stringify custom converter', () { | 140 test('stringify custom converter', () { |
141 List l = [const C(0), const C(1)]; | 141 List l = [const C(0), const C(1)]; |
142 expect(JSON.encode(l, (x) => "C(${x.id})"), equals("[C(0),C(1)]")); | 142 expect(JSON.encode(l, toEncodable: (x) => "C(${x.id})"), |
143 expect(JSON.encode(l), equals("[C/0,C/1]")); | 143 equals('["C(0)","C(1)"]')); |
| 144 expect(JSON.encode(l), equals('["C/0","C/1"]')); |
144 }); | 145 }); |
145 } | 146 } |
146 | 147 |
147 class C { | 148 class C { |
148 final int id; | 149 final int id; |
149 const C(this.id); | 150 const C(this.id); |
150 toJson() => "C/$id"; | 151 toJson() => "C/$id"; |
151 } | 152 } |
152 | 153 |
153 class TestClass { | 154 class TestClass { |
154 int x; | 155 int x; |
155 String y; | 156 String y; |
156 | 157 |
157 TestClass() : x = 3, y = 'joe' { } | 158 TestClass() : x = 3, y = 'joe' { } |
158 } | 159 } |
159 | 160 |
160 class ToJson { | 161 class ToJson { |
161 final object; | 162 final object; |
162 const ToJson(this.object); | 163 const ToJson(this.object); |
163 toJson() => object; | 164 toJson() => object; |
164 } | 165 } |
165 | 166 |
166 /** | 167 /** |
167 * Checks that the argument can be converted to a JSON string and | 168 * Checks that the argument can be converted to a JSON string and |
168 * back, and produce something equivalent to the argument. | 169 * back, and produce something equivalent to the argument. |
169 */ | 170 */ |
170 validateRoundTrip(expected) { | 171 validateRoundTrip(expected) { |
171 expect(JSON.decode(JSON.encode(expected)), equals(expected)); | 172 expect(JSON.decode(JSON.encode(expected)), equals(expected)); |
172 } | 173 } |
OLD | NEW |