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

Side by Side Diff: tests/lib/convert/json2_test.dart

Issue 223053004: lib/convert: removed unused import, removed redundant test (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: removed status file entry 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
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 json2_tests;
6
7 import "dart:convert";
8
9 import 'package:unittest/unittest.dart';
10
11 // Moved from "tests/json/json_test.dart"
12
13 void main() {
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 expect(JSON.encode('hi\nthere'), equals('"hi\\nthere"'));
89 expect(JSON.encode('hi\r\nthere'), equals('"hi\\r\\nthere"'));
90 expect(JSON.encode(''), equals('""'));
91
92 // Lists.
93 expect(JSON.encode([]), equals('[]'));
94 expect(JSON.encode(new List(0)), equals('[]'));
95 expect(JSON.encode(new List(3)), equals('[null,null,null]'));
96 validateRoundTrip([3, -4.5, null, true, 'hi', false]);
97 expect(JSON.encode([[3], [], [null], ['hi', true]]),
98 equals('[[3],[],[null],["hi",true]]'));
99
100 // Maps.
101 expect(JSON.encode({}), equals('{}'));
102 expect(JSON.encode(new Map()), equals('{}'));
103 expect(JSON.encode({'x':{}}), equals('{"x":{}}'));
104 expect(JSON.encode({'x':{'a':3}}), equals('{"x":{"a":3}}'));
105
106 // Dart does not guarantee an order on the keys
107 // of a map literal, so reparse and compare to the original Map.
108 validateRoundTrip(
109 {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false});
110 validateRoundTrip({"x":3, "y":-4.5, "z":'hi'});
111 validateRoundTrip({' hi bob ':3, '':4.5});
112 validateRoundTrip(
113 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true},
114 'v':null});
115
116 expect(JSON.encode(new ToJson(4)), "4");
117 expect(JSON.encode(new ToJson([4, "a"])), '[4,"a"]');
118 expect(JSON.encode(new ToJson([4, new ToJson({"x":42})])),
119 '[4,{"x":42}]');
120
121 expect(() => JSON.encode([new ToJson(new ToJson(4))]), throws);
122
123 expect(() => JSON.encode([new Object()]), throws);
124 });
125
126 test('stringify throws if argument cannot be converted', () {
127 /**
128 * Checks that we get an exception (rather than silently returning null) if
129 * we try to stringify something that cannot be converted to json.
130 */
131 expect(() => JSON.encode(new TestClass()), throws);
132 });
133
134 test('stringify custom converter', () {
135 List l = [const C(0), const C(1)];
136 expect(JSON.encode(l, toEncodable: (x) => "C(${x.id})"),
137 equals('["C(0)","C(1)"]'));
138 expect(JSON.encode(l), equals('["C/0","C/1"]'));
139 });
140 }
141
142 class C {
143 final int id;
144 const C(this.id);
145 toJson() => "C/$id";
146 }
147
148 class TestClass {
149 int x;
150 String y;
151
152 TestClass() : x = 3, y = 'joe' { }
153 }
154
155 class ToJson {
156 final object;
157 const ToJson(this.object);
158 toJson() => object;
159 }
160
161 /**
162 * Checks that the argument can be converted to a JSON string and
163 * back, and produce something equivalent to the argument.
164 */
165 void validateRoundTrip(expected) {
166 expect(JSON.decode(JSON.encode(expected)), equals(expected));
167 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698