| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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_test; | 5 library json_test; |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import "dart:convert"; | 8 import "dart:convert"; |
| 9 | 9 |
| 10 bool badFormat(e) => e is FormatException; | 10 bool badFormat(e) => e is FormatException; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 } else if (expected is num) { | 27 } else if (expected is num) { |
| 28 Expect.equals(expected is int, actual is int, "$path: same number type"); | 28 Expect.equals(expected is int, actual is int, "$path: same number type"); |
| 29 Expect.isTrue(expected.compareTo(actual) == 0, | 29 Expect.isTrue(expected.compareTo(actual) == 0, |
| 30 "$path: $expected vs. $actual"); | 30 "$path: $expected vs. $actual"); |
| 31 } else { | 31 } else { |
| 32 // String, bool, null. | 32 // String, bool, null. |
| 33 Expect.equals(expected, actual, path); | 33 Expect.equals(expected, actual, path); |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 for (var reviver in [null, (k, v) => v]) { | 36 for (var reviver in [null, (k, v) => v]) { |
| 37 var name = (reviver == null) ? "" : "reviver:"; |
| 37 var value = JSON.decode(json, reviver: reviver); | 38 var value = JSON.decode(json, reviver: reviver); |
| 38 compare(expected, value, "$value"); | 39 compare(expected, value, "$name$value"); |
| 39 value = JSON.decode(" $json ", reviver: reviver); | 40 value = JSON.decode(" $json ", reviver: reviver); |
| 40 compare(expected, value, "-$value-"); | 41 compare(expected, value, "$name-$value-"); |
| 41 value = JSON.decode("[$json]", reviver: reviver); | 42 value = JSON.decode("[$json]", reviver: reviver); |
| 42 compare([expected], value, "[$value]"); | 43 compare([expected], value, "$name[$value]"); |
| 43 value = JSON.decode('{"x":$json}', reviver: reviver); | 44 value = JSON.decode('{"x":$json}', reviver: reviver); |
| 44 compare({"x":expected}, value, "{x:$value}"); | 45 compare({"x":expected}, value, "$name{x:$value}"); |
| 45 } | 46 } |
| 46 } | 47 } |
| 47 | 48 |
| 48 String escape(String s) { | 49 String escape(String s) { |
| 49 var sb = new StringBuffer(); | 50 var sb = new StringBuffer(); |
| 50 for (int i = 0; i < s.length; i++) { | 51 for (int i = 0; i < s.length; i++) { |
| 51 int code = s.codeUnitAt(i); | 52 int code = s.codeUnitAt(i); |
| 52 if (code == '\\'.codeUnitAt(0)) sb.write(r'\\'); | 53 if (code == '\\'.codeUnitAt(0)) sb.write(r'\\'); |
| 53 else if (code == '\"'.codeUnitAt(0)) sb.write(r'\"'); | 54 else if (code == '\"'.codeUnitAt(0)) sb.write(r'\"'); |
| 54 else if (code >= 32 && code < 127) sb.writeCharCode(code); | 55 else if (code >= 32 && code < 127) sb.writeCharCode(code); |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 } | 288 } |
| 288 | 289 |
| 289 main() { | 290 main() { |
| 290 testNumbers(); | 291 testNumbers(); |
| 291 testStrings(); | 292 testStrings(); |
| 292 testWords(); | 293 testWords(); |
| 293 testObjects(); | 294 testObjects(); |
| 294 testArrays(); | 295 testArrays(); |
| 295 testWhitespace(); | 296 testWhitespace(); |
| 296 } | 297 } |
| OLD | NEW |