@@ -23,6 +23,7 @@ import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
23
23
import 'package:analyzer/src/generated/resolver.dart' show ResolverErrorCode;
24
24
import 'package:analyzer/src/generated/sdk.dart' ;
25
25
import 'package:analyzer/src/generated/source.dart' ;
26
+ import 'package:analyzer/src/generated/utilities_dart.dart' ;
26
27
import 'package:analyzer/src/summary/idl.dart' ;
27
28
import 'package:analyzer/src/summary/package_bundle_reader.dart' ;
28
29
import 'package:front_end/byte_store.dart' ;
@@ -66,10 +67,11 @@ class AnalysisDriverResolutionTest extends BaseAnalysisDriverTest {
66
67
test_local_function () async {
67
68
addTestFile (r'''
68
69
void main() {
69
- double f() {}
70
- var v = f();
70
+ double f(int a, String b ) {}
71
+ var v = f(1, '2' );
71
72
}
72
73
''' );
74
+ String fTypeString = '(int, String) → double' ;
73
75
74
76
AnalysisResult result = await driver.getResult (testFile);
75
77
List <Statement > mainStatements = _getMainStatements (result);
@@ -82,7 +84,7 @@ void main() {
82
84
FunctionExpression fExpression = fNode.functionExpression;
83
85
FunctionElement fElement = fNode.element;
84
86
expect (fElement, isNotNull);
85
- expect (fElement.type.toString (), '() → double' );
87
+ expect (fElement.type.toString (), fTypeString );
86
88
87
89
expect (fNode.name.staticElement, same (fElement));
88
90
expect (fNode.name.staticType, fElement.type);
@@ -93,15 +95,83 @@ void main() {
93
95
94
96
expect (fExpression.element, same (fElement));
95
97
98
+ {
99
+ var parameters = fElement.parameters;
100
+ expect (parameters, hasLength (2 ));
101
+
102
+ expect (parameters[0 ].name, 'a' );
103
+ expect (parameters[0 ].nameOffset, 29 );
104
+ expect (parameters[0 ].parameterKind, ParameterKind .REQUIRED );
105
+ expect (parameters[0 ].type, typeProvider.intType);
106
+
107
+ expect (parameters[1 ].name, 'b' );
108
+ expect (parameters[1 ].nameOffset, 39 );
109
+ expect (parameters[1 ].parameterKind, ParameterKind .REQUIRED );
110
+ expect (parameters[1 ].type, typeProvider.stringType);
111
+ }
112
+
96
113
VariableDeclarationStatement vStatement = mainStatements[1 ];
97
114
VariableDeclaration vDeclaration = vStatement.variables.variables[0 ];
98
115
expect (vDeclaration.element.type, same (doubleType));
99
116
100
117
MethodInvocation fInvocation = vDeclaration.initializer;
101
118
expect (fInvocation.methodName.staticElement, same (fElement));
102
- expect (fInvocation.methodName.staticType.toString (), '() → double' );
119
+ expect (fInvocation.methodName.staticType.toString (), fTypeString );
103
120
expect (fInvocation.staticType, same (doubleType));
104
- expect (fInvocation.staticInvokeType.toString (), '() → double' );
121
+ expect (fInvocation.staticInvokeType.toString (), fTypeString);
122
+ }
123
+
124
+ test_local_function_namedParameters () async {
125
+ addTestFile (r'''
126
+ void main() {
127
+ double f(int a, {String b, bool c}) {}
128
+ }
129
+ ''' );
130
+ String fTypeString = '(int, {b: String, c: bool}) → double' ;
131
+
132
+ AnalysisResult result = await driver.getResult (testFile);
133
+ List <Statement > mainStatements = _getMainStatements (result);
134
+
135
+ var typeProvider = result.unit.element.context.typeProvider;
136
+ InterfaceType doubleType = typeProvider.doubleType;
137
+
138
+ FunctionDeclarationStatement fStatement = mainStatements[0 ];
139
+ FunctionDeclaration fNode = fStatement.functionDeclaration;
140
+ FunctionExpression fExpression = fNode.functionExpression;
141
+ FunctionElement fElement = fNode.element;
142
+ expect (fElement, isNotNull);
143
+ expect (fElement.type.toString (), fTypeString);
144
+
145
+ expect (fNode.name.staticElement, same (fElement));
146
+ expect (fNode.name.staticType, fElement.type);
147
+
148
+ TypeName fReturnTypeNode = fNode.returnType;
149
+ expect (fReturnTypeNode.name.staticElement, same (doubleType.element));
150
+ expect (fReturnTypeNode.type, doubleType);
151
+
152
+ expect (fExpression.element, same (fElement));
153
+
154
+ {
155
+ var parameters = fElement.parameters;
156
+ expect (parameters, hasLength (3 ));
157
+
158
+ expect (parameters[0 ].name, 'a' );
159
+ expect (parameters[0 ].nameOffset, 29 );
160
+ expect (parameters[0 ].parameterKind, ParameterKind .REQUIRED );
161
+ expect (parameters[0 ].type, typeProvider.intType);
162
+
163
+ expect (parameters[1 ].name, 'b' );
164
+ expect (parameters[1 ].nameOffset, 40 );
165
+ expect (parameters[1 ].parameterKind, ParameterKind .NAMED );
166
+ expect (parameters[1 ].type, typeProvider.stringType);
167
+
168
+ expect (parameters[2 ].name, 'c' );
169
+ expect (parameters[2 ].nameOffset, 48 );
170
+ expect (parameters[2 ].parameterKind, ParameterKind .NAMED );
171
+ expect (parameters[2 ].type, typeProvider.boolType);
172
+ }
173
+
174
+ // TODO(scheglov) Add invocation test when we can resolve named parameters.
105
175
}
106
176
107
177
test_local_function_noReturnType () async {
@@ -129,6 +199,68 @@ void main() {
129
199
expect (fExpression.element, same (fElement));
130
200
}
131
201
202
+ test_local_function_optionalParameters () async {
203
+ addTestFile (r'''
204
+ void main() {
205
+ double f(int a, [String b, bool c]) {}
206
+ var v = f(1, '2', true);
207
+ }
208
+ ''' );
209
+ String fTypeString = '(int, [String, bool]) → double' ;
210
+
211
+ AnalysisResult result = await driver.getResult (testFile);
212
+ List <Statement > mainStatements = _getMainStatements (result);
213
+
214
+ var typeProvider = result.unit.element.context.typeProvider;
215
+ InterfaceType doubleType = typeProvider.doubleType;
216
+
217
+ FunctionDeclarationStatement fStatement = mainStatements[0 ];
218
+ FunctionDeclaration fNode = fStatement.functionDeclaration;
219
+ FunctionExpression fExpression = fNode.functionExpression;
220
+ FunctionElement fElement = fNode.element;
221
+ expect (fElement, isNotNull);
222
+ expect (fElement.type.toString (), fTypeString);
223
+
224
+ expect (fNode.name.staticElement, same (fElement));
225
+ expect (fNode.name.staticType, fElement.type);
226
+
227
+ TypeName fReturnTypeNode = fNode.returnType;
228
+ expect (fReturnTypeNode.name.staticElement, same (doubleType.element));
229
+ expect (fReturnTypeNode.type, doubleType);
230
+
231
+ expect (fExpression.element, same (fElement));
232
+
233
+ {
234
+ var parameters = fElement.parameters;
235
+ expect (parameters, hasLength (3 ));
236
+
237
+ expect (parameters[0 ].name, 'a' );
238
+ expect (parameters[0 ].nameOffset, 29 );
239
+ expect (parameters[0 ].parameterKind, ParameterKind .REQUIRED );
240
+ expect (parameters[0 ].type, typeProvider.intType);
241
+
242
+ expect (parameters[1 ].name, 'b' );
243
+ expect (parameters[1 ].nameOffset, 40 );
244
+ expect (parameters[1 ].parameterKind, ParameterKind .POSITIONAL );
245
+ expect (parameters[1 ].type, typeProvider.stringType);
246
+
247
+ expect (parameters[2 ].name, 'c' );
248
+ expect (parameters[2 ].nameOffset, 48 );
249
+ expect (parameters[2 ].parameterKind, ParameterKind .POSITIONAL );
250
+ expect (parameters[2 ].type, typeProvider.boolType);
251
+ }
252
+
253
+ VariableDeclarationStatement vStatement = mainStatements[1 ];
254
+ VariableDeclaration vDeclaration = vStatement.variables.variables[0 ];
255
+ expect (vDeclaration.element.type, same (doubleType));
256
+
257
+ MethodInvocation fInvocation = vDeclaration.initializer;
258
+ expect (fInvocation.methodName.staticElement, same (fElement));
259
+ expect (fInvocation.methodName.staticType.toString (), fTypeString);
260
+ expect (fInvocation.staticType, same (doubleType));
261
+ expect (fInvocation.staticInvokeType.toString (), fTypeString);
262
+ }
263
+
132
264
test_local_variable () async {
133
265
String content = r'''
134
266
void main() {
0 commit comments