| 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; |
| 11 | 11 |
| 12 void testJson(json, expected) { | 12 void testJson(json, expected) { |
| 13 var value = JSON.decode(json); | |
| 14 compare(expected, actual, path) { | 13 compare(expected, actual, path) { |
| 15 if (expected is List) { | 14 if (expected is List) { |
| 16 Expect.isTrue(actual is List); | 15 Expect.isTrue(actual is List); |
| 17 Expect.equals(expected.length, actual.length, "$path: List length"); | 16 Expect.equals(expected.length, actual.length, "$path: List length"); |
| 18 for (int i = 0; i < expected.length; i++) { | 17 for (int i = 0; i < expected.length; i++) { |
| 19 compare(expected[i], actual[i], "$path[$i]"); | 18 compare(expected[i], actual[i], "$path[$i]"); |
| 20 } | 19 } |
| 21 } else if (expected is Map) { | 20 } else if (expected is Map) { |
| 22 Expect.isTrue(actual is Map); | 21 Expect.isTrue(actual is Map); |
| 23 Expect.equals(expected.length, actual.length, "$path: Map size"); | 22 Expect.equals(expected.length, actual.length, "$path: Map size"); |
| 24 expected.forEach((key, value) { | 23 expected.forEach((key, value) { |
| 25 Expect.isTrue(actual.containsKey(key)); | 24 Expect.isTrue(actual.containsKey(key)); |
| 26 compare(value, actual[key], "$path[$key]"); | 25 compare(value, actual[key], "$path[$key]"); |
| 27 }); | 26 }); |
| 28 } else if (expected is num) { | 27 } else if (expected is num) { |
| 29 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"); |
| 30 Expect.isTrue(expected.compareTo(actual) == 0, | 29 Expect.isTrue(expected.compareTo(actual) == 0, |
| 31 "$path: $expected vs. $actual"); | 30 "$path: $expected vs. $actual"); |
| 32 } else { | 31 } else { |
| 33 // String, bool, null. | 32 // String, bool, null. |
| 34 Expect.equals(expected, actual, path); | 33 Expect.equals(expected, actual, path); |
| 35 } | 34 } |
| 36 } | 35 } |
| 37 compare(expected, value, "value"); | 36 for (var reviver in [null, (k, v) => v]) { |
| 37 var value = JSON.decode(json, reviver: reviver); |
| 38 compare(expected, value, "$value"); |
| 39 value = JSON.decode(" $json ", reviver: reviver); |
| 40 compare(expected, value, "-$value-"); |
| 41 value = JSON.decode("[$json]", reviver: reviver); |
| 42 compare([expected], value, "[$value]"); |
| 43 value = JSON.decode('{"x":$json}', reviver: reviver); |
| 44 compare({"x":expected}, value, "{x:$value}"); |
| 45 } |
| 38 } | 46 } |
| 39 | 47 |
| 40 String escape(String s) { | 48 String escape(String s) { |
| 41 var sb = new StringBuffer(); | 49 var sb = new StringBuffer(); |
| 42 for (int i = 0; i < s.length; i++) { | 50 for (int i = 0; i < s.length; i++) { |
| 43 int code = s.codeUnitAt(i); | 51 int code = s.codeUnitAt(i); |
| 44 if (code == '\\'.codeUnitAt(0)) sb.write(r'\\'); | 52 if (code == '\\'.codeUnitAt(0)) sb.write(r'\\'); |
| 45 else if (code == '\"'.codeUnitAt(0)) sb.write(r'\"'); | 53 else if (code == '\"'.codeUnitAt(0)) sb.write(r'\"'); |
| 46 else if (code >= 32 && code < 127) sb.writeCharCode(code); | 54 else if (code >= 32 && code < 127) sb.writeCharCode(code); |
| 47 else { | 55 else { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 } | 153 } |
| 146 | 154 |
| 147 testStrings() { | 155 testStrings() { |
| 148 // String parser accepts and understands escapes. | 156 // String parser accepts and understands escapes. |
| 149 var input = r'"\u0000\uffff\n\r\f\t\b\/\\\"' '\x20\ufffd\uffff"'; | 157 var input = r'"\u0000\uffff\n\r\f\t\b\/\\\"' '\x20\ufffd\uffff"'; |
| 150 var expected = "\u0000\uffff\n\r\f\t\b\/\\\"\x20\ufffd\uffff"; | 158 var expected = "\u0000\uffff\n\r\f\t\b\/\\\"\x20\ufffd\uffff"; |
| 151 testJson(input, expected); | 159 testJson(input, expected); |
| 152 // Empty string. | 160 // Empty string. |
| 153 testJson(r'""', ""); | 161 testJson(r'""', ""); |
| 154 // Escape first. | 162 // Escape first. |
| 155 testJson(r'"\"........"', "\"........"); | 163 var escapes = { |
| 156 // Escape last. | 164 "f": "\f", |
| 157 testJson(r'"........\""', "........\""); | 165 "b": "\b", |
| 158 // Escape middle. | 166 "n": "\n", |
| 159 testJson(r'"....\"...."', "....\"...."); | 167 "r": "\r", |
| 168 "t": "\t", |
| 169 r"\": r"\", |
| 170 '"': '"', |
| 171 "/": "/", |
| 172 }; |
| 173 escapes.forEach((esc, lit) { |
| 174 testJson('"\\$esc........"', "$lit........"); |
| 175 // Escape last. |
| 176 testJson('"........\\$esc"', "........$lit"); |
| 177 // Escape middle. |
| 178 testJson('"....\\$esc...."', "....$lit...."); |
| 179 }); |
| 160 | 180 |
| 161 // Does not accept single quotes. | 181 // Does not accept single quotes. |
| 162 testThrows(r"''"); | 182 testThrows(r"''"); |
| 163 // Throws on unterminated strings. | 183 // Throws on unterminated strings. |
| 164 testThrows(r'"......\"'); | 184 testThrows(r'"......\"'); |
| 165 // Throws on unterminated escapes. | 185 // Throws on unterminated escapes. |
| 166 testThrows(r'"\'); // ' is not escaped. | 186 testThrows(r'"\'); // ' is not escaped. |
| 167 testThrows(r'"\a"'); | 187 testThrows(r'"\a"'); |
| 168 testThrows(r'"\u"'); | 188 testThrows(r'"\u"'); |
| 169 testThrows(r'"\u1"'); | 189 testThrows(r'"\u1"'); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 } | 287 } |
| 268 | 288 |
| 269 main() { | 289 main() { |
| 270 testNumbers(); | 290 testNumbers(); |
| 271 testStrings(); | 291 testStrings(); |
| 272 testWords(); | 292 testWords(); |
| 273 testObjects(); | 293 testObjects(); |
| 274 testArrays(); | 294 testArrays(); |
| 275 testWhitespace(); | 295 testWhitespace(); |
| 276 } | 296 } |
| OLD | NEW |