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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2018, 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';
 
import '../test_utils.dart';
import 'generic_class.dart';
 
void main() {
  group('generic', () {
    GenericClass<T, S> roundTripGenericClass<T extends num, S>(
        GenericClass<T, S> p) {
      final outputJson = loudEncode(p);
      final p2 = GenericClass<T, S>.fromJson(
          jsonDecode(outputJson) as Map<String, dynamic>);
      final outputJson2 = loudEncode(p2);
      expect(outputJson2, outputJson);
      return p2;
    }
 
    test('no type args', () {
      roundTripGenericClass(GenericClass()
        ..fieldDynamic = 1
        ..fieldInt = 2
        ..fieldObject = 3
        ..fieldT = 5
        ..fieldS = 'six');
    });
    test('with type arguments', () {
      roundTripGenericClass(GenericClass<double, String>()
        ..fieldDynamic = 1
        ..fieldInt = 2
        ..fieldObject = 3
        ..fieldT = 5.0
        ..fieldS = 'six');
    });
    test('with bad arguments', () {
      expect(
          () => GenericClass<double, String>()
            ..fieldT = (true as dynamic) as double,
          throwsCastError);
    });
    test('with bad arguments', () {
      expect(
          () =>
              GenericClass<double, String>()..fieldS = (5 as dynamic) as String,
          throwsCastError);
    });
  });
}