trphoenix
2018-11-12 29fbfc5dd1d55d189f23eb6d32f000252f92985f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (c) 2015, 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.
 
import 'dart:convert';
 
import 'package:test/test.dart';
 
final isCastError = const TypeMatcher<CastError>();
final throwsCastError = throwsA(isCastError);
 
T roundTripObject<T>(T object, T factory(Map<String, dynamic> json)) {
  final data = loudEncode(object);
 
  final object2 = factory(json.decode(data) as Map<String, dynamic>);
 
  expect(object2, equals(object));
 
  final json2 = loudEncode(object2);
 
  expect(json2, equals(data));
  return object2;
}
 
/// Prints out nested causes before throwing `JsonUnsupportedObjectError`.
String loudEncode(Object object) {
  try {
    return const JsonEncoder.withIndent(' ').convert(object);
  } on JsonUnsupportedObjectError catch (e) {
    var error = e;
    do {
      final cause = error.cause;
      print(cause);
      error = (cause is JsonUnsupportedObjectError) ? cause : null;
    } while (error != null);
    rethrow;
  }
}