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

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

Issue 212813006: Fix JSON tests, re-enable in all cases - 2nd try (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 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/json/json.status ('k') | tests/lib/convert/json2_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:expect/expect.dart";
7 import "dart:convert";
8 import 'dart:html';
9 import '../../pkg/unittest/lib/unittest.dart';
10 import '../../pkg/unittest/lib/html_config.dart';
11
12 main() {
13 useHtmlConfiguration();
14 test('Parse', () {
15 // Scalars.
16 expect(JSON.decode(' 5 '), equals(5));
17 expect(JSON.decode(' -42 '), equals(-42));
18 expect(JSON.decode(' 3e0 '), equals(3));
19 expect(JSON.decode(' 3.14 '), equals(3.14));
20 expect(JSON.decode('true '), isTrue);
21 expect(JSON.decode(' false'), isFalse);
22 expect(JSON.decode(' null '), isNull);
23 expect(JSON.decode('\n\rnull\t'), isNull);
24 expect(JSON.decode(' "hi there\\" bob" '), equals('hi there" bob'));
25 expect(JSON.decode(' "" '), isEmpty);
26
27 // Lists.
28 expect(JSON.decode(' [] '), isEmpty);
29 expect(JSON.decode('[ ]'), isEmpty);
30 expect(JSON.decode(' [3, -4.5, true, "hi", false] '),
31 equals([3, -4.5, true, 'hi', false]));
32 // Nulls are tricky.
33 expect(JSON.decode('[null]'), orderedEquals([null]));
34 expect(JSON.decode(' [3, -4.5, null, true, "hi", false] '),
35 equals([3, -4.5, null, true, 'hi', false]));
36 expect(JSON.decode('[[null]]'), equals([[null]]));
37 expect(JSON.decode(' [ [3], [], [null], ["hi", true]] '),
38 equals([[3], [], [null], ['hi', true]]));
39
40 // Maps.
41 expect(JSON.decode(' {} '), isEmpty);
42 expect(JSON.decode('{ }'), isEmpty);
43
44 expect(JSON.decode(
45 ' {"x":3, "y": -4.5, "z" : "hi","u" : true, "v": false } '),
46 equals({"x":3, "y": -4.5, "z" : "hi", "u" : true, "v": false }));
47
48 expect(JSON.decode(' {"x":3, "y": -4.5, "z" : "hi" } '),
49 equals({"x":3, "y": -4.5, "z" : "hi" }));
50
51 expect(JSON.decode(' {"y": -4.5, "z" : "hi" ,"x":3 } '),
52 equals({"y": -4.5, "z" : "hi" ,"x":3 }));
53
54 expect(JSON.decode('{ " hi bob " :3, "": 4.5}'),
55 equals({ " hi bob " :3, "": 4.5}));
56
57 expect(JSON.decode(' { "x" : { } } '), equals({ 'x' : {}}));
58 expect(JSON.decode('{"x":{}}'), equals({ 'x' : {}}));
59
60 // Nulls are tricky.
61 expect(JSON.decode('{"w":null}'), equals({ 'w' : null}));
62
63 expect(JSON.decode('{"x":{"w":null}}'), equals({"x":{"w":null}}));
64
65 expect(JSON.decode(' {"x":3, "y": -4.5, "z" : "hi",'
66 '"w":null, "u" : true, "v": false } '),
67 equals({"x":3, "y": -4.5, "z" : "hi",
68 "w":null, "u" : true, "v": false }));
69
70 expect(JSON.decode('{"x": {"a":3, "b": -4.5}, "y":[{}], '
71 '"z":"hi","w":{"c":null,"d":true}, "v":null}'),
72 equals({"x": {"a":3, "b": -4.5}, "y":[{}],
73 "z":"hi","w":{"c":null,"d":true}, "v":null}));
74 });
75
76 test('stringify', () {
77 // Scalars.
78 expect(JSON.encode(5), equals('5'));
79 expect(JSON.encode(-42), equals('-42'));
80 // Dart does not guarantee a formatting for doubles,
81 // so reparse and compare to the original.
82 validateRoundTrip(3.14);
83 expect(JSON.encode(true), equals('true'));
84 expect(JSON.encode(false), equals('false'));
85 expect(JSON.encode(null), equals('null'));
86 expect(JSON.encode(' hi there" bob '), equals('" hi there\\" bob "'));
87 expect(JSON.encode('hi\\there'), equals('"hi\\\\there"'));
88 // TODO(devoncarew): these tests break the dartium build
89 //expect(JSON.encode('hi\nthere'), equals('"hi\\nthere"'));
90 //expect(JSON.encode('hi\r\nthere'), equals('"hi\\r\\nthere"'));
91 expect(JSON.encode(''), equals('""'));
92
93 // Lists.
94 expect(JSON.encode([]), equals('[]'));
95 expect(JSON.encode(new List(0)), equals('[]'));
96 expect(JSON.encode(new List(3)), equals('[null,null,null]'));
97 validateRoundTrip([3, -4.5, null, true, 'hi', false]);
98 expect(JSON.encode([[3], [], [null], ['hi', true]]),
99 equals('[[3],[],[null],["hi",true]]'));
100
101 // Maps.
102 expect(JSON.encode({}), equals('{}'));
103 expect(JSON.encode(new Map()), equals('{}'));
104 expect(JSON.encode({'x':{}}), equals('{"x":{}}'));
105 expect(JSON.encode({'x':{'a':3}}), equals('{"x":{"a":3}}'));
106
107 // Dart does not guarantee an order on the keys
108 // of a map literal, so reparse and compare to the original Map.
109 validateRoundTrip(
110 {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false});
111 validateRoundTrip({"x":3, "y":-4.5, "z":'hi'});
112 validateRoundTrip({' hi bob ':3, '':4.5});
113 validateRoundTrip(
114 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true},
115 'v':null});
116
117 expect(JSON.encode(new ToJson(4)), "4");
118 expect(JSON.encode(new ToJson([4, "a"])), '[4,"a"]');
119 expect(JSON.encode(new ToJson([4, new ToJson({"x":42})])),
120 '[4,{"x":42}]');
121
122 Expect.throws(() {
123 JSON.encode([new ToJson(new ToJson(4))]);
124 });
125
126 Expect.throws(() {
127 JSON.encode([new Object()]);
128 });
129
130 });
131
132 test('stringify throws if argument cannot be converted', () {
133 /**
134 * Checks that we get an exception (rather than silently returning null) if
135 * we try to stringify something that cannot be converted to json.
136 */
137 expect(() => JSON.encode(new TestClass()), throws);
138 });
139
140 test('stringify custom converter', () {
141 List l = [const C(0), const C(1)];
142 expect(JSON.encode(l, toEncodable: (x) => "C(${x.id})"),
143 equals('["C(0)","C(1)"]'));
144 expect(JSON.encode(l), equals('["C/0","C/1"]'));
145 });
146 }
147
148 class C {
149 final int id;
150 const C(this.id);
151 toJson() => "C/$id";
152 }
153
154 class TestClass {
155 int x;
156 String y;
157
158 TestClass() : x = 3, y = 'joe' { }
159 }
160
161 class ToJson {
162 final object;
163 const ToJson(this.object);
164 toJson() => object;
165 }
166
167 /**
168 * Checks that the argument can be converted to a JSON string and
169 * back, and produce something equivalent to the argument.
170 */
171 validateRoundTrip(expected) {
172 expect(JSON.decode(JSON.encode(expected)), equals(expected));
173 }
OLDNEW
« no previous file with comments | « tests/json/json.status ('k') | tests/lib/convert/json2_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698