OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 import "package:async_helper/async_helper.dart"; | 8 import "package:async_helper/async_helper.dart"; |
9 | 9 |
10 final TESTS = [ | 10 final TESTS = [ |
11 [ 5, '5' ], | 11 [5, '5'], |
12 [ -42, '-42' ], | 12 [-42, '-42'], |
13 [ 3.14, '3.14' ], | 13 [3.14, '3.14'], |
14 [ true, 'true' ], | 14 [true, 'true'], |
15 [ false, 'false' ], | 15 [false, 'false'], |
16 [ null, 'null' ], | 16 [null, 'null'], |
17 [ 'quote"or\'', '"quote\\"or\'"' ], | 17 ['quote"or\'', '"quote\\"or\'"'], |
18 [ '', '""' ], | 18 ['', '""'], |
19 [ [], "[]" ], | 19 [[], "[]"], |
20 [ [3, -4.5, true, "hi", false], '[3,-4.5,true,"hi",false]' ], | 20 [ |
21 [ [null], "[null]" ], | 21 [3, -4.5, true, "hi", false], |
22 [ [[null]], "[[null]]" ], | 22 '[3,-4.5,true,"hi",false]' |
23 [ [[3]], "[[3]]" ], | 23 ], |
24 [ {}, "{}" ], | 24 [ |
25 [ { "x": 3, "y": 4.5, "z": "hi", "u": true, "v": false }, | 25 [null], |
26 '{"x":3,"y":4.5,"z":"hi","u":true,"v":false}' ], | 26 "[null]" |
27 [ { "x": null }, '{"x":null}' ], | 27 ], |
28 [ { "x": {} }, '{"x":{}}' ], | 28 [ |
29 // Note that -0.0 won't be treated the same in JS. The Json spec seems to | 29 [ |
30 // allow it, though. | 30 [null] |
31 [ { "hi there": 499, "'":-0.0 }, '{"hi there":499,"\'":-0.0}' ], | 31 ], |
32 [ r'\foo', r'"\\foo"' ], | 32 "[[null]]" |
33 ]; | 33 ], |
| 34 [ |
| 35 [ |
| 36 [3] |
| 37 ], |
| 38 "[[3]]" |
| 39 ], |
| 40 [{}, "{}"], |
| 41 [ |
| 42 {"x": 3, "y": 4.5, "z": "hi", "u": true, "v": false}, |
| 43 '{"x":3,"y":4.5,"z":"hi","u":true,"v":false}' |
| 44 ], |
| 45 [ |
| 46 {"x": null}, |
| 47 '{"x":null}' |
| 48 ], |
| 49 [ |
| 50 {"x": {}}, |
| 51 '{"x":{}}' |
| 52 ], |
| 53 // Note that -0.0 won't be treated the same in JS. The Json spec seems to |
| 54 // allow it, though. |
| 55 [ |
| 56 {"hi there": 499, "'": -0.0}, |
| 57 '{"hi there":499,"\'":-0.0}' |
| 58 ], |
| 59 [r'\foo', r'"\\foo"'], |
| 60 ]; |
34 | 61 |
35 bool isJsonEqual(o1, o2) { | 62 bool isJsonEqual(o1, o2) { |
36 if (o1 == o2) return true; | 63 if (o1 == o2) return true; |
37 if (o1 is List && o2 is List) { | 64 if (o1 is List && o2 is List) { |
38 if (o1.length != o2.length) return false; | 65 if (o1.length != o2.length) return false; |
39 for (int i = 0; i < o1.length; i++) { | 66 for (int i = 0; i < o1.length; i++) { |
40 if (!isJsonEqual(o1[i], o2[i])) return false; | 67 if (!isJsonEqual(o1[i], o2[i])) return false; |
41 } | 68 } |
42 return true; | 69 return true; |
43 } | 70 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 Expect.isTrue(isJsonEqual(expected, o)); | 104 Expect.isTrue(isJsonEqual(expected, o)); |
78 asyncEnd(); | 105 asyncEnd(); |
79 }); | 106 }); |
80 } | 107 } |
81 | 108 |
82 void main() { | 109 void main() { |
83 var tests = TESTS.expand((test) { | 110 var tests = TESTS.expand((test) { |
84 var object = test[0]; | 111 var object = test[0]; |
85 var string = test[1]; | 112 var string = test[1]; |
86 var longString = " " | 113 var longString = " " |
87 " " | 114 " " |
88 "$string" | 115 "$string" |
89 " " | 116 " " |
90 " "; | 117 " "; |
91 return [test, [object, longString]]; | 118 return [ |
| 119 test, |
| 120 [object, longString] |
| 121 ]; |
92 }); | 122 }); |
93 for (var test in TESTS) { | 123 for (var test in TESTS) { |
94 var o = test[0]; | 124 var o = test[0]; |
95 var string = test[1]; | 125 var string = test[1]; |
96 checkIsJsonEqual(o, decode(string)); | 126 checkIsJsonEqual(o, decode(string)); |
97 checkIsJsonEqual(o, decode2(string)); | 127 checkIsJsonEqual(o, decode2(string)); |
98 } | 128 } |
99 } | 129 } |
OLD | NEW |