| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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_tests; | 5 import 'package:expect/expect.dart'; |
| 6 import 'package:unittest/unittest.dart'; | |
| 7 import 'dart:convert'; | 6 import 'dart:convert'; |
| 8 | 7 |
| 9 main() { | 8 main() { |
| 10 test('Parse', () { | 9 testParsing(); |
| 11 // Scalars. | 10 testStringify(); |
| 12 expect(JSON.decode(' 5 '), equals(5)); | 11 testStringifyErrors(); |
| 13 expect(JSON.decode(' -42 '), equals(-42)); | 12 } |
| 14 expect(JSON.decode(' 3e0 '), equals(3)); | |
| 15 expect(JSON.decode(' 3.14 '), equals(3.14)); | |
| 16 expect(JSON.decode('true '), isTrue); | |
| 17 expect(JSON.decode(' false'), isFalse); | |
| 18 expect(JSON.decode(' null '), isNull); | |
| 19 expect(JSON.decode('\n\rnull\t'), isNull); | |
| 20 expect(JSON.decode(' "hi there\\" bob" '), equals('hi there" bob')); | |
| 21 expect(JSON.decode(' "" '), isEmpty); | |
| 22 | 13 |
| 23 // Lists. | 14 void testParsing() { |
| 24 expect(JSON.decode(' [] '), isEmpty); | 15 // Scalars. |
| 25 expect(JSON.decode('[ ]'), isEmpty); | 16 Expect.equals(5, JSON.decode(' 5 ')); |
| 26 expect(JSON.decode(' [3, -4.5, true, "hi", false] '), | 17 Expect.equals(-42, JSON.decode(' -42 ')); |
| 27 equals([3, -4.5, true, 'hi', false])); | 18 Expect.equals(3, JSON.decode(' 3e0 ')); |
| 28 // Nulls are tricky. | 19 Expect.equals(3.14, JSON.decode(' 3.14 ')); |
| 29 expect(JSON.decode('[null]'), orderedEquals([null])); | 20 Expect.isTrue(JSON.decode('true ')); |
| 30 expect(JSON.decode(' [3, -4.5, null, true, "hi", false] '), | 21 Expect.isFalse(JSON.decode(' false')); |
| 31 equals([3, -4.5, null, true, 'hi', false])); | 22 Expect.isNull(JSON.decode(' null ')); |
| 32 expect(JSON.decode('[[null]]'), equals([[null]])); | 23 Expect.isNull(JSON.decode('\n\rnull\t')); |
| 33 expect(JSON.decode(' [ [3], [], [null], ["hi", true]] '), | 24 Expect.equals('hi there" bob', JSON.decode(' "hi there\\" bob" ')); |
| 34 equals([[3], [], [null], ['hi', true]])); | 25 Expect.equals('', JSON.decode(' "" ')); |
| 35 | 26 |
| 36 // Maps. | 27 // Lists. |
| 37 expect(JSON.decode(' {} '), isEmpty); | 28 Expect.deepEquals([], JSON.decode(' [] ')); |
| 38 expect(JSON.decode('{ }'), isEmpty); | 29 Expect.deepEquals([], JSON.decode('[ ]')); |
| 30 Expect.deepEquals([3, -4.5, true, 'hi', false], |
| 31 JSON.decode(' [3, -4.5, true, "hi", false] ')); |
| 32 // Nulls are tricky. |
| 33 Expect.deepEquals([null], JSON.decode('[null]')); |
| 34 Expect.deepEquals([3, -4.5, null, true, 'hi', false], |
| 35 JSON.decode(' [3, -4.5, null, true, "hi", false] ')); |
| 36 Expect.deepEquals([ |
| 37 [null] |
| 38 ], JSON.decode('[[null]]')); |
| 39 Expect.deepEquals([ |
| 40 [3], |
| 41 [], |
| 42 [null], |
| 43 ['hi', true] |
| 44 ], JSON.decode(' [ [3], [], [null], ["hi", true]] ')); |
| 39 | 45 |
| 40 expect(JSON.decode( | 46 // Maps. |
| 41 ' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } '), | 47 Expect.deepEquals({}, JSON.decode(' {} ')); |
| 42 equals({"x":3, "y": -4.5, "z" : "hi", "u" : true, "v": false })); | 48 Expect.deepEquals({}, JSON.decode('{ }')); |
| 43 | 49 |
| 44 expect(JSON.decode(' {"x":3, "y": -4.5, "z" : "hi" } '), | 50 Expect.deepEquals({"x": 3, "y": -4.5, "z": "hi", "u": true, "v": false}, |
| 45 equals({"x":3, "y": -4.5, "z" : "hi" })); | 51 JSON.decode(' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } ')); |
| 46 | 52 |
| 47 expect(JSON.decode(' {"y": -4.5, "z" : "hi" ,"x":3 } '), | 53 Expect.deepEquals({"x": 3, "y": -4.5, "z": "hi"}, |
| 48 equals({"y": -4.5, "z" : "hi" ,"x":3 })); | 54 JSON.decode(' {"x":3, "y": -4.5, "z" : "hi" } ')); |
| 49 | 55 |
| 50 expect(JSON.decode('{ " hi bob " :3, "": 4.5}'), | 56 Expect.deepEquals({"y": -4.5, "z": "hi", "x": 3}, |
| 51 equals({ " hi bob " :3, "": 4.5})); | 57 JSON.decode(' {"y": -4.5, "z" : "hi" ,"x":3 } ')); |
| 52 | 58 |
| 53 expect(JSON.decode(' { "x" : { } } '), equals({ 'x' : {}})); | 59 Expect.deepEquals( |
| 54 expect(JSON.decode('{"x":{}}'), equals({ 'x' : {}})); | 60 {" hi bob ": 3, "": 4.5}, JSON.decode('{ " hi bob " :3, "": 4.5}')); |
| 55 | 61 |
| 56 // Nulls are tricky. | 62 Expect.deepEquals({'x': {}}, JSON.decode(' { "x" : { } } ')); |
| 57 expect(JSON.decode('{"w":null}'), equals({ 'w' : null})); | 63 Expect.deepEquals({'x': {}}, JSON.decode('{"x":{}}')); |
| 58 | 64 |
| 59 expect(JSON.decode('{"x":{"w":null}}'), equals({"x":{"w":null}})); | 65 // Nulls are tricky. |
| 66 Expect.deepEquals({'w': null}, JSON.decode('{"w":null}')); |
| 60 | 67 |
| 61 expect(JSON.decode(' {"x":3, "y": -4.5, "z" : "hi",' | 68 Expect.deepEquals({ |
| 62 '"w":null, "u" : true, "v": false } '), | 69 "x": {"w": null} |
| 63 equals({"x":3, "y": -4.5, "z" : "hi", | 70 }, JSON.decode('{"x":{"w":null}}')); |
| 64 "w":null, "u" : true, "v": false })); | |
| 65 | 71 |
| 66 expect(JSON.decode('{"x": {"a":3, "b": -4.5}, "y":[{}], ' | 72 Expect.deepEquals( |
| 67 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), | 73 {"x": 3, "y": -4.5, "z": "hi", "w": null, "u": true, "v": false}, |
| 68 equals({"x": {"a":3, "b": -4.5}, "y":[{}], | 74 JSON.decode(' {"x":3, "y": -4.5, "z" : "hi",' |
| 69 "z":"hi","w":{"c":null,"d":true}, "v":null})); | 75 '"w":null, "u" : true, "v": false } ')); |
| 76 |
| 77 Expect.deepEquals( |
| 78 { |
| 79 "x": {"a": 3, "b": -4.5}, |
| 80 "y": [{}], |
| 81 "z": "hi", |
| 82 "w": {"c": null, "d": true}, |
| 83 "v": null |
| 84 }, |
| 85 JSON.decode('{"x": {"a":3, "b": -4.5}, "y":[{}], ' |
| 86 '"z":"hi","w":{"c":null,"d":true}, "v":null}')); |
| 87 } |
| 88 |
| 89 void testStringify() { |
| 90 // Scalars. |
| 91 Expect.equals('5', JSON.encode(5)); |
| 92 Expect.equals('-42', JSON.encode(-42)); |
| 93 // Dart does not guarantee a formatting for doubles, |
| 94 // so reparse and compare to the original. |
| 95 validateRoundTrip(3.14); |
| 96 Expect.equals('true', JSON.encode(true)); |
| 97 Expect.equals('false', JSON.encode(false)); |
| 98 Expect.equals('null', JSON.encode(null)); |
| 99 Expect.equals('" hi there\\" bob "', JSON.encode(' hi there" bob ')); |
| 100 Expect.equals('"hi\\\\there"', JSON.encode('hi\\there')); |
| 101 Expect.equals('"hi\\nthere"', JSON.encode('hi\nthere')); |
| 102 Expect.equals('"hi\\r\\nthere"', JSON.encode('hi\r\nthere')); |
| 103 Expect.equals('""', JSON.encode('')); |
| 104 |
| 105 // Lists. |
| 106 Expect.equals('[]', JSON.encode([])); |
| 107 Expect.equals('[]', JSON.encode(new List(0))); |
| 108 Expect.equals('[null,null,null]', JSON.encode(new List(3))); |
| 109 validateRoundTrip([3, -4.5, null, true, 'hi', false]); |
| 110 Expect.equals( |
| 111 '[[3],[],[null],["hi",true]]', |
| 112 JSON.encode([ |
| 113 [3], |
| 114 [], |
| 115 [null], |
| 116 ['hi', true] |
| 117 ])); |
| 118 |
| 119 // Maps. |
| 120 Expect.equals('{}', JSON.encode({})); |
| 121 Expect.equals('{}', JSON.encode(new Map())); |
| 122 Expect.equals('{"x":{}}', JSON.encode({'x': {}})); |
| 123 Expect.equals( |
| 124 '{"x":{"a":3}}', |
| 125 JSON.encode({ |
| 126 'x': {'a': 3} |
| 127 })); |
| 128 |
| 129 // Dart does not guarantee an order on the keys |
| 130 // of a map literal, so reparse and compare to the original Map. |
| 131 validateRoundTrip( |
| 132 {'x': 3, 'y': -4.5, 'z': 'hi', 'w': null, 'u': true, 'v': false}); |
| 133 validateRoundTrip({"x": 3, "y": -4.5, "z": 'hi'}); |
| 134 validateRoundTrip({' hi bob ': 3, '': 4.5}); |
| 135 validateRoundTrip({ |
| 136 'x': {'a': 3, 'b': -4.5}, |
| 137 'y': [{}], |
| 138 'z': 'hi', |
| 139 'w': {'c': null, 'd': true}, |
| 140 'v': null |
| 70 }); | 141 }); |
| 71 | 142 |
| 72 test('stringify', () { | 143 Expect.equals("4", JSON.encode(new ToJson(4))); |
| 73 // Scalars. | 144 Expect.equals('[4,"a"]', JSON.encode(new ToJson([4, "a"]))); |
| 74 expect(JSON.encode(5), equals('5')); | 145 Expect.equals( |
| 75 expect(JSON.encode(-42), equals('-42')); | 146 '[4,{"x":42}]', |
| 76 // Dart does not guarantee a formatting for doubles, | 147 JSON.encode(new ToJson([ |
| 77 // so reparse and compare to the original. | 148 4, |
| 78 validateRoundTrip(3.14); | 149 new ToJson({"x": 42}) |
| 79 expect(JSON.encode(true), equals('true')); | 150 ]))); |
| 80 expect(JSON.encode(false), equals('false')); | |
| 81 expect(JSON.encode(null), equals('null')); | |
| 82 expect(JSON.encode(' hi there" bob '), equals('" hi there\\" bob "')); | |
| 83 expect(JSON.encode('hi\\there'), equals('"hi\\\\there"')); | |
| 84 expect(JSON.encode('hi\nthere'), equals('"hi\\nthere"')); | |
| 85 expect(JSON.encode('hi\r\nthere'), equals('"hi\\r\\nthere"')); | |
| 86 expect(JSON.encode(''), equals('""')); | |
| 87 | 151 |
| 88 // Lists. | 152 expectThrowsJsonError(() => JSON.encode([new ToJson(new ToJson(4))])); |
| 89 expect(JSON.encode([]), equals('[]')); | 153 expectThrowsJsonError(() => JSON.encode([new Object()])); |
| 90 expect(JSON.encode(new List(0)), equals('[]')); | 154 } |
| 91 expect(JSON.encode(new List(3)), equals('[null,null,null]')); | |
| 92 validateRoundTrip([3, -4.5, null, true, 'hi', false]); | |
| 93 expect(JSON.encode([[3], [], [null], ['hi', true]]), | |
| 94 equals('[[3],[],[null],["hi",true]]')); | |
| 95 | 155 |
| 96 // Maps. | 156 void testStringifyErrors() { |
| 97 expect(JSON.encode({}), equals('{}')); | 157 // Throws if argument cannot be converted. |
| 98 expect(JSON.encode(new Map()), equals('{}')); | 158 expectThrowsJsonError(() => JSON.encode(new TestClass())); |
| 99 expect(JSON.encode({'x':{}}), equals('{"x":{}}')); | |
| 100 expect(JSON.encode({'x':{'a':3}}), equals('{"x":{"a":3}}')); | |
| 101 | 159 |
| 102 // Dart does not guarantee an order on the keys | 160 // Throws if toJson throws. |
| 103 // of a map literal, so reparse and compare to the original Map. | 161 expectThrowsJsonError(() => JSON.encode(new ToJsoner("bad", throws: true))); |
| 104 validateRoundTrip( | |
| 105 {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false}); | |
| 106 validateRoundTrip({"x":3, "y":-4.5, "z":'hi'}); | |
| 107 validateRoundTrip({' hi bob ':3, '':4.5}); | |
| 108 validateRoundTrip( | |
| 109 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, | |
| 110 'v':null}); | |
| 111 | 162 |
| 112 expect(JSON.encode(new ToJson(4)), "4"); | 163 // Throws if toJson returns non-serializable value. |
| 113 expect(JSON.encode(new ToJson([4, "a"])), '[4,"a"]'); | 164 expectThrowsJsonError(() => JSON.encode(new ToJsoner(new TestClass()))); |
| 114 expect(JSON.encode(new ToJson([4, new ToJson({"x":42})])), | |
| 115 '[4,{"x":42}]'); | |
| 116 | 165 |
| 117 expect(() { | 166 // Throws on cyclic values. |
| 118 JSON.encode([new ToJson(new ToJson(4))]); | 167 var a = []; |
| 119 }, throwsJsonError); | 168 var b = a; |
| 169 for (int i = 0; i < 50; i++) { |
| 170 b = [b]; |
| 171 } |
| 172 a.add(b); |
| 173 expectThrowsJsonError(() => JSON.encode(a)); |
| 174 } |
| 120 | 175 |
| 121 expect(() { | 176 void expectThrowsJsonError(void f()) { |
| 122 JSON.encode([new Object()]); | 177 Expect.throws(f, (e) => e is JsonUnsupportedObjectError); |
| 123 }, throwsJsonError); | |
| 124 | |
| 125 }); | |
| 126 | |
| 127 test('stringify throws if argument cannot be converted', () { | |
| 128 /** | |
| 129 * Checks that we get an exception (rather than silently returning null) if | |
| 130 * we try to stringify something that cannot be converted to json. | |
| 131 */ | |
| 132 expect(() => JSON.encode(new TestClass()), throwsJsonError); | |
| 133 }); | |
| 134 | |
| 135 test('stringify throws if toJson throws', () { | |
| 136 expect(() => JSON.encode(new ToJsoner("bad", throws: true)), | |
| 137 throwsJsonError); | |
| 138 }); | |
| 139 | |
| 140 test('stringify throws if toJson returns non-serializable value', () { | |
| 141 expect(() => JSON.encode(new ToJsoner(new TestClass())), | |
| 142 throwsJsonError); | |
| 143 }); | |
| 144 | |
| 145 test('stringify throws on cyclic values', () { | |
| 146 var a = []; | |
| 147 var b = a; | |
| 148 for (int i = 0; i < 50; i++) { | |
| 149 b = [b]; | |
| 150 } | |
| 151 a.add(b); | |
| 152 expect(() => JSON.encode(a), throwsJsonError); | |
| 153 }); | |
| 154 } | 178 } |
| 155 | 179 |
| 156 class TestClass { | 180 class TestClass { |
| 157 int x; | 181 int x; |
| 158 String y; | 182 String y; |
| 159 | 183 |
| 160 TestClass() : x = 3, y = 'joe' { } | 184 TestClass() |
| 185 : x = 3, |
| 186 y = 'joe' {} |
| 161 } | 187 } |
| 162 | 188 |
| 163 class ToJsoner { | 189 class ToJsoner { |
| 164 final Object returnValue; | 190 final Object returnValue; |
| 165 final bool throws; | 191 final bool throws; |
| 166 ToJsoner(this.returnValue, {this.throws}); | 192 ToJsoner(this.returnValue, {this.throws}); |
| 167 Object toJson() { | 193 Object toJson() { |
| 168 if (throws) throw returnValue; | 194 if (throws) throw returnValue; |
| 169 return returnValue; | 195 return returnValue; |
| 170 } | 196 } |
| 171 } | 197 } |
| 172 | 198 |
| 173 class ToJson { | 199 class ToJson { |
| 174 final object; | 200 final object; |
| 175 const ToJson(this.object); | 201 const ToJson(this.object); |
| 176 toJson() => object; | 202 toJson() => object; |
| 177 } | 203 } |
| 178 | 204 |
| 179 var throwsJsonError = | |
| 180 throwsA(new isInstanceOf<JsonUnsupportedObjectError>()); | |
| 181 | |
| 182 /** | 205 /** |
| 183 * Checks that the argument can be converted to a JSON string and | 206 * Checks that the argument can be converted to a JSON string and |
| 184 * back, and produce something equivalent to the argument. | 207 * back, and produce something equivalent to the argument. |
| 185 */ | 208 */ |
| 186 validateRoundTrip(expected) { | 209 validateRoundTrip(expected) { |
| 187 expect(JSON.decode(JSON.encode(expected)), equals(expected)); | 210 Expect.deepEquals(expected, JSON.decode(JSON.encode(expected))); |
| 188 } | 211 } |
| OLD | NEW |