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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014, 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 json_tests;
6 import 'package:expect/expect.dart';
7 import 'dart:convert';
8
9 class A {
10 final x;
11 A(this.x);
12 }
13
14 toEncodable(A a) => { "A": a.x };
15 reviver(key, value) {
16 if (value is Map && value.length == 1 && value["A"] != null) {
17 return new A(value["A"]);
18 }
19 return value;
20 }
21
22 const extendedJson =
23 const JsonCodec(toEncodable: toEncodable, reviver: reviver);
24
25 main() {
26 var encoded = extendedJson.encode([new A(0), { "2": new A(1) }]);
27 Expect.equals('[{"A":0},{"2":{"A":1}}]', encoded);
28 var decoded = extendedJson.decode(encoded);
29 Expect.isTrue(decoded is List);
30 Expect.equals(2, decoded.length);
31 Expect.isTrue(decoded[0] is A);
32 Expect.equals(0, decoded[0].x);
33 Expect.isTrue(decoded[1] is Map);
34 Expect.isNotNull(decoded[1]["2"]);
35 Expect.isTrue(decoded[1]["2"] is A);
36 Expect.equals(1, decoded[1]["2"].x);
37
38 var a = extendedJson.decode(extendedJson.encode(new A(499)));
39 Expect.isTrue(a is A);
40 Expect.equals(499, a.x);
41
42 testInvalidMap();
43 }
44
45
46 void testInvalidMap() {
47 var map = {"a" : 42, "b": 42, 37: 42}; // Non-string key.
48 var enc = new JsonEncoder((_) => "fixed");
49 var res = enc.convert(map);
50 Expect.equals('"fixed"', res);
51
52 enc = new JsonEncoder.withIndent(" ", (_) => "fixed");
53 res = enc.convert(map);
54 Expect.equals('"fixed"', res);
55 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698