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 json2_tests; |
6 import "package:expect/expect.dart"; | 6 |
7 import "dart:convert"; | 7 import "dart:convert"; |
8 import 'dart:html'; | |
9 import '../../pkg/unittest/lib/unittest.dart'; | |
10 import '../../pkg/unittest/lib/html_config.dart'; | |
11 | 8 |
12 main() { | 9 import 'package:unittest/unittest.dart'; |
13 useHtmlConfiguration(); | 10 |
| 11 // Moved from "tests/json/json_test.dart" |
| 12 |
| 13 void main() { |
14 test('Parse', () { | 14 test('Parse', () { |
15 // Scalars. | 15 // Scalars. |
16 expect(JSON.decode(' 5 '), equals(5)); | 16 expect(JSON.decode(' 5 '), equals(5)); |
17 expect(JSON.decode(' -42 '), equals(-42)); | 17 expect(JSON.decode(' -42 '), equals(-42)); |
18 expect(JSON.decode(' 3e0 '), equals(3)); | 18 expect(JSON.decode(' 3e0 '), equals(3)); |
19 expect(JSON.decode(' 3.14 '), equals(3.14)); | 19 expect(JSON.decode(' 3.14 '), equals(3.14)); |
20 expect(JSON.decode('true '), isTrue); | 20 expect(JSON.decode('true '), isTrue); |
21 expect(JSON.decode(' false'), isFalse); | 21 expect(JSON.decode(' false'), isFalse); |
22 expect(JSON.decode(' null '), isNull); | 22 expect(JSON.decode(' null '), isNull); |
23 expect(JSON.decode('\n\rnull\t'), isNull); | 23 expect(JSON.decode('\n\rnull\t'), isNull); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 expect(JSON.encode(5), equals('5')); | 78 expect(JSON.encode(5), equals('5')); |
79 expect(JSON.encode(-42), equals('-42')); | 79 expect(JSON.encode(-42), equals('-42')); |
80 // Dart does not guarantee a formatting for doubles, | 80 // Dart does not guarantee a formatting for doubles, |
81 // so reparse and compare to the original. | 81 // so reparse and compare to the original. |
82 validateRoundTrip(3.14); | 82 validateRoundTrip(3.14); |
83 expect(JSON.encode(true), equals('true')); | 83 expect(JSON.encode(true), equals('true')); |
84 expect(JSON.encode(false), equals('false')); | 84 expect(JSON.encode(false), equals('false')); |
85 expect(JSON.encode(null), equals('null')); | 85 expect(JSON.encode(null), equals('null')); |
86 expect(JSON.encode(' hi there" bob '), equals('" hi there\\" bob "')); | 86 expect(JSON.encode(' hi there" bob '), equals('" hi there\\" bob "')); |
87 expect(JSON.encode('hi\\there'), equals('"hi\\\\there"')); | 87 expect(JSON.encode('hi\\there'), equals('"hi\\\\there"')); |
88 // TODO(devoncarew): these tests break the dartium build | 88 expect(JSON.encode('hi\nthere'), equals('"hi\\nthere"')); |
89 //expect(JSON.encode('hi\nthere'), equals('"hi\\nthere"')); | 89 expect(JSON.encode('hi\r\nthere'), equals('"hi\\r\\nthere"')); |
90 //expect(JSON.encode('hi\r\nthere'), equals('"hi\\r\\nthere"')); | |
91 expect(JSON.encode(''), equals('""')); | 90 expect(JSON.encode(''), equals('""')); |
92 | 91 |
93 // Lists. | 92 // Lists. |
94 expect(JSON.encode([]), equals('[]')); | 93 expect(JSON.encode([]), equals('[]')); |
95 expect(JSON.encode(new List(0)), equals('[]')); | 94 expect(JSON.encode(new List(0)), equals('[]')); |
96 expect(JSON.encode(new List(3)), equals('[null,null,null]')); | 95 expect(JSON.encode(new List(3)), equals('[null,null,null]')); |
97 validateRoundTrip([3, -4.5, null, true, 'hi', false]); | 96 validateRoundTrip([3, -4.5, null, true, 'hi', false]); |
98 expect(JSON.encode([[3], [], [null], ['hi', true]]), | 97 expect(JSON.encode([[3], [], [null], ['hi', true]]), |
99 equals('[[3],[],[null],["hi",true]]')); | 98 equals('[[3],[],[null],["hi",true]]')); |
100 | 99 |
(...skipping 11 matching lines...) Expand all Loading... |
112 validateRoundTrip({' hi bob ':3, '':4.5}); | 111 validateRoundTrip({' hi bob ':3, '':4.5}); |
113 validateRoundTrip( | 112 validateRoundTrip( |
114 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, | 113 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, |
115 'v':null}); | 114 'v':null}); |
116 | 115 |
117 expect(JSON.encode(new ToJson(4)), "4"); | 116 expect(JSON.encode(new ToJson(4)), "4"); |
118 expect(JSON.encode(new ToJson([4, "a"])), '[4,"a"]'); | 117 expect(JSON.encode(new ToJson([4, "a"])), '[4,"a"]'); |
119 expect(JSON.encode(new ToJson([4, new ToJson({"x":42})])), | 118 expect(JSON.encode(new ToJson([4, new ToJson({"x":42})])), |
120 '[4,{"x":42}]'); | 119 '[4,{"x":42}]'); |
121 | 120 |
122 Expect.throws(() { | 121 expect(() => JSON.encode([new ToJson(new ToJson(4))]), throws); |
123 JSON.encode([new ToJson(new ToJson(4))]); | |
124 }); | |
125 | 122 |
126 Expect.throws(() { | 123 expect(() => JSON.encode([new Object()]), throws); |
127 JSON.encode([new Object()]); | |
128 }); | |
129 | |
130 }); | 124 }); |
131 | 125 |
132 test('stringify throws if argument cannot be converted', () { | 126 test('stringify throws if argument cannot be converted', () { |
133 /** | 127 /** |
134 * Checks that we get an exception (rather than silently returning null) if | 128 * Checks that we get an exception (rather than silently returning null) if |
135 * we try to stringify something that cannot be converted to json. | 129 * we try to stringify something that cannot be converted to json. |
136 */ | 130 */ |
137 expect(() => JSON.encode(new TestClass()), throws); | 131 expect(() => JSON.encode(new TestClass()), throws); |
138 }); | 132 }); |
139 | 133 |
(...skipping 21 matching lines...) Expand all Loading... |
161 class ToJson { | 155 class ToJson { |
162 final object; | 156 final object; |
163 const ToJson(this.object); | 157 const ToJson(this.object); |
164 toJson() => object; | 158 toJson() => object; |
165 } | 159 } |
166 | 160 |
167 /** | 161 /** |
168 * Checks that the argument can be converted to a JSON string and | 162 * Checks that the argument can be converted to a JSON string and |
169 * back, and produce something equivalent to the argument. | 163 * back, and produce something equivalent to the argument. |
170 */ | 164 */ |
171 validateRoundTrip(expected) { | 165 void validateRoundTrip(expected) { |
172 expect(JSON.decode(JSON.encode(expected)), equals(expected)); | 166 expect(JSON.decode(JSON.encode(expected)), equals(expected)); |
173 } | 167 } |
OLD | NEW |