| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library jsonTest; | |
| 6 | |
| 7 import "package:expect/expect.dart"; | |
| 8 import 'dart:convert'; | |
| 9 | |
| 10 main() { | |
| 11 testEscaping(); | |
| 12 testParse(); | |
| 13 testParseInvalid(); | |
| 14 } | |
| 15 | |
| 16 void testParse() { | |
| 17 // Scalars. | |
| 18 Expect.equals(5, JSON.decode(' 5 ')); | |
| 19 Expect.equals(-42, JSON.decode(' -42 ')); | |
| 20 Expect.equals(3, JSON.decode(' 3e0 ')); | |
| 21 Expect.equals(3.14, JSON.decode(' 3.14 ')); | |
| 22 Expect.equals(1.0E-06, JSON.decode(' 1.0E-06 ')); | |
| 23 Expect.equals(0, JSON.decode("0")); | |
| 24 Expect.equals(1, JSON.decode("1")); | |
| 25 Expect.equals(0.1, JSON.decode("0.1")); | |
| 26 Expect.equals(1.1, JSON.decode("1.1")); | |
| 27 Expect.equals(1.1, JSON.decode("1.100000")); | |
| 28 Expect.equals(1.111111, JSON.decode("1.111111")); | |
| 29 Expect.equals(-0, JSON.decode("-0")); | |
| 30 Expect.equals(-1, JSON.decode("-1")); | |
| 31 Expect.equals(-0.1, JSON.decode("-0.1")); | |
| 32 Expect.equals(-1.1, JSON.decode("-1.1")); | |
| 33 Expect.equals(-1.1, JSON.decode("-1.100000")); | |
| 34 Expect.equals(-1.111111, JSON.decode("-1.111111")); | |
| 35 Expect.equals(11, JSON.decode("1.1e1")); | |
| 36 Expect.equals(11, JSON.decode("1.1e+1")); | |
| 37 Expect.equals(0.11, JSON.decode("1.1e-1")); | |
| 38 Expect.equals(11, JSON.decode("1.1E1")); | |
| 39 Expect.equals(11, JSON.decode("1.1E+1")); | |
| 40 Expect.equals(0.11, JSON.decode("1.1E-1")); | |
| 41 Expect.equals(1E0, JSON.decode(" 1E0")); | |
| 42 Expect.equals(1E+0, JSON.decode(" 1E+0")); | |
| 43 Expect.equals(1E-0, JSON.decode(" 1E-0")); | |
| 44 Expect.equals(1E00, JSON.decode(" 1E00")); | |
| 45 Expect.equals(1E+00, JSON.decode(" 1E+00")); | |
| 46 Expect.equals(1E-00, JSON.decode(" 1E-00")); | |
| 47 Expect.equals(1E+10, JSON.decode(" 1E+10")); | |
| 48 Expect.equals(1E+010, JSON.decode(" 1E+010")); | |
| 49 Expect.equals(1E+0010, JSON.decode(" 1E+0010")); | |
| 50 Expect.equals(1E10, JSON.decode(" 1E10")); | |
| 51 Expect.equals(1E010, JSON.decode(" 1E010")); | |
| 52 Expect.equals(1E0010, JSON.decode(" 1E0010")); | |
| 53 Expect.equals(1E-10, JSON.decode(" 1E-10")); | |
| 54 Expect.equals(1E-0010, JSON.decode(" 1E-0010")); | |
| 55 Expect.equals(1E0, JSON.decode(" 1e0")); | |
| 56 Expect.equals(1E+0, JSON.decode(" 1e+0")); | |
| 57 Expect.equals(1E-0, JSON.decode(" 1e-0")); | |
| 58 Expect.equals(1E00, JSON.decode(" 1e00")); | |
| 59 Expect.equals(1E+00, JSON.decode(" 1e+00")); | |
| 60 Expect.equals(1E-00, JSON.decode(" 1e-00")); | |
| 61 Expect.equals(1E+10, JSON.decode(" 1e+10")); | |
| 62 Expect.equals(1E+010, JSON.decode(" 1e+010")); | |
| 63 Expect.equals(1E+0010, JSON.decode(" 1e+0010")); | |
| 64 Expect.equals(1E10, JSON.decode(" 1e10")); | |
| 65 Expect.equals(1E010, JSON.decode(" 1e010")); | |
| 66 Expect.equals(1E0010, JSON.decode(" 1e0010")); | |
| 67 Expect.equals(1E-10, JSON.decode(" 1e-10")); | |
| 68 Expect.equals(1E-0010, JSON.decode(" 1e-0010")); | |
| 69 Expect.equals(true, JSON.decode(' true ')); | |
| 70 Expect.equals(false, JSON.decode(' false')); | |
| 71 Expect.equals(null, JSON.decode(' null ')); | |
| 72 Expect.equals(null, JSON.decode('\n\rnull\t')); | |
| 73 Expect.equals('hi there" bob', JSON.decode(' "hi there\\" bob" ')); | |
| 74 Expect.equals('', JSON.decode(' "" ')); | |
| 75 | |
| 76 // Lists. | |
| 77 Expect.listEquals([], JSON.decode(' [] ')); | |
| 78 Expect.listEquals(["entry"], JSON.decode(' ["entry"] ')); | |
| 79 Expect.listEquals([true, false], JSON.decode(' [true, false] ')); | |
| 80 Expect.listEquals([1, 2, 3], JSON.decode(' [ 1 , 2 , 3 ] ')); | |
| 81 | |
| 82 // Maps. | |
| 83 Expect.mapEquals({}, JSON.decode(' {} ')); | |
| 84 Expect.mapEquals({"key": "value"}, JSON.decode(' {"key": "value" } ')); | |
| 85 Expect.mapEquals({"key1": 1, "key2": 2}, | |
| 86 JSON.decode(' {"key1": 1, "key2": 2} ')); | |
| 87 Expect.mapEquals({"key1": 1}, | |
| 88 JSON.decode(' { "key1" : 1 } ')); | |
| 89 } | |
| 90 | |
| 91 void testParseInvalid() { | |
| 92 void testString(String s) { | |
| 93 Expect.throws(() => JSON.decode(s), (e) => e is FormatException); | |
| 94 } | |
| 95 // Scalars | |
| 96 testString(""); | |
| 97 testString("-"); | |
| 98 testString("-."); | |
| 99 testString("3.a"); | |
| 100 testString("{ key: value }"); | |
| 101 testString("tru"); | |
| 102 testString("1E--6"); | |
| 103 testString("1E-+6"); | |
| 104 testString("1E+-6"); | |
| 105 testString("1E++6"); | |
| 106 testString("1E6.6"); | |
| 107 testString("1E-6.6"); | |
| 108 testString("1E+6.6"); | |
| 109 | |
| 110 // JavaScript number literals not valid in JSON. | |
| 111 testString('[01]'); | |
| 112 testString('[.1]'); | |
| 113 testString('[1.]'); | |
| 114 testString('[1.e1]'); | |
| 115 testString('[-.1]'); | |
| 116 testString('[-1.]'); | |
| 117 | |
| 118 // Plain invalid number literals. | |
| 119 testString('-'); | |
| 120 testString('--1'); | |
| 121 testString('-1e'); | |
| 122 testString('1e--1]'); | |
| 123 testString('1e+-1'); | |
| 124 testString('1e-+1'); | |
| 125 testString('1e++1'); | |
| 126 | |
| 127 // Lists | |
| 128 testString('[, ]'); | |
| 129 testString('["", ]'); | |
| 130 testString('["", "]'); | |
| 131 testString('["" ""]'); | |
| 132 | |
| 133 // Maps | |
| 134 testString('{"" ""}'); | |
| 135 testString('{"": "",}'); | |
| 136 } | |
| 137 | |
| 138 void testEscaping() { | |
| 139 Expect.stringEquals('""', JSON.encode('')); | |
| 140 Expect.stringEquals('"\\u0000"', JSON.encode('\u0000')); | |
| 141 Expect.stringEquals('"\\u0001"', JSON.encode('\u0001')); | |
| 142 Expect.stringEquals('"\\u0002"', JSON.encode('\u0002')); | |
| 143 Expect.stringEquals('"\\u0003"', JSON.encode('\u0003')); | |
| 144 Expect.stringEquals('"\\u0004"', JSON.encode('\u0004')); | |
| 145 Expect.stringEquals('"\\u0005"', JSON.encode('\u0005')); | |
| 146 Expect.stringEquals('"\\u0006"', JSON.encode('\u0006')); | |
| 147 Expect.stringEquals('"\\u0007"', JSON.encode('\u0007')); | |
| 148 Expect.stringEquals('"\\b"', JSON.encode('\u0008')); | |
| 149 Expect.stringEquals('"\\t"', JSON.encode('\u0009')); | |
| 150 Expect.stringEquals('"\\n"', JSON.encode('\u000a')); | |
| 151 Expect.stringEquals('"\\u000b"', JSON.encode('\u000b')); | |
| 152 Expect.stringEquals('"\\f"', JSON.encode('\u000c')); | |
| 153 Expect.stringEquals('"\\r"', JSON.encode('\u000d')); | |
| 154 Expect.stringEquals('"\\u000e"', JSON.encode('\u000e')); | |
| 155 Expect.stringEquals('"\\u000f"', JSON.encode('\u000f')); | |
| 156 Expect.stringEquals('"\\u0010"', JSON.encode('\u0010')); | |
| 157 Expect.stringEquals('"\\u0011"', JSON.encode('\u0011')); | |
| 158 Expect.stringEquals('"\\u0012"', JSON.encode('\u0012')); | |
| 159 Expect.stringEquals('"\\u0013"', JSON.encode('\u0013')); | |
| 160 Expect.stringEquals('"\\u0014"', JSON.encode('\u0014')); | |
| 161 Expect.stringEquals('"\\u0015"', JSON.encode('\u0015')); | |
| 162 Expect.stringEquals('"\\u0016"', JSON.encode('\u0016')); | |
| 163 Expect.stringEquals('"\\u0017"', JSON.encode('\u0017')); | |
| 164 Expect.stringEquals('"\\u0018"', JSON.encode('\u0018')); | |
| 165 Expect.stringEquals('"\\u0019"', JSON.encode('\u0019')); | |
| 166 Expect.stringEquals('"\\u001a"', JSON.encode('\u001a')); | |
| 167 Expect.stringEquals('"\\u001b"', JSON.encode('\u001b')); | |
| 168 Expect.stringEquals('"\\u001c"', JSON.encode('\u001c')); | |
| 169 Expect.stringEquals('"\\u001d"', JSON.encode('\u001d')); | |
| 170 Expect.stringEquals('"\\u001e"', JSON.encode('\u001e')); | |
| 171 Expect.stringEquals('"\\u001f"', JSON.encode('\u001f')); | |
| 172 Expect.stringEquals('"\\\""', JSON.encode('"')); | |
| 173 Expect.stringEquals('"\\\\"', JSON.encode('\\')); | |
| 174 Expect.stringEquals('"Got \\b, \\f, \\n, \\r, \\t, \\u0000, \\\\, and \\"."', | |
| 175 JSON.encode('Got \b, \f, \n, \r, \t, \u0000, \\, and ".')); | |
| 176 Expect.stringEquals('"Got \\b\\f\\n\\r\\t\\u0000\\\\\\"."', | |
| 177 JSON.encode('Got \b\f\n\r\t\u0000\\".')); | |
| 178 } | |
| OLD | NEW |