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

Unified 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: Made JSON.length disappear. 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 side-by-side diff with in-line comments
Download patch
« lib/json/json.dart ('K') | « lib/json/json.dart ('k') | tools/ddbg.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/json/json_test.dart
diff --git a/tests/json/json_test.dart b/tests/json/json_test.dart
index 91965c3878a51ebd27696afce8f818db1f27db3f..649deba157f72457062aff506f9c48255908b32d 100644
--- a/tests/json/json_test.dart
+++ b/tests/json/json_test.dart
@@ -53,7 +53,7 @@ main() {
expect(JSON.parse('{ " hi bob " :3, "": 4.5}'),
equals({ " hi bob " :3, "": 4.5}));
-
+
expect(JSON.parse(' { "x" : { } } '), equals({ 'x' : {}}));
expect(JSON.parse('{"x":{}}'), equals({ 'x' : {}}));
@@ -112,6 +112,20 @@ main() {
validateRoundTrip(
{'x':{'a':3, 'b':-4.5}, 'y':[{}], 'z':'hi', 'w':{'c':null, 'd':true},
'v':null});
+
+ expect(JSON.stringify(new ToJson(4)), "4");
+ expect(JSON.stringify(new ToJson([4, "a"])), '[4,"a"]');
+ expect(JSON.stringify(new ToJson([4, new ToJson({"x":42})])),
+ '[4,{"x":42}]');
+
+ Expect.throws(() {
+ JSON.stringify([new ToJson(new ToJson(4))]);
+ });
+
+ Expect.throws(() {
+ JSON.stringify([new Object()]);
+ });
+
});
test('stringify throws if argument cannot be converted', () {
@@ -131,6 +145,12 @@ class TestClass {
TestClass() : x = 3, y = 'joe' { }
}
+class ToJson {
+ final object;
+ const ToJson(this.object);
+ toJson() => object;
+}
+
/**
* Checks that the argument can be converted to a JSON string and
* back, and produce something equivalent to the argument.
« lib/json/json.dart ('K') | « lib/json/json.dart ('k') | tools/ddbg.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698