| 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(); | 11 testParse(); |
| 12 testParseInvalid(); | 12 testParseInvalid(); |
| 13 } | 13 } |
| 14 | 14 |
| 15 void testParse() { | 15 void testParse() { |
| 16 // Scalars. | 16 // Scalars. |
| 17 Expect.equals(5, JSON.parse(' 5 ')); | 17 Expect.equals(5, JSON.parse(' 5 ')); |
| 18 Expect.equals(-42, JSON.parse(' -42 ')); | 18 Expect.equals(-42, JSON.parse(' -42 ')); |
| 19 Expect.equals(3, JSON.parse(' 3e0 ')); | 19 Expect.equals(3, JSON.parse(' 3e0 ')); |
| 20 Expect.equals(3.14, JSON.parse(' 3.14 ')); | 20 Expect.equals(3.14, JSON.parse(' 3.14 ')); |
| 21 Expect.equals(1.0E-06, JSON.parse(' 1.0E-06 ')); |
| 22 Expect.equals(0, JSON.parse("0")); |
| 23 Expect.equals(1, JSON.parse("1")); |
| 24 Expect.equals(0.1, JSON.parse("0.1")); |
| 25 Expect.equals(1.1, JSON.parse("1.1")); |
| 26 Expect.equals(1.1, JSON.parse("1.100000")); |
| 27 Expect.equals(1.111111, JSON.parse("1.111111")); |
| 28 Expect.equals(-0, JSON.parse("-0")); |
| 29 Expect.equals(-1, JSON.parse("-1")); |
| 30 Expect.equals(-0.1, JSON.parse("-0.1")); |
| 31 Expect.equals(-1.1, JSON.parse("-1.1")); |
| 32 Expect.equals(-1.1, JSON.parse("-1.100000")); |
| 33 Expect.equals(-1.111111, JSON.parse("-1.111111")); |
| 34 Expect.equals(11, JSON.parse("1.1e1")); |
| 35 Expect.equals(11, JSON.parse("1.1e+1")); |
| 36 Expect.equals(0.11, JSON.parse("1.1e-1")); |
| 37 Expect.equals(11, JSON.parse("1.1E1")); |
| 38 Expect.equals(11, JSON.parse("1.1E+1")); |
| 39 Expect.equals(0.11, JSON.parse("1.1E-1")); |
| 40 Expect.equals(1E0, JSON.parse(" 1E0")); |
| 41 Expect.equals(1E+0, JSON.parse(" 1E+0")); |
| 42 Expect.equals(1E-0, JSON.parse(" 1E-0")); |
| 43 Expect.equals(1E00, JSON.parse(" 1E00")); |
| 44 Expect.equals(1E+00, JSON.parse(" 1E+00")); |
| 45 Expect.equals(1E-00, JSON.parse(" 1E-00")); |
| 46 Expect.equals(1E+10, JSON.parse(" 1E+10")); |
| 47 Expect.equals(1E+010, JSON.parse(" 1E+010")); |
| 48 Expect.equals(1E+0010, JSON.parse(" 1E+0010")); |
| 49 Expect.equals(1E10, JSON.parse(" 1E10")); |
| 50 Expect.equals(1E010, JSON.parse(" 1E010")); |
| 51 Expect.equals(1E0010, JSON.parse(" 1E0010")); |
| 52 Expect.equals(1E-10, JSON.parse(" 1E-10")); |
| 53 Expect.equals(1E-0010, JSON.parse(" 1E-0010")); |
| 54 Expect.equals(1E0, JSON.parse(" 1e0")); |
| 55 Expect.equals(1E+0, JSON.parse(" 1e+0")); |
| 56 Expect.equals(1E-0, JSON.parse(" 1e-0")); |
| 57 Expect.equals(1E00, JSON.parse(" 1e00")); |
| 58 Expect.equals(1E+00, JSON.parse(" 1e+00")); |
| 59 Expect.equals(1E-00, JSON.parse(" 1e-00")); |
| 60 Expect.equals(1E+10, JSON.parse(" 1e+10")); |
| 61 Expect.equals(1E+010, JSON.parse(" 1e+010")); |
| 62 Expect.equals(1E+0010, JSON.parse(" 1e+0010")); |
| 63 Expect.equals(1E10, JSON.parse(" 1e10")); |
| 64 Expect.equals(1E010, JSON.parse(" 1e010")); |
| 65 Expect.equals(1E0010, JSON.parse(" 1e0010")); |
| 66 Expect.equals(1E-10, JSON.parse(" 1e-10")); |
| 67 Expect.equals(1E-0010, JSON.parse(" 1e-0010")); |
| 21 Expect.equals(true, JSON.parse(' true ')); | 68 Expect.equals(true, JSON.parse(' true ')); |
| 22 Expect.equals(false, JSON.parse(' false')); | 69 Expect.equals(false, JSON.parse(' false')); |
| 23 Expect.equals(null, JSON.parse(' null ')); | 70 Expect.equals(null, JSON.parse(' null ')); |
| 24 Expect.equals(null, JSON.parse('\n\rnull\t')); | 71 Expect.equals(null, JSON.parse('\n\rnull\t')); |
| 25 Expect.equals('hi there" bob', JSON.parse(' "hi there\\" bob" ')); | 72 Expect.equals('hi there" bob', JSON.parse(' "hi there\\" bob" ')); |
| 26 Expect.equals('', JSON.parse(' "" ')); | 73 Expect.equals('', JSON.parse(' "" ')); |
| 27 | 74 |
| 28 // Lists. | 75 // Lists. |
| 29 Expect.listEquals([], JSON.parse(' [] ')); | 76 Expect.listEquals([], JSON.parse(' [] ')); |
| 30 Expect.listEquals(["entry"], JSON.parse(' ["entry"] ')); | 77 Expect.listEquals(["entry"], JSON.parse(' ["entry"] ')); |
| 31 Expect.listEquals([true, false], JSON.parse(' [true, false] ')); | 78 Expect.listEquals([true, false], JSON.parse(' [true, false] ')); |
| 32 Expect.listEquals([1, 2, 3], JSON.parse(' [ 1 , 2 , 3 ] ')); | 79 Expect.listEquals([1, 2, 3], JSON.parse(' [ 1 , 2 , 3 ] ')); |
| 33 | 80 |
| 34 // Maps. | 81 // Maps. |
| 35 Expect.mapEquals({}, JSON.parse(' {} ')); | 82 Expect.mapEquals({}, JSON.parse(' {} ')); |
| 36 Expect.mapEquals({"key": "value"}, JSON.parse(' {"key": "value" } ')); | 83 Expect.mapEquals({"key": "value"}, JSON.parse(' {"key": "value" } ')); |
| 37 Expect.mapEquals({"key1": 1, "key2": 2}, | 84 Expect.mapEquals({"key1": 1, "key2": 2}, |
| 38 JSON.parse(' {"key1": 1, "key2": 2} ')); | 85 JSON.parse(' {"key1": 1, "key2": 2} ')); |
| 39 Expect.mapEquals({"key1": 1}, | 86 Expect.mapEquals({"key1": 1}, |
| 40 JSON.parse(' { "key1" : 1 } ')); | 87 JSON.parse(' { "key1" : 1 } ')); |
| 41 } | 88 } |
| 42 | 89 |
| 43 void testParseInvalid() { | 90 void testParseInvalid() { |
| 44 void testString(String s) { | 91 void testString(String s) { |
| 45 // TODO(ajohnsen): Require JSONParseException exception once all JSON libs | 92 // TODO(ajohnsen): Require JSONParseException exception once all JSON libs |
| 46 // have been updated. | 93 // have been updated. |
| 47 Expect.throws(() => JSON.parse(s)); | 94 Expect.throws(() => JSON.parse(s)); |
| 48 } | 95 } |
| 96 // Scalars |
| 49 testString(""); | 97 testString(""); |
| 50 testString("-"); | 98 testString("-"); |
| 51 testString("-."); | 99 testString("-."); |
| 52 testString("3.a"); | 100 testString("3.a"); |
| 53 testString("{ key: value }"); | 101 testString("{ key: value }"); |
| 54 testString("tru"); | 102 testString("tru"); |
| 103 testString("1E--6"); |
| 104 testString("1E-+6"); |
| 105 testString("1E+-6"); |
| 106 testString("1E++6"); |
| 107 testString("1E6.6"); |
| 108 testString("1E-6.6"); |
| 109 testString("1E+6.6"); |
| 110 |
| 111 // JavaScript number literals not valid in JSON. |
| 112 testString('[01]'); |
| 113 testString('[.1]'); |
| 114 testString('[1.]'); |
| 115 testString('[1.e1]'); |
| 116 testString('[-.1]'); |
| 117 testString('[-1.]'); |
| 118 |
| 119 // Plain invalid number literals. |
| 120 testString('-'); |
| 121 testString('--1'); |
| 122 testString('-1e'); |
| 123 testString('1e--1]'); |
| 124 testString('1e+-1'); |
| 125 testString('1e-+1'); |
| 126 testString('1e++1'); |
| 55 | 127 |
| 56 // Lists | 128 // Lists |
| 57 testString('[, ]'); | 129 testString('[, ]'); |
| 58 testString('["", ]'); | 130 testString('["", ]'); |
| 59 testString('["", "]'); | 131 testString('["", "]'); |
| 60 testString('["" ""]'); | 132 testString('["" ""]'); |
| 61 | 133 |
| 62 // Maps | 134 // Maps |
| 63 testString('{"" ""}'); | 135 testString('{"" ""}'); |
| 64 testString('{"": "",}'); | 136 testString('{"": "",}'); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 Expect.stringEquals('"\\u001d"', JSON.stringify('\u001d')); | 170 Expect.stringEquals('"\\u001d"', JSON.stringify('\u001d')); |
| 99 Expect.stringEquals('"\\u001e"', JSON.stringify('\u001e')); | 171 Expect.stringEquals('"\\u001e"', JSON.stringify('\u001e')); |
| 100 Expect.stringEquals('"\\u001f"', JSON.stringify('\u001f')); | 172 Expect.stringEquals('"\\u001f"', JSON.stringify('\u001f')); |
| 101 Expect.stringEquals('"\\\""', JSON.stringify('"')); | 173 Expect.stringEquals('"\\\""', JSON.stringify('"')); |
| 102 Expect.stringEquals('"\\\\"', JSON.stringify('\\')); | 174 Expect.stringEquals('"\\\\"', JSON.stringify('\\')); |
| 103 Expect.stringEquals('"Got \\b, \\f, \\n, \\r, \\t, \\u0000, \\\\, and \\"."', | 175 Expect.stringEquals('"Got \\b, \\f, \\n, \\r, \\t, \\u0000, \\\\, and \\"."', |
| 104 JSON.stringify('Got \b, \f, \n, \r, \t, \u0000, \\, and ".')); | 176 JSON.stringify('Got \b, \f, \n, \r, \t, \u0000, \\, and ".')); |
| 105 Expect.stringEquals('"Got \\b\\f\\n\\r\\t\\u0000\\\\\\"."', | 177 Expect.stringEquals('"Got \\b\\f\\n\\r\\t\\u0000\\\\\\"."', |
| 106 JSON.stringify('Got \b\f\n\r\t\u0000\\".')); | 178 JSON.stringify('Got \b\f\n\r\t\u0000\\".')); |
| 107 } | 179 } |
| OLD | NEW |