-
Notifications
You must be signed in to change notification settings - Fork 65
/
reflected_type_test.dart
184 lines (154 loc) · 7.78 KB
/
reflected_type_test.dart
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) 2015, the Dart Team. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in
// the LICENSE file.
// File used to test reflectable code generation.
// Uses `reflectedType` to access a `Type` value for the type annotation
// of various declarations.
library test_reflectable.test.reflected_type_test;
import 'package:reflectable/reflectable.dart';
import 'package:test/test.dart';
import 'reflected_type_test.reflectable.dart';
// ignore_for_file: omit_local_variable_types
class Reflector extends Reflectable {
const Reflector()
: super(reflectedTypeCapability, invokingCapability,
declarationsCapability);
}
const reflector = Reflector();
@reflector
class A {
int arg0() => 0;
int? arg1(int x) => null;
int arg2to4(A x, int y, [Reflector? z, w = 41.99999999]) => 0;
int? argNamed(int x, y, {num? z}) => null;
int operator +(int x) => 0;
int? operator [](int x) => null;
void operator []=(int x, v) {}
String get getset => '42';
set getset(String string) {}
static int noArguments() => 0;
static int? oneArgument(String x) => null;
static int optionalArguments(A x, double y, [Reflector? z, dynamic w = 42]) =>
0;
// ignore: prefer_adjacent_string_concatenation
static int? namedArguments(String x, List y, {String z = '4' + '2'}) => null;
static List<String> get staticGetset => ['42'];
static set staticGetset(List<String> list) {}
}
final throwsNoCapability = throwsA(const TypeMatcher<NoSuchCapabilityError>());
void main() {
initializeReflectable();
var aMirror = reflector.reflectType(A) as ClassMirror;
Map<String, DeclarationMirror> declarations = aMirror.declarations;
var arg0Mirror = declarations['arg0'] as MethodMirror;
var arg1Mirror = declarations['arg1'] as MethodMirror;
var arg2to4Mirror = declarations['arg2to4'] as MethodMirror;
var argNamedMirror = declarations['argNamed'] as MethodMirror;
var opPlusMirror = declarations['+'] as MethodMirror;
var opBracketMirror = declarations['[]'] as MethodMirror;
var opBracketEqualsMirror = declarations['[]='] as MethodMirror;
var getsetMirror = declarations['getset'] as MethodMirror;
var getsetEqualsMirror = declarations['getset='] as MethodMirror;
var noArgumentsMirror = declarations['noArguments'] as MethodMirror;
var oneArgumentMirror = declarations['oneArgument'] as MethodMirror;
var optionalArgumentsMirror =
declarations['optionalArguments'] as MethodMirror;
var namedArgumentsMirror = declarations['namedArguments'] as MethodMirror;
var staticGetsetMirror = declarations['staticGetset'] as MethodMirror;
var staticGetsetEqualsMirror = declarations['staticGetset='] as MethodMirror;
test('parameter reflected types, instance methods', () {
expect(arg0Mirror.parameters.length, 0);
expect(arg1Mirror.parameters.length, 1);
expect(arg1Mirror.parameters[0].reflectedType, int);
expect(arg2to4Mirror.parameters.length, 4);
expect(arg2to4Mirror.parameters[0].reflectedType, A);
expect(arg2to4Mirror.parameters[1].reflectedType, int);
expect(arg2to4Mirror.parameters[2].reflectedType, Reflector);
expect(arg2to4Mirror.parameters[3].reflectedType, dynamic);
expect(argNamedMirror.parameters.length, 3);
expect(argNamedMirror.parameters[0].reflectedType, int);
expect(argNamedMirror.parameters[1].reflectedType, dynamic);
expect(argNamedMirror.parameters[2].reflectedType, num);
});
test('parameter reflected types, operators', () {
expect(opPlusMirror.parameters.length, 1);
expect(opPlusMirror.parameters[0].reflectedType, int);
expect(opBracketMirror.parameters.length, 1);
expect(opBracketMirror.parameters[0].reflectedType, int);
expect(opBracketEqualsMirror.parameters.length, 2);
var opBracketEqualsParameter0 = opBracketEqualsMirror.parameters[0];
var opBracketEqualsParameter1 = opBracketEqualsMirror.parameters[1];
expect(opBracketEqualsParameter0.reflectedType, int);
expect(opBracketEqualsParameter1.reflectedType, dynamic);
});
test('parameter reflected types, getters and setters', () {
expect(getsetMirror.parameters.length, 0);
expect(getsetEqualsMirror.parameters.length, 1);
var getsetEqualsParameter0 = getsetEqualsMirror.parameters[0];
expect(getsetEqualsParameter0.reflectedType, String);
});
test('parameter reflected types, static methods', () {
expect(noArgumentsMirror.parameters.length, 0);
expect(oneArgumentMirror.parameters.length, 1);
expect(oneArgumentMirror.parameters[0].reflectedType, String);
expect(optionalArgumentsMirror.parameters.length, 4);
var optionalArgumentsParameter0 = optionalArgumentsMirror.parameters[0];
var optionalArgumentsParameter1 = optionalArgumentsMirror.parameters[1];
var optionalArgumentsParameter2 = optionalArgumentsMirror.parameters[2];
var optionalArgumentsParameter3 = optionalArgumentsMirror.parameters[3];
expect(optionalArgumentsParameter0.reflectedType, A);
expect(optionalArgumentsParameter1.reflectedType, double);
expect(optionalArgumentsParameter2.reflectedType, Reflector);
expect(optionalArgumentsParameter3.reflectedType, dynamic);
expect(namedArgumentsMirror.parameters.length, 3);
var namedArgumentsParameter0 = namedArgumentsMirror.parameters[0];
var namedArgumentsParameter1 = namedArgumentsMirror.parameters[1];
var namedArgumentsParameter2 = namedArgumentsMirror.parameters[2];
expect(namedArgumentsParameter0.reflectedType, String);
expect(namedArgumentsParameter1.reflectedType, List);
expect(namedArgumentsParameter2.reflectedType, String);
});
test('parameter reflected types, static getters and setters', () {
expect(staticGetsetMirror.parameters.length, 0);
expect(staticGetsetEqualsMirror.parameters.length, 1);
var staticGetsetEqualsParameter0 = staticGetsetEqualsMirror.parameters[0];
expect(staticGetsetEqualsParameter0.reflectedType,
const TypeValue<List<String>>().type);
});
test('reflected return types, methods', () {
expect(arg0Mirror.hasReflectedReturnType, true);
expect(arg0Mirror.reflectedReturnType, int);
expect(arg1Mirror.hasReflectedReturnType, true);
expect(arg1Mirror.reflectedReturnType, int);
expect(arg2to4Mirror.hasReflectedReturnType, true);
expect(arg2to4Mirror.reflectedReturnType, int);
expect(argNamedMirror.hasReflectedReturnType, true);
expect(argNamedMirror.reflectedReturnType, int);
expect(opPlusMirror.hasReflectedReturnType, true);
expect(opPlusMirror.reflectedReturnType, int);
expect(opBracketMirror.hasReflectedReturnType, true);
expect(opBracketMirror.reflectedReturnType, int);
expect(opBracketEqualsMirror.hasReflectedReturnType, true);
expect(opBracketEqualsMirror.reflectedReturnType,
const TypeValue<void>().type);
expect(getsetMirror.hasReflectedReturnType, true);
expect(getsetMirror.reflectedReturnType, String);
expect(getsetEqualsMirror.hasReflectedReturnType, true);
expect(
getsetEqualsMirror.reflectedReturnType, const TypeValue<void>().type);
expect(noArgumentsMirror.hasReflectedReturnType, true);
expect(noArgumentsMirror.reflectedReturnType, int);
expect(oneArgumentMirror.hasReflectedReturnType, true);
expect(oneArgumentMirror.reflectedReturnType, int);
expect(optionalArgumentsMirror.hasReflectedReturnType, true);
expect(optionalArgumentsMirror.reflectedReturnType, int);
expect(namedArgumentsMirror.hasReflectedReturnType, true);
expect(namedArgumentsMirror.reflectedReturnType, int);
expect(staticGetsetMirror.hasReflectedReturnType, true);
expect(staticGetsetMirror.reflectedReturnType,
const TypeValue<List<String>>().type);
expect(staticGetsetEqualsMirror.hasReflectedReturnType, true);
expect(staticGetsetEqualsMirror.reflectedReturnType,
const TypeValue<void>().type);
});
}