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