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

Unified Diff: tests/lib/json/json_test.dart

Issue 14289002: Make JSON stringify throw a JsonUnsupportedObjectError on cycles. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/json/json.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/json/json_test.dart
diff --git a/tests/json/json_test.dart b/tests/lib/json/json_test.dart
similarity index 84%
copy from tests/json/json_test.dart
copy to tests/lib/json/json_test.dart
index af68004b36f42698dc8a4261199ff05e5f401e1f..b485cd67a3d30dd47bb89b5b34dc0ba55a27e5bc 100644
--- a/tests/json/json_test.dart
+++ b/tests/lib/json/json_test.dart
@@ -3,14 +3,10 @@
// BSD-style license that can be found in the LICENSE file.
library json_tests;
-import "package:expect/expect.dart";
+import 'package:unittest/unittest.dart';
import 'dart:json' as json;
-import 'dart:html';
-import '../../pkg/unittest/lib/unittest.dart';
-import '../../pkg/unittest/lib/html_config.dart';
main() {
- useHtmlConfiguration();
test('Parse', () {
// Scalars.
expect(json.parse(' 5 '), equals(5));
@@ -118,13 +114,13 @@ main() {
expect(json.stringify(new ToJson([4, new ToJson({"x":42})])),
'[4,{"x":42}]');
- Expect.throws(() {
+ expect(() {
json.stringify([new ToJson(new ToJson(4))]);
- });
+ }, throwsJsonError);
- Expect.throws(() {
+ expect(() {
json.stringify([new Object()]);
- });
+ }, throwsJsonError);
});
@@ -133,9 +129,29 @@ main() {
* Checks that we get an exception (rather than silently returning null) if
* we try to stringify something that cannot be converted to json.
*/
- expect(() => json.stringify(new TestClass()), throws);
+ expect(() => json.stringify(new TestClass()), throwsJsonError);
});
});
+
+ test('stringify throws if toJson throws', () {
+ expect(() => json.stringify(new ToJsoner("bad", throws: true)),
+ throwsJsonError);
+ });
+
+ test('stringify throws if toJson returns non-serializable value', () {
+ expect(() => json.stringify(new ToJsoner(new TestClass())),
+ throwsJsonError);
+ });
+
+ test('stringify throws on cyclic values', () {
+ var a = [];
+ var b = a;
+ for (int i = 0; i < 50; i++) {
+ b = [b];
+ }
+ a.add(b);
+ expect(() => json.stringify(a), throwsJsonError);
+ });
}
class TestClass {
@@ -145,12 +161,25 @@ class TestClass {
TestClass() : x = 3, y = 'joe' { }
}
+class ToJsoner {
+ final Object returnValue;
+ final bool throws;
+ ToJsoner(this.returnValue, {this.throws});
+ Object toJson() {
+ if (throws) throw returnValue;
+ return returnValue;
+ }
+}
+
class ToJson {
final object;
const ToJson(this.object);
toJson() => object;
}
+var throwsJsonError =
+ throwsA(new isInstanceOf<json.JsonUnsupportedObjectError>());
+
/**
* Checks that the argument can be converted to a JSON string and
* back, and produce something equivalent to the argument.
@@ -158,5 +187,3 @@ class ToJson {
validateRoundTrip(expected) {
expect(json.parse(json.stringify(expected)), equals(expected));
}
-
-
« no previous file with comments | « sdk/lib/json/json.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698