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

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

Issue 10914009: Make JSON.stringify call toJson() on objects that it can't serialize. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 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
« lib/json/json.dart ('K') | « lib/json/json.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #library('json_tests');
6 6
7 #import('dart:json'); 7 #import('dart:json');
8 #import('dart:html'); 8 #import('dart:html');
9 #import('../../pkg/unittest/unittest.dart'); 9 #import('../../pkg/unittest/unittest.dart');
10 #import('../../pkg/unittest/html_config.dart'); 10 #import('../../pkg/unittest/html_config.dart');
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 equals({"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 47
48 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi" } '), 48 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi" } '),
49 equals({"x":3, "y": -4.5, "z" : "hi" })); 49 equals({"x":3, "y": -4.5, "z" : "hi" }));
50 50
51 expect(JSON.parse(' {"y": -4.5, "z" : "hi" ,"x":3 } '), 51 expect(JSON.parse(' {"y": -4.5, "z" : "hi" ,"x":3 } '),
52 equals({"y": -4.5, "z" : "hi" ,"x":3 })); 52 equals({"y": -4.5, "z" : "hi" ,"x":3 }));
53 53
54 expect(JSON.parse('{ " hi bob " :3, "": 4.5}'), 54 expect(JSON.parse('{ " hi bob " :3, "": 4.5}'),
55 equals({ " hi bob " :3, "": 4.5})); 55 equals({ " hi bob " :3, "": 4.5}));
56 56
57 expect(JSON.parse(' { "x" : { } } '), equals({ 'x' : {}})); 57 expect(JSON.parse(' { "x" : { } } '), equals({ 'x' : {}}));
58 expect(JSON.parse('{"x":{}}'), equals({ 'x' : {}})); 58 expect(JSON.parse('{"x":{}}'), equals({ 'x' : {}}));
59 59
60 // Nulls are tricky. 60 // Nulls are tricky.
61 expect(JSON.parse('{"w":null}'), equals({ 'w' : null})); 61 expect(JSON.parse('{"w":null}'), equals({ 'w' : null}));
62 62
63 expect(JSON.parse('{"x":{"w":null}}'), equals({"x":{"w":null}})); 63 expect(JSON.parse('{"x":{"w":null}}'), equals({"x":{"w":null}}));
64 64
65 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi",' 65 expect(JSON.parse(' {"x":3, "y": -4.5, "z" : "hi",'
66 '"w":null, "u" : true, "v": false } '), 66 '"w":null, "u" : true, "v": false } '),
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 105
106 // Dart does not guarantee an order on the keys 106 // Dart does not guarantee an order on the keys
107 // of a map literal, so reparse and compare to the original Map. 107 // of a map literal, so reparse and compare to the original Map.
108 validateRoundTrip( 108 validateRoundTrip(
109 {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false}); 109 {'x':3, 'y':-4.5, 'z':'hi', 'w':null, 'u':true, 'v':false});
110 validateRoundTrip({"x":3, "y":-4.5, "z":'hi'}); 110 validateRoundTrip({"x":3, "y":-4.5, "z":'hi'});
111 validateRoundTrip({' hi bob ':3, '':4.5}); 111 validateRoundTrip({' hi bob ':3, '':4.5});
112 validateRoundTrip( 112 validateRoundTrip(
113 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true}, 113 {'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true},
114 'v':null}); 114 'v':null});
115
116 expect(JSON.stringify(new ToJson(4)), "4");
117 expect(JSON.stringify(new ToJson([4, "a"])), '[4,"a"]');
118 expect(JSON.stringify(new ToJson([4, new ToJson({"x":42})])),
119 '[4,{"x":42}]');
120
121 Expect.throws(() {
122 JSON.stringify([new ToJson(new ToJson(4))]);
123 });
124
125 Expect.throws(() {
126 JSON.stringify([new Object()]);
127 });
128
115 }); 129 });
116 130
117 test('stringify throws if argument cannot be converted', () { 131 test('stringify throws if argument cannot be converted', () {
118 /** 132 /**
119 * Checks that we get an exception (rather than silently returning null) if 133 * Checks that we get an exception (rather than silently returning null) if
120 * we try to stringify something that cannot be converted to json. 134 * we try to stringify something that cannot be converted to json.
121 */ 135 */
122 expect(() => JSON.stringify(new TestClass()), throws); 136 expect(() => JSON.stringify(new TestClass()), throws);
123 }); 137 });
124 }); 138 });
125 } 139 }
126 140
127 class TestClass { 141 class TestClass {
128 int x; 142 int x;
129 String y; 143 String y;
130 144
131 TestClass() : x = 3, y = 'joe' { } 145 TestClass() : x = 3, y = 'joe' { }
132 } 146 }
133 147
148 class ToJson {
149 final object;
150 const ToJson(this.object);
151 toJson() => object;
152 }
153
134 /** 154 /**
135 * Checks that the argument can be converted to a JSON string and 155 * Checks that the argument can be converted to a JSON string and
136 * back, and produce something equivalent to the argument. 156 * back, and produce something equivalent to the argument.
137 */ 157 */
138 validateRoundTrip(expected) { 158 validateRoundTrip(expected) {
139 expect(JSON.parse(JSON.stringify(expected)), equals(expected)); 159 expect(JSON.parse(JSON.stringify(expected)), equals(expected));
140 } 160 }
141 161
142 162
OLDNEW
« lib/json/json.dart ('K') | « lib/json/json.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698