| 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 "dart:json"; | 7 import "dart:json"; |
| 8 | 8 |
| 9 bool badFormat(e) => e is FormatException; | 9 bool badFormat(e) => e is FormatException; |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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 compare(expected, value, "value"); | 36 compare(expected, value, "value"); |
| 37 } | 37 } |
| 38 | 38 |
| 39 String escape(String s) { |
| 40 var sb = new StringBuffer(); |
| 41 for (int i = 0; i < s.length; i++) { |
| 42 int code = s.codeUnitAt(i); |
| 43 if (code == '\\'.charCodeAt(0)) sb.write(r'\\'); |
| 44 else if (code == '\"'.charCodeAt(0)) sb.write(r'\"'); |
| 45 else if (code >= 32 && code < 127) sb.writeCharCode(code); |
| 46 else { |
| 47 String hex = '000${code.toRadixString(16)}'; |
| 48 sb.write(r'\u' '${hex.substring(hex.length - 4)}'); |
| 49 } |
| 50 } |
| 51 return '$sb'; |
| 52 } |
| 53 |
| 39 void testThrows(json) { | 54 void testThrows(json) { |
| 40 Expect.throws(() => parse(json), badFormat); | 55 Expect.throws(() => parse(json), badFormat, "json = '${escape(json)}'"); |
| 41 } | 56 } |
| 42 | 57 |
| 43 testNumbers() { | 58 testNumbers() { |
| 44 // Positive tests for number formats. | 59 // Positive tests for number formats. |
| 45 var integerList = ["0","9","9999"]; | 60 var integerList = ["0","9","9999"]; |
| 46 var signList = ["", "-"]; | 61 var signList = ["", "-"]; |
| 47 var fractionList = ["", ".0", ".1", ".99999"]; | 62 var fractionList = ["", ".0", ".1", ".99999"]; |
| 48 var exponentList = [""]; | 63 var exponentList = [""]; |
| 49 for (var exphead in ["e", "E", "e-", "E-", "e+", "E+"]) { | 64 for (var exphead in ["e", "E", "e-", "E-", "e+", "E+"]) { |
| 50 for (var expval in ["0", "1", "200"]) { | 65 for (var expval in ["0", "1", "200"]) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } | 105 } |
| 91 } | 106 } |
| 92 } | 107 } |
| 93 } | 108 } |
| 94 // Doubles overflow to Infinity. | 109 // Doubles overflow to Infinity. |
| 95 testJson("1e+400", double.INFINITY); | 110 testJson("1e+400", double.INFINITY); |
| 96 // (Integers do not, but we don't have those on dart2js). | 111 // (Integers do not, but we don't have those on dart2js). |
| 97 | 112 |
| 98 // Integer part cannot be omitted: | 113 // Integer part cannot be omitted: |
| 99 testError(integers: ""); | 114 testError(integers: ""); |
| 100 // Initial zero only allowed for zero integer part. | 115 |
| 101 testError(integers: ["00", "01"]); | 116 // Test for "Initial zero only allowed for zero integer part" moved to |
| 117 // json_strict_test.dart because IE's JSON.parse accepts additional initial |
| 118 // zeros. |
| 119 |
| 102 // Only minus allowed as sign. | 120 // Only minus allowed as sign. |
| 103 testError(signs: "+"); | 121 testError(signs: "+"); |
| 104 // Requires digits after decimal point. | 122 // Requires digits after decimal point. |
| 105 testError(fractions: "."); | 123 testError(fractions: "."); |
| 106 // Requires exponent digts, and only digits. | 124 // Requires exponent digts, and only digits. |
| 107 testError(exponents: ["e", "e+", "e-", "e.0"]); | 125 testError(exponents: ["e", "e+", "e-", "e.0"]); |
| 108 | 126 |
| 109 // No whitespace inside numbers. | 127 // No whitespace inside numbers. |
| 110 testThrows("- 2.2e+2"); | 128 // Additional case "- 2.2e+2" in json_strict_test.dart. |
| 111 testThrows("-2 .2e+2"); | 129 testThrows("-2 .2e+2"); |
| 112 testThrows("-2. 2e+2"); | 130 testThrows("-2. 2e+2"); |
| 113 testThrows("-2.2 e+2"); | 131 testThrows("-2.2 e+2"); |
| 114 testThrows("-2.2e +2"); | 132 testThrows("-2.2e +2"); |
| 115 testThrows("-2.2e+ 2"); | 133 testThrows("-2.2e+ 2"); |
| 116 | 134 |
| 117 testThrows("[2.,2]"); | 135 testThrows("[2.,2]"); |
| 118 testThrows("{2.:2}"); | 136 testThrows("{2.:2}"); |
| 119 } | 137 } |
| 120 | 138 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 testWhitespace() { | 234 testWhitespace() { |
| 217 // Valid white-space characters. | 235 // Valid white-space characters. |
| 218 var v = '\t\r\n\ '; | 236 var v = '\t\r\n\ '; |
| 219 // Invalid white-space and non-recognized characters. | 237 // Invalid white-space and non-recognized characters. |
| 220 var invalids = ['\x00', '\f', '\x08', '\\', '\xa0','\u2028', '\u2029']; | 238 var invalids = ['\x00', '\f', '\x08', '\\', '\xa0','\u2028', '\u2029']; |
| 221 | 239 |
| 222 // Valid whitespace accepted "everywhere". | 240 // Valid whitespace accepted "everywhere". |
| 223 testJson('$v[${v}-2.2e2$v,$v{$v"key"$v:${v}true$v}$v,$v"ab"$v]$v', | 241 testJson('$v[${v}-2.2e2$v,$v{$v"key"$v:${v}true$v}$v,$v"ab"$v]$v', |
| 224 [-2.2e2, {"key": true}, "ab"]); | 242 [-2.2e2, {"key": true}, "ab"]); |
| 225 | 243 |
| 244 // IE9 accepts invalid characters at the end, so some of these tests have been |
| 245 // moved to json_strict_test.dart. |
| 226 for (var i in invalids) { | 246 for (var i in invalids) { |
| 227 testThrows('${i}"s"'); | 247 testThrows('${i}"s"'); |
| 228 testThrows('"s"${i}'); | |
| 229 testThrows('42${i}'); | 248 testThrows('42${i}'); |
| 230 testThrows('$i[]'); | 249 testThrows('$i[]'); |
| 231 testThrows('[$i]'); | 250 testThrows('[$i]'); |
| 232 testThrows('[$i"s"]'); | 251 testThrows('[$i"s"]'); |
| 233 testThrows('["s"$i]'); | 252 testThrows('["s"$i]'); |
| 234 testThrows('[]$i'); | |
| 235 testThrows('$i{"k":"v"}'); | 253 testThrows('$i{"k":"v"}'); |
| 236 testThrows('{$i"k":"v"}'); | 254 testThrows('{$i"k":"v"}'); |
| 237 testThrows('{"k"$i:"v"}'); | 255 testThrows('{"k"$i:"v"}'); |
| 238 testThrows('{"k":$i"v"}'); | 256 testThrows('{"k":$i"v"}'); |
| 239 testThrows('{"k":"v"$i}'); | 257 testThrows('{"k":"v"$i}'); |
| 240 testThrows('{"k":"v"}$i'); | |
| 241 } | 258 } |
| 242 } | 259 } |
| 243 | 260 |
| 244 main() { | 261 main() { |
| 245 testNumbers(); | 262 testNumbers(); |
| 246 testStrings(); | 263 testStrings(); |
| 247 testWords(); | 264 testWords(); |
| 248 testObjects(); | 265 testObjects(); |
| 249 testArrays(); | 266 testArrays(); |
| 250 testWhitespace(); | 267 testWhitespace(); |
| 251 } | 268 } |
| OLD | NEW |