Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: tests/lib/json/json_test.dart

Issue 136373002: Fix JSON encoder accepting NaN and Infinity. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/lib/convert/json_util_test.dart ('k') | tests/utils/json_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 'dart:convert';
8
9 main() {
10 test('Parse', () {
11 // Scalars.
12 expect(JSON.decode(' 5 '), equals(5));
13 expect(JSON.decode(' -42 '), equals(-42));
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
23 // Lists.
24 expect(JSON.decode(' [] '), isEmpty);
25 expect(JSON.decode('[ ]'), isEmpty);
26 expect(JSON.decode(' [3, -4.5, true, "hi", false] '),
27 equals([3, -4.5, true, 'hi', false]));
28 // Nulls are tricky.
29 expect(JSON.decode('[null]'), orderedEquals([null]));
30 expect(JSON.decode(' [3, -4.5, null, true, "hi", false] '),
31 equals([3, -4.5, null, true, 'hi', false]));
32 expect(JSON.decode('[[null]]'), equals([[null]]));
33 expect(JSON.decode(' [ [3], [], [null], ["hi", true]] '),
34 equals([[3], [], [null], ['hi', true]]));
35
36 // Maps.
37 expect(JSON.decode(' {} '), isEmpty);
38 expect(JSON.decode('{ }'), isEmpty);
39
40 expect(JSON.decode(
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.decode(' {"x":3, "y": -4.5, "z" : "hi" } '),
45 equals({"x":3, "y": -4.5, "z" : "hi" }));
46
47 expect(JSON.decode(' {"y": -4.5, "z" : "hi" ,"x":3 } '),
48 equals({"y": -4.5, "z" : "hi" ,"x":3 }));
49
50 expect(JSON.decode('{ " hi bob " :3, "": 4.5}'),
51 equals({ " hi bob " :3, "": 4.5}));
52
53 expect(JSON.decode(' { "x" : { } } '), equals({ 'x' : {}}));
54 expect(JSON.decode('{"x":{}}'), equals({ 'x' : {}}));
55
56 // Nulls are tricky.
57 expect(JSON.decode('{"w":null}'), equals({ 'w' : null}));
58
59 expect(JSON.decode('{"x":{"w":null}}'), equals({"x":{"w":null}}));
60
61 expect(JSON.decode(' {"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.decode('{"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.encode(5), equals('5'));
75 expect(JSON.encode(-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.encode(true), equals('true'));
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 // TODO(devoncarew): these tests break the dartium build
85 //expect(JSON.encode('hi\nthere'), equals('"hi\\nthere"'));
86 //expect(JSON.encode('hi\r\nthere'), equals('"hi\\r\\nthere"'));
87 expect(JSON.encode(''), equals('""'));
88
89 // Lists.
90 expect(JSON.encode([]), equals('[]'));
91 expect(JSON.encode(new List(0)), equals('[]'));
92 expect(JSON.encode(new List(3)), equals('[null,null,null]'));
93 validateRoundTrip([3, -4.5, null, true, 'hi', false]);
94 expect(JSON.encode([[3], [], [null], ['hi', true]]),
95 equals('[[3],[],[null],["hi",true]]'));
96
97 // Maps.
98 expect(JSON.encode({}), equals('{}'));
99 expect(JSON.encode(new Map()), equals('{}'));
100 expect(JSON.encode({'x':{}}), equals('{"x":{}}'));
101 expect(JSON.encode({'x':{'a':3}}), equals('{"x":{"a":3}}'));
102
103 // Dart does not guarantee an order on the keys
104 // of a map literal, so reparse and compare to the original Map.
105 validateRoundTrip(
106 {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false});
107 validateRoundTrip({"x":3, "y":-4.5, "z":'hi'});
108 validateRoundTrip({' hi bob ':3, '':4.5});
109 validateRoundTrip(
110 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true},
111 'v':null});
112
113 expect(JSON.encode(new ToJson(4)), "4");
114 expect(JSON.encode(new ToJson([4, "a"])), '[4,"a"]');
115 expect(JSON.encode(new ToJson([4, new ToJson({"x":42})])),
116 '[4,{"x":42}]');
117
118 expect(() {
119 JSON.encode([new ToJson(new ToJson(4))]);
120 }, throwsJsonError);
121
122 expect(() {
123 JSON.encode([new Object()]);
124 }, throwsJsonError);
125
126 });
127
128 test('stringify throws if argument cannot be converted', () {
129 /**
130 * Checks that we get an exception (rather than silently returning null) if
131 * we try to stringify something that cannot be converted to json.
132 */
133 expect(() => JSON.encode(new TestClass()), throwsJsonError);
134 });
135
136 test('stringify throws if toJson throws', () {
137 expect(() => JSON.encode(new ToJsoner("bad", throws: true)),
138 throwsJsonError);
139 });
140
141 test('stringify throws if toJson returns non-serializable value', () {
142 expect(() => JSON.encode(new ToJsoner(new TestClass())),
143 throwsJsonError);
144 });
145
146 test('stringify throws on cyclic values', () {
147 var a = [];
148 var b = a;
149 for (int i = 0; i < 50; i++) {
150 b = [b];
151 }
152 a.add(b);
153 expect(() => JSON.encode(a), throwsJsonError);
154 });
155 }
156
157 class TestClass {
158 int x;
159 String y;
160
161 TestClass() : x = 3, y = 'joe' { }
162 }
163
164 class ToJsoner {
165 final Object returnValue;
166 final bool throws;
167 ToJsoner(this.returnValue, {this.throws});
168 Object toJson() {
169 if (throws) throw returnValue;
170 return returnValue;
171 }
172 }
173
174 class ToJson {
175 final object;
176 const ToJson(this.object);
177 toJson() => object;
178 }
179
180 var throwsJsonError =
181 throwsA(new isInstanceOf<JsonUnsupportedObjectError>());
182
183 /**
184 * Checks that the argument can be converted to a JSON string and
185 * back, and produce something equivalent to the argument.
186 */
187 validateRoundTrip(expected) {
188 expect(JSON.decode(JSON.encode(expected)), equals(expected));
189 }
OLDNEW
« no previous file with comments | « tests/lib/convert/json_util_test.dart ('k') | tests/utils/json_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698