|
1 |
| -// ignore_for_file: unused_local_variable, strict_raw_type |
| 1 | +// ignore_for_file: unused_local_variable |
2 | 2 | import 'package:test/test.dart';
|
3 | 3 |
|
4 | 4 | import 'package:type_system_examples/animal.dart';
|
5 |
| -import 'package:type_system_examples/bounded/my_collection.dart'; |
6 | 5 |
|
7 | 6 | Matcher _throwsA<T>(String msg) => throwsA(
|
8 | 7 | allOf(TypeMatcher<T>(), predicate((e) => e.toString().contains(msg))),
|
@@ -36,92 +35,5 @@ void main() {
|
36 | 35 | const msg = "type 'List<Dog>' is not a subtype of type 'List<Cat>'";
|
37 | 36 | expect(main, _throwsA<TypeError>(msg));
|
38 | 37 | });
|
39 |
| - |
40 |
| - test('downcast check fails', () { |
41 |
| - void downcastCheck() { |
42 |
| - // #docregion fail-downcast-check |
43 |
| - assumeStrings(<int>[1, 2, 3]); |
44 |
| - // #enddocregion fail-downcast-check |
45 |
| - } |
46 |
| - |
47 |
| - // #docregion downcast-check-msg |
48 |
| - const msg = "type 'List<int>' is not a subtype of type 'List<String>'"; |
49 |
| - // #enddocregion downcast-check-msg |
50 |
| - expect(downcastCheck, _throwsA<TypeError>(msg)); |
51 |
| - }); |
52 |
| - |
53 |
| - final expectedOutput = 'a string\n'; |
54 |
| - |
55 |
| - test('downcast check ok for <String>[]', () { |
56 |
| - void downcastCheck() { |
57 |
| - // #docregion typed-list-lit |
58 |
| - var list = <String>[]; |
59 |
| - list.add('a string'); |
60 |
| - list.add('another'); |
61 |
| - assumeStrings(list); |
62 |
| - // #enddocregion typed-list-lit |
63 |
| - } |
64 |
| - |
65 |
| - expect(downcastCheck, prints(expectedOutput)); |
66 |
| - }); |
67 |
| - |
68 |
| - test('downcast check ok for List<String>', () { |
69 |
| - void downcastCheck() { |
70 |
| - // #docregion typed-list |
71 |
| - List<String> list = []; |
72 |
| - list.add('a string'); |
73 |
| - list.add('another'); |
74 |
| - assumeStrings(list); |
75 |
| - // #enddocregion typed-list |
76 |
| - } |
77 |
| - |
78 |
| - expect(downcastCheck, prints(expectedOutput)); |
79 |
| - }); |
80 |
| - |
81 |
| - Map<String, dynamic> fetchFromExternalSource() => { |
82 |
| - 'names': ['a string'], |
83 |
| - }; |
84 |
| - |
85 |
| - test('downcast check ok: use cast()', () { |
86 |
| - void downcastCheck() { |
87 |
| - // #docregion cast |
88 |
| - Map<String, dynamic> json = fetchFromExternalSource(); |
89 |
| - var names = json['names'] as List; |
90 |
| - assumeStrings(names.cast<String>()); |
91 |
| - // #enddocregion cast |
92 |
| - } |
93 |
| - |
94 |
| - expect(downcastCheck, prints(expectedOutput)); |
95 |
| - }); |
96 |
| - |
97 |
| - test('instantiate-to-bound sanity', () { |
98 |
| - final b = B(); |
99 |
| - expect(b.typeOfS, 'int'); |
100 |
| - expect(b.typeOfT, 'dynamic'); |
101 |
| - }); |
102 |
| - |
103 |
| - test('instantiate-to-bound fix: add type arg', () { |
104 |
| - // #docregion add-type-arg |
105 |
| - var c = C<List>([]).collection; |
106 |
| - c.add(2); |
107 |
| - // #enddocregion add-type-arg |
108 |
| - expect(c, [2]); |
109 |
| - }); |
110 | 38 | });
|
111 | 39 | }
|
112 |
| - |
113 |
| -// #docregion downcast-check |
114 |
| -void assumeStrings(dynamic objects) { |
115 |
| - // ignore: stable, beta, dev, invalid_assignment |
116 |
| - List<String> strings = objects; // Runtime downcast check |
117 |
| - String string = strings[0]; // Expect a String value |
118 |
| - // #enddocregion downcast-check |
119 |
| - print(string); |
120 |
| - // #docregion downcast-check |
121 |
| -} |
122 |
| -// #enddocregion downcast-check |
123 |
| - |
124 |
| -class B<S extends int, T> { |
125 |
| - String get typeOfS => '$S'; |
126 |
| - String get typeOfT => '$T'; |
127 |
| -} |
0 commit comments