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

Unified Diff: test/codegen/lib/convert/json_toEncodable_reviver_test.dart

Issue 1965563003: Update dart:convert and dart:core Uri. (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 7 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
Index: test/codegen/lib/convert/json_toEncodable_reviver_test.dart
diff --git a/test/codegen/lib/convert/json_toEncodable_reviver_test.dart b/test/codegen/lib/convert/json_toEncodable_reviver_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..77ff1d4ac53ff2e3c98ce76b660461cfebabd7ad
--- /dev/null
+++ b/test/codegen/lib/convert/json_toEncodable_reviver_test.dart
@@ -0,0 +1,55 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library json_tests;
+import 'package:expect/expect.dart';
+import 'dart:convert';
+
+class A {
+ final x;
+ A(this.x);
+}
+
+toEncodable(A a) => { "A": a.x };
+reviver(key, value) {
+ if (value is Map && value.length == 1 && value["A"] != null) {
+ return new A(value["A"]);
+ }
+ return value;
+}
+
+const extendedJson =
+ const JsonCodec(toEncodable: toEncodable, reviver: reviver);
+
+main() {
+ var encoded = extendedJson.encode([new A(0), { "2": new A(1) }]);
+ Expect.equals('[{"A":0},{"2":{"A":1}}]', encoded);
+ var decoded = extendedJson.decode(encoded);
+ Expect.isTrue(decoded is List);
+ Expect.equals(2, decoded.length);
+ Expect.isTrue(decoded[0] is A);
+ Expect.equals(0, decoded[0].x);
+ Expect.isTrue(decoded[1] is Map);
+ Expect.isNotNull(decoded[1]["2"]);
+ Expect.isTrue(decoded[1]["2"] is A);
+ Expect.equals(1, decoded[1]["2"].x);
+
+ var a = extendedJson.decode(extendedJson.encode(new A(499)));
+ Expect.isTrue(a is A);
+ Expect.equals(499, a.x);
+
+ testInvalidMap();
+}
+
+
+void testInvalidMap() {
+ var map = {"a" : 42, "b": 42, 37: 42}; // Non-string key.
+ var enc = new JsonEncoder((_) => "fixed");
+ var res = enc.convert(map);
+ Expect.equals('"fixed"', res);
+
+ enc = new JsonEncoder.withIndent(" ", (_) => "fixed");
+ res = enc.convert(map);
+ Expect.equals('"fixed"', res);
+}

Powered by Google App Engine
This is Rietveld 408576698