| 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('jsonTest'); | 5 #library('jsonTest'); |
| 6 | 6 |
| 7 #import('dart:json'); | 7 #import('dart:json'); |
| 8 | 8 |
| 9 main() { | 9 main() { |
| 10 testEscaping(); | 10 testEscaping(); |
| 11 testParse(); |
| 12 testParseInvalid(); |
| 13 } |
| 14 |
| 15 void testParse() { |
| 16 // Scalars. |
| 17 Expect.equals(5, JSON.parse(' 5 ')); |
| 18 Expect.equals(-42, JSON.parse(' -42 ')); |
| 19 Expect.equals(3, JSON.parse(' 3e0 ')); |
| 20 Expect.equals(3.14, JSON.parse(' 3.14 ')); |
| 21 Expect.equals(true, JSON.parse(' true ')); |
| 22 Expect.equals(false, JSON.parse(' false')); |
| 23 Expect.equals(null, JSON.parse(' null ')); |
| 24 Expect.equals(null, JSON.parse('\n\rnull\t')); |
| 25 Expect.equals('hi there" bob', JSON.parse(' "hi there\\" bob" ')); |
| 26 Expect.equals('', JSON.parse(' "" ')); |
| 27 |
| 28 // Lists. |
| 29 Expect.listEquals([], JSON.parse(' [] ')); |
| 30 Expect.listEquals(["entry"], JSON.parse(' ["entry"] ')); |
| 31 Expect.listEquals([true, false], JSON.parse(' [true, false] ')); |
| 32 Expect.listEquals([1, 2, 3], JSON.parse(' [ 1 , 2 , 3 ] ')); |
| 33 |
| 34 // Maps. |
| 35 Expect.mapEquals({}, JSON.parse(' {} ')); |
| 36 Expect.mapEquals({"key": "value"}, JSON.parse(' {"key": "value" } ')); |
| 37 Expect.mapEquals({"key1": 1, "key2": 2}, |
| 38 JSON.parse(' {"key1": 1, "key2": 2} ')); |
| 39 Expect.mapEquals({"key1": 1}, |
| 40 JSON.parse(' { "key1" : 1 } ')); |
| 41 } |
| 42 |
| 43 void testParseInvalid() { |
| 44 void testString(String s) { |
| 45 Expect.throws(() => JSON.parse(s), (e) => e is JSONParseException); |
| 46 } |
| 47 testString(""); |
| 48 testString("3.a"); |
| 49 testString("{ key: value }"); |
| 50 testString("tru"); |
| 51 |
| 52 // Lists |
| 53 testString('[, ]'); |
| 54 testString('["", ]'); |
| 55 testString('["", "]'); |
| 56 testString('["" ""]'); |
| 57 |
| 58 // Maps |
| 59 testString('{"" ""}'); |
| 60 testString('{"": "",}'); |
| 11 } | 61 } |
| 12 | 62 |
| 13 void testEscaping() { | 63 void testEscaping() { |
| 14 Expect.stringEquals('""', JSON.stringify('')); | 64 Expect.stringEquals('""', JSON.stringify('')); |
| 15 Expect.stringEquals('"\\u0000"', JSON.stringify('\u0000')); | 65 Expect.stringEquals('"\\u0000"', JSON.stringify('\u0000')); |
| 16 Expect.stringEquals('"\\u0001"', JSON.stringify('\u0001')); | 66 Expect.stringEquals('"\\u0001"', JSON.stringify('\u0001')); |
| 17 Expect.stringEquals('"\\u0002"', JSON.stringify('\u0002')); | 67 Expect.stringEquals('"\\u0002"', JSON.stringify('\u0002')); |
| 18 Expect.stringEquals('"\\u0003"', JSON.stringify('\u0003')); | 68 Expect.stringEquals('"\\u0003"', JSON.stringify('\u0003')); |
| 19 Expect.stringEquals('"\\u0004"', JSON.stringify('\u0004')); | 69 Expect.stringEquals('"\\u0004"', JSON.stringify('\u0004')); |
| 20 Expect.stringEquals('"\\u0005"', JSON.stringify('\u0005')); | 70 Expect.stringEquals('"\\u0005"', JSON.stringify('\u0005')); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 44 Expect.stringEquals('"\\u001d"', JSON.stringify('\u001d')); | 94 Expect.stringEquals('"\\u001d"', JSON.stringify('\u001d')); |
| 45 Expect.stringEquals('"\\u001e"', JSON.stringify('\u001e')); | 95 Expect.stringEquals('"\\u001e"', JSON.stringify('\u001e')); |
| 46 Expect.stringEquals('"\\u001f"', JSON.stringify('\u001f')); | 96 Expect.stringEquals('"\\u001f"', JSON.stringify('\u001f')); |
| 47 Expect.stringEquals('"\\\""', JSON.stringify('"')); | 97 Expect.stringEquals('"\\\""', JSON.stringify('"')); |
| 48 Expect.stringEquals('"\\\\"', JSON.stringify('\\')); | 98 Expect.stringEquals('"\\\\"', JSON.stringify('\\')); |
| 49 Expect.stringEquals('"Got \\b, \\f, \\n, \\r, \\t, \\u0000, \\\\, and \\"."', | 99 Expect.stringEquals('"Got \\b, \\f, \\n, \\r, \\t, \\u0000, \\\\, and \\"."', |
| 50 JSON.stringify('Got \b, \f, \n, \r, \t, \u0000, \\, and ".')); | 100 JSON.stringify('Got \b, \f, \n, \r, \t, \u0000, \\, and ".')); |
| 51 Expect.stringEquals('"Got \\b\\f\\n\\r\\t\\u0000\\\\\\"."', | 101 Expect.stringEquals('"Got \\b\\f\\n\\r\\t\\u0000\\\\\\"."', |
| 52 JSON.stringify('Got \b\f\n\r\t\u0000\\".')); | 102 JSON.stringify('Got \b\f\n\r\t\u0000\\".')); |
| 53 } | 103 } |
| OLD | NEW |