Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: pkg/analyzer/test/src/dart/constant/evaluation_test.dart

Issue 2624793002: Make error producing tests ansynchronous. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.test.constant_test; 5 library analyzer.test.constant_test;
6 6
7 import 'dart:async';
7 import 'package:analyzer/context/declared_variables.dart'; 8 import 'package:analyzer/context/declared_variables.dart';
8 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
9 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart';
10 import 'package:analyzer/dart/ast/token.dart'; 11 import 'package:analyzer/dart/ast/token.dart';
11 import 'package:analyzer/dart/element/element.dart'; 12 import 'package:analyzer/dart/element/element.dart';
12 import 'package:analyzer/error/error.dart'; 13 import 'package:analyzer/error/error.dart';
13 import 'package:analyzer/error/listener.dart'; 14 import 'package:analyzer/error/listener.dart';
14 import 'package:analyzer/src/dart/element/element.dart'; 15 import 'package:analyzer/src/dart/element/element.dart';
15 import 'package:analyzer/src/error/codes.dart'; 16 import 'package:analyzer/src/error/codes.dart';
16 import 'package:analyzer/src/generated/constant.dart'; 17 import 'package:analyzer/src/generated/constant.dart';
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 === ${s.shortName} 86 === ${s.shortName}
86 $text'''); 87 $text''');
87 } 88 }
88 } 89 }
89 fail(out.toString()); 90 fail(out.toString());
90 } 91 }
91 } 92 }
92 93
93 @reflectiveTest 94 @reflectiveTest
94 class ConstantValueComputerTest extends ResolverTestCase { 95 class ConstantValueComputerTest extends ResolverTestCase {
95 void test_annotation_constConstructor() { 96 test_annotation_constConstructor() async {
96 CompilationUnit compilationUnit = resolveSource(r''' 97 CompilationUnit compilationUnit = resolveSource(r'''
97 class A { 98 class A {
98 final int i; 99 final int i;
99 const A(this.i); 100 const A(this.i);
100 } 101 }
101 102
102 class C { 103 class C {
103 @A(5) 104 @A(5)
104 f() {} 105 f() {}
105 } 106 }
106 '''); 107 ''');
107 EvaluationResultImpl result = 108 EvaluationResultImpl result =
108 _evaluateAnnotation(compilationUnit, "C", "f"); 109 _evaluateAnnotation(compilationUnit, "C", "f");
109 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A'); 110 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A');
110 _assertIntField(annotationFields, 'i', 5); 111 _assertIntField(annotationFields, 'i', 5);
111 } 112 }
112 113
113 void test_annotation_constConstructor_named() { 114 test_annotation_constConstructor_named() async {
114 CompilationUnit compilationUnit = resolveSource(r''' 115 CompilationUnit compilationUnit = resolveSource(r'''
115 class A { 116 class A {
116 final int i; 117 final int i;
117 const A.named(this.i); 118 const A.named(this.i);
118 } 119 }
119 120
120 class C { 121 class C {
121 @A.named(5) 122 @A.named(5)
122 f() {} 123 f() {}
123 } 124 }
124 '''); 125 ''');
125 EvaluationResultImpl result = 126 EvaluationResultImpl result =
126 _evaluateAnnotation(compilationUnit, "C", "f"); 127 _evaluateAnnotation(compilationUnit, "C", "f");
127 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A'); 128 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A');
128 _assertIntField(annotationFields, 'i', 5); 129 _assertIntField(annotationFields, 'i', 5);
129 } 130 }
130 131
131 void test_annotation_constConstructor_noArgs() { 132 test_annotation_constConstructor_noArgs() async {
132 // Failing to pass arguments to an annotation which is a constant 133 // Failing to pass arguments to an annotation which is a constant
133 // constructor is illegal, but shouldn't crash analysis. 134 // constructor is illegal, but shouldn't crash analysis.
134 CompilationUnit compilationUnit = resolveSource(r''' 135 CompilationUnit compilationUnit = resolveSource(r'''
135 class A { 136 class A {
136 final int i; 137 final int i;
137 const A(this.i); 138 const A(this.i);
138 } 139 }
139 140
140 class C { 141 class C {
141 @A 142 @A
142 f() {} 143 f() {}
143 } 144 }
144 '''); 145 ''');
145 _evaluateAnnotation(compilationUnit, "C", "f"); 146 _evaluateAnnotation(compilationUnit, "C", "f");
146 } 147 }
147 148
148 void test_annotation_constConstructor_noArgs_named() { 149 test_annotation_constConstructor_noArgs_named() async {
149 // Failing to pass arguments to an annotation which is a constant 150 // Failing to pass arguments to an annotation which is a constant
150 // constructor is illegal, but shouldn't crash analysis. 151 // constructor is illegal, but shouldn't crash analysis.
151 CompilationUnit compilationUnit = resolveSource(r''' 152 CompilationUnit compilationUnit = resolveSource(r'''
152 class A { 153 class A {
153 final int i; 154 final int i;
154 const A.named(this.i); 155 const A.named(this.i);
155 } 156 }
156 157
157 class C { 158 class C {
158 @A.named 159 @A.named
159 f() {} 160 f() {}
160 } 161 }
161 '''); 162 ''');
162 _evaluateAnnotation(compilationUnit, "C", "f"); 163 _evaluateAnnotation(compilationUnit, "C", "f");
163 } 164 }
164 165
165 void test_annotation_nonConstConstructor() { 166 test_annotation_nonConstConstructor() async {
166 // Calling a non-const constructor from an annotation that is illegal, but 167 // Calling a non-const constructor from an annotation that is illegal, but
167 // shouldn't crash analysis. 168 // shouldn't crash analysis.
168 CompilationUnit compilationUnit = resolveSource(r''' 169 CompilationUnit compilationUnit = resolveSource(r'''
169 class A { 170 class A {
170 final int i; 171 final int i;
171 A(this.i); 172 A(this.i);
172 } 173 }
173 174
174 class C { 175 class C {
175 @A(5) 176 @A(5)
176 f() {} 177 f() {}
177 } 178 }
178 '''); 179 ''');
179 _evaluateAnnotation(compilationUnit, "C", "f"); 180 _evaluateAnnotation(compilationUnit, "C", "f");
180 } 181 }
181 182
182 void test_annotation_staticConst() { 183 test_annotation_staticConst() async {
183 CompilationUnit compilationUnit = resolveSource(r''' 184 CompilationUnit compilationUnit = resolveSource(r'''
184 class C { 185 class C {
185 static const int i = 5; 186 static const int i = 5;
186 187
187 @i 188 @i
188 f() {} 189 f() {}
189 } 190 }
190 '''); 191 ''');
191 EvaluationResultImpl result = 192 EvaluationResultImpl result =
192 _evaluateAnnotation(compilationUnit, "C", "f"); 193 _evaluateAnnotation(compilationUnit, "C", "f");
193 expect(_assertValidInt(result), 5); 194 expect(_assertValidInt(result), 5);
194 } 195 }
195 196
196 void test_annotation_staticConst_args() { 197 test_annotation_staticConst_args() async {
197 // Applying arguments to an annotation that is a static const is 198 // Applying arguments to an annotation that is a static const is
198 // illegal, but shouldn't crash analysis. 199 // illegal, but shouldn't crash analysis.
199 CompilationUnit compilationUnit = resolveSource(r''' 200 CompilationUnit compilationUnit = resolveSource(r'''
200 class C { 201 class C {
201 static const int i = 5; 202 static const int i = 5;
202 203
203 @i(1) 204 @i(1)
204 f() {} 205 f() {}
205 } 206 }
206 '''); 207 ''');
207 _evaluateAnnotation(compilationUnit, "C", "f"); 208 _evaluateAnnotation(compilationUnit, "C", "f");
208 } 209 }
209 210
210 void test_annotation_staticConst_otherClass() { 211 test_annotation_staticConst_otherClass() async {
211 CompilationUnit compilationUnit = resolveSource(r''' 212 CompilationUnit compilationUnit = resolveSource(r'''
212 class A { 213 class A {
213 static const int i = 5; 214 static const int i = 5;
214 } 215 }
215 216
216 class C { 217 class C {
217 @A.i 218 @A.i
218 f() {} 219 f() {}
219 } 220 }
220 '''); 221 ''');
221 EvaluationResultImpl result = 222 EvaluationResultImpl result =
222 _evaluateAnnotation(compilationUnit, "C", "f"); 223 _evaluateAnnotation(compilationUnit, "C", "f");
223 expect(_assertValidInt(result), 5); 224 expect(_assertValidInt(result), 5);
224 } 225 }
225 226
226 void test_annotation_staticConst_otherClass_args() { 227 test_annotation_staticConst_otherClass_args() async {
227 // Applying arguments to an annotation that is a static const is 228 // Applying arguments to an annotation that is a static const is
228 // illegal, but shouldn't crash analysis. 229 // illegal, but shouldn't crash analysis.
229 CompilationUnit compilationUnit = resolveSource(r''' 230 CompilationUnit compilationUnit = resolveSource(r'''
230 class A { 231 class A {
231 static const int i = 5; 232 static const int i = 5;
232 } 233 }
233 234
234 class C { 235 class C {
235 @A.i(1) 236 @A.i(1)
236 f() {} 237 f() {}
237 } 238 }
238 '''); 239 ''');
239 _evaluateAnnotation(compilationUnit, "C", "f"); 240 _evaluateAnnotation(compilationUnit, "C", "f");
240 } 241 }
241 242
242 void test_annotation_topLevelVariable() { 243 test_annotation_topLevelVariable() async {
243 CompilationUnit compilationUnit = resolveSource(r''' 244 CompilationUnit compilationUnit = resolveSource(r'''
244 const int i = 5; 245 const int i = 5;
245 class C { 246 class C {
246 @i 247 @i
247 f() {} 248 f() {}
248 } 249 }
249 '''); 250 ''');
250 EvaluationResultImpl result = 251 EvaluationResultImpl result =
251 _evaluateAnnotation(compilationUnit, "C", "f"); 252 _evaluateAnnotation(compilationUnit, "C", "f");
252 expect(_assertValidInt(result), 5); 253 expect(_assertValidInt(result), 5);
253 } 254 }
254 255
255 void test_annotation_topLevelVariable_args() { 256 test_annotation_topLevelVariable_args() async {
256 // Applying arguments to an annotation that is a top-level variable is 257 // Applying arguments to an annotation that is a top-level variable is
257 // illegal, but shouldn't crash analysis. 258 // illegal, but shouldn't crash analysis.
258 CompilationUnit compilationUnit = resolveSource(r''' 259 CompilationUnit compilationUnit = resolveSource(r'''
259 const int i = 5; 260 const int i = 5;
260 class C { 261 class C {
261 @i(1) 262 @i(1)
262 f() {} 263 f() {}
263 } 264 }
264 '''); 265 ''');
265 _evaluateAnnotation(compilationUnit, "C", "f"); 266 _evaluateAnnotation(compilationUnit, "C", "f");
266 } 267 }
267 268
268 void test_computeValues_cycle() { 269 test_computeValues_cycle() async {
269 TestLogger logger = new TestLogger(); 270 TestLogger logger = new TestLogger();
270 AnalysisEngine.instance.logger = logger; 271 AnalysisEngine.instance.logger = logger;
271 try { 272 try {
272 Source source = addSource(r''' 273 Source source = addSource(r'''
273 const int a = c; 274 const int a = c;
274 const int b = a; 275 const int b = a;
275 const int c = b;'''); 276 const int c = b;''');
276 LibraryElement libraryElement = resolve2(source); 277 LibraryElement libraryElement = resolve2(source);
277 CompilationUnit unit = 278 CompilationUnit unit =
278 analysisContext.resolveCompilationUnit(source, libraryElement); 279 analysisContext.resolveCompilationUnit(source, libraryElement);
279 analysisContext.computeErrors(source); 280 analysisContext.computeErrors(source);
280 expect(unit, isNotNull); 281 expect(unit, isNotNull);
281 ConstantValueComputer computer = _makeConstantValueComputer(); 282 ConstantValueComputer computer = _makeConstantValueComputer();
282 computer.add(unit); 283 computer.add(unit);
283 computer.computeValues(); 284 computer.computeValues();
284 NodeList<CompilationUnitMember> members = unit.declarations; 285 NodeList<CompilationUnitMember> members = unit.declarations;
285 expect(members, hasLength(3)); 286 expect(members, hasLength(3));
286 _validate(false, (members[0] as TopLevelVariableDeclaration).variables); 287 _validate(false, (members[0] as TopLevelVariableDeclaration).variables);
287 _validate(false, (members[1] as TopLevelVariableDeclaration).variables); 288 _validate(false, (members[1] as TopLevelVariableDeclaration).variables);
288 _validate(false, (members[2] as TopLevelVariableDeclaration).variables); 289 _validate(false, (members[2] as TopLevelVariableDeclaration).variables);
289 } finally { 290 } finally {
290 AnalysisEngine.instance.logger = Logger.NULL; 291 AnalysisEngine.instance.logger = Logger.NULL;
291 } 292 }
292 } 293 }
293 294
294 void test_computeValues_dependentVariables() { 295 test_computeValues_dependentVariables() async {
295 Source source = addSource(r''' 296 Source source = addSource(r'''
296 const int b = a; 297 const int b = a;
297 const int a = 0;'''); 298 const int a = 0;''');
298 LibraryElement libraryElement = resolve2(source); 299 LibraryElement libraryElement = resolve2(source);
299 CompilationUnit unit = 300 CompilationUnit unit =
300 analysisContext.resolveCompilationUnit(source, libraryElement); 301 analysisContext.resolveCompilationUnit(source, libraryElement);
301 expect(unit, isNotNull); 302 expect(unit, isNotNull);
302 ConstantValueComputer computer = _makeConstantValueComputer(); 303 ConstantValueComputer computer = _makeConstantValueComputer();
303 computer.add(unit); 304 computer.add(unit);
304 computer.computeValues(); 305 computer.computeValues();
305 NodeList<CompilationUnitMember> members = unit.declarations; 306 NodeList<CompilationUnitMember> members = unit.declarations;
306 expect(members, hasLength(2)); 307 expect(members, hasLength(2));
307 _validate(true, (members[0] as TopLevelVariableDeclaration).variables); 308 _validate(true, (members[0] as TopLevelVariableDeclaration).variables);
308 _validate(true, (members[1] as TopLevelVariableDeclaration).variables); 309 _validate(true, (members[1] as TopLevelVariableDeclaration).variables);
309 } 310 }
310 311
311 void test_computeValues_empty() { 312 test_computeValues_empty() async {
312 ConstantValueComputer computer = _makeConstantValueComputer(); 313 ConstantValueComputer computer = _makeConstantValueComputer();
313 computer.computeValues(); 314 computer.computeValues();
314 } 315 }
315 316
316 void test_computeValues_multipleSources() { 317 test_computeValues_multipleSources() async {
317 Source librarySource = addNamedSource( 318 Source librarySource = addNamedSource(
318 "/lib.dart", 319 "/lib.dart",
319 r''' 320 r'''
320 library lib; 321 library lib;
321 part 'part.dart'; 322 part 'part.dart';
322 const int c = b; 323 const int c = b;
323 const int a = 0;'''); 324 const int a = 0;''');
324 Source partSource = addNamedSource( 325 Source partSource = addNamedSource(
325 "/part.dart", 326 "/part.dart",
326 r''' 327 r'''
(...skipping 16 matching lines...) Expand all
343 _validate( 344 _validate(
344 true, (libraryMembers[0] as TopLevelVariableDeclaration).variables); 345 true, (libraryMembers[0] as TopLevelVariableDeclaration).variables);
345 _validate( 346 _validate(
346 true, (libraryMembers[1] as TopLevelVariableDeclaration).variables); 347 true, (libraryMembers[1] as TopLevelVariableDeclaration).variables);
347 NodeList<CompilationUnitMember> partMembers = libraryUnit.declarations; 348 NodeList<CompilationUnitMember> partMembers = libraryUnit.declarations;
348 expect(partMembers, hasLength(2)); 349 expect(partMembers, hasLength(2));
349 _validate(true, (partMembers[0] as TopLevelVariableDeclaration).variables); 350 _validate(true, (partMembers[0] as TopLevelVariableDeclaration).variables);
350 _validate(true, (partMembers[1] as TopLevelVariableDeclaration).variables); 351 _validate(true, (partMembers[1] as TopLevelVariableDeclaration).variables);
351 } 352 }
352 353
353 void test_computeValues_singleVariable() { 354 test_computeValues_singleVariable() async {
354 Source source = addSource("const int a = 0;"); 355 Source source = addSource("const int a = 0;");
355 LibraryElement libraryElement = resolve2(source); 356 LibraryElement libraryElement = resolve2(source);
356 CompilationUnit unit = 357 CompilationUnit unit =
357 analysisContext.resolveCompilationUnit(source, libraryElement); 358 analysisContext.resolveCompilationUnit(source, libraryElement);
358 expect(unit, isNotNull); 359 expect(unit, isNotNull);
359 ConstantValueComputer computer = _makeConstantValueComputer(); 360 ConstantValueComputer computer = _makeConstantValueComputer();
360 computer.add(unit); 361 computer.add(unit);
361 computer.computeValues(); 362 computer.computeValues();
362 NodeList<CompilationUnitMember> members = unit.declarations; 363 NodeList<CompilationUnitMember> members = unit.declarations;
363 expect(members, hasLength(1)); 364 expect(members, hasLength(1));
364 _validate(true, (members[0] as TopLevelVariableDeclaration).variables); 365 _validate(true, (members[0] as TopLevelVariableDeclaration).variables);
365 } 366 }
366 367
367 void test_computeValues_value_depends_on_enum() { 368 test_computeValues_value_depends_on_enum() async {
368 Source source = addSource(''' 369 Source source = addSource('''
369 enum E { id0, id1 } 370 enum E { id0, id1 }
370 const E e = E.id0; 371 const E e = E.id0;
371 '''); 372 ''');
372 LibraryElement libraryElement = resolve2(source); 373 LibraryElement libraryElement = resolve2(source);
373 CompilationUnit unit = 374 CompilationUnit unit =
374 analysisContext.resolveCompilationUnit(source, libraryElement); 375 analysisContext.resolveCompilationUnit(source, libraryElement);
375 expect(unit, isNotNull); 376 expect(unit, isNotNull);
376 ConstantValueComputer computer = _makeConstantValueComputer(); 377 ConstantValueComputer computer = _makeConstantValueComputer();
377 computer.add(unit); 378 computer.add(unit);
378 computer.computeValues(); 379 computer.computeValues();
379 TopLevelVariableDeclaration declaration = unit.declarations 380 TopLevelVariableDeclaration declaration = unit.declarations
380 .firstWhere((member) => member is TopLevelVariableDeclaration); 381 .firstWhere((member) => member is TopLevelVariableDeclaration);
381 _validate(true, declaration.variables); 382 _validate(true, declaration.variables);
382 } 383 }
383 384
384 void test_dependencyOnConstructor() { 385 test_dependencyOnConstructor() async {
385 // x depends on "const A()" 386 // x depends on "const A()"
386 _assertProperDependencies(r''' 387 await _assertProperDependencies2(r'''
387 class A { 388 class A {
388 const A(); 389 const A();
389 } 390 }
390 const x = const A();'''); 391 const x = const A();''');
391 } 392 }
392 393
393 void test_dependencyOnConstructorArgument() { 394 test_dependencyOnConstructorArgument() async {
394 // "const A(x)" depends on x 395 // "const A(x)" depends on x
395 _assertProperDependencies(r''' 396 await _assertProperDependencies2(r'''
396 class A { 397 class A {
397 const A(this.next); 398 const A(this.next);
398 final A next; 399 final A next;
399 } 400 }
400 const A x = const A(null); 401 const A x = const A(null);
401 const A y = const A(x);'''); 402 const A y = const A(x);''');
402 } 403 }
403 404
404 void test_dependencyOnConstructorArgument_unresolvedConstructor() { 405 test_dependencyOnConstructorArgument_unresolvedConstructor() async {
405 // "const A.a(x)" depends on x even if the constructor A.a can't be found. 406 // "const A.a(x)" depends on x even if the constructor A.a can't be found.
406 _assertProperDependencies( 407 await _assertProperDependencies2(
407 r''' 408 r'''
408 class A { 409 class A {
409 } 410 }
410 const int x = 1; 411 const int x = 1;
411 const A y = const A.a(x);''', 412 const A y = const A.a(x);''',
412 [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR]); 413 [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR]);
413 } 414 }
414 415
415 void test_dependencyOnConstructorInitializer() { 416 test_dependencyOnConstructorInitializer() async {
416 // "const A()" depends on x 417 // "const A()" depends on x
417 _assertProperDependencies(r''' 418 await _assertProperDependencies2(r'''
418 const int x = 1; 419 const int x = 1;
419 class A { 420 class A {
420 const A() : v = x; 421 const A() : v = x;
421 final int v; 422 final int v;
422 }'''); 423 }''');
423 } 424 }
424 425
425 void test_dependencyOnExplicitSuperConstructor() { 426 test_dependencyOnExplicitSuperConstructor() async {
426 // b depends on B() depends on A() 427 // b depends on B() depends on A()
427 _assertProperDependencies(r''' 428 await _assertProperDependencies2(r'''
428 class A { 429 class A {
429 const A(this.x); 430 const A(this.x);
430 final int x; 431 final int x;
431 } 432 }
432 class B extends A { 433 class B extends A {
433 const B() : super(5); 434 const B() : super(5);
434 } 435 }
435 const B b = const B();'''); 436 const B b = const B();''');
436 } 437 }
437 438
438 void test_dependencyOnExplicitSuperConstructorParameters() { 439 test_dependencyOnExplicitSuperConstructorParameters() async {
439 // b depends on B() depends on i 440 // b depends on B() depends on i
440 _assertProperDependencies(r''' 441 await _assertProperDependencies2(r'''
441 class A { 442 class A {
442 const A(this.x); 443 const A(this.x);
443 final int x; 444 final int x;
444 } 445 }
445 class B extends A { 446 class B extends A {
446 const B() : super(i); 447 const B() : super(i);
447 } 448 }
448 const B b = const B(); 449 const B b = const B();
449 const int i = 5;'''); 450 const int i = 5;''');
450 } 451 }
451 452
452 void test_dependencyOnFactoryRedirect() { 453 test_dependencyOnFactoryRedirect() async {
453 // a depends on A.foo() depends on A.bar() 454 // a depends on A.foo() depends on A.bar()
454 _assertProperDependencies(r''' 455 await _assertProperDependencies2(r'''
455 const A a = const A.foo(); 456 const A a = const A.foo();
456 class A { 457 class A {
457 factory const A.foo() = A.bar; 458 factory const A.foo() = A.bar;
458 const A.bar(); 459 const A.bar();
459 }'''); 460 }''');
460 } 461 }
461 462
462 void test_dependencyOnFactoryRedirectWithTypeParams() { 463 test_dependencyOnFactoryRedirectWithTypeParams() async {
463 _assertProperDependencies(r''' 464 await _assertProperDependencies2(r'''
464 class A { 465 class A {
465 const factory A(var a) = B<int>; 466 const factory A(var a) = B<int>;
466 } 467 }
467 468
468 class B<T> implements A { 469 class B<T> implements A {
469 final T x; 470 final T x;
470 const B(this.x); 471 const B(this.x);
471 } 472 }
472 473
473 const A a = const A(10);'''); 474 const A a = const A(10);''');
474 } 475 }
475 476
476 void test_dependencyOnImplicitSuperConstructor() { 477 test_dependencyOnImplicitSuperConstructor() async {
477 // b depends on B() depends on A() 478 // b depends on B() depends on A()
478 _assertProperDependencies(r''' 479 await _assertProperDependencies2(r'''
479 class A { 480 class A {
480 const A() : x = 5; 481 const A() : x = 5;
481 final int x; 482 final int x;
482 } 483 }
483 class B extends A { 484 class B extends A {
484 const B(); 485 const B();
485 } 486 }
486 const B b = const B();'''); 487 const B b = const B();''');
487 } 488 }
488 489
489 void test_dependencyOnInitializedFinal() { 490 test_dependencyOnInitializedFinal() async {
490 // a depends on A() depends on A.x 491 // a depends on A() depends on A.x
491 _assertProperDependencies(''' 492 await _assertProperDependencies2('''
492 class A { 493 class A {
493 const A(); 494 const A();
494 final int x = 1; 495 final int x = 1;
495 } 496 }
496 const A a = const A(); 497 const A a = const A();
497 '''); 498 ''');
498 } 499 }
499 500
500 void test_dependencyOnInitializedNonStaticConst() { 501 test_dependencyOnInitializedNonStaticConst() async {
501 // Even though non-static consts are not allowed by the language, we need 502 // Even though non-static consts are not allowed by the language, we need
502 // to handle them for error recovery purposes. 503 // to handle them for error recovery purposes.
503 // a depends on A() depends on A.x 504 // a depends on A() depends on A.x
504 _assertProperDependencies( 505 await _assertProperDependencies2(
505 ''' 506 '''
506 class A { 507 class A {
507 const A(); 508 const A();
508 const int x = 1; 509 const int x = 1;
509 } 510 }
510 const A a = const A(); 511 const A a = const A();
511 ''', 512 ''',
512 [CompileTimeErrorCode.CONST_INSTANCE_FIELD]); 513 [CompileTimeErrorCode.CONST_INSTANCE_FIELD]);
513 } 514 }
514 515
515 void test_dependencyOnNonFactoryRedirect() { 516 test_dependencyOnNonFactoryRedirect() async {
516 // a depends on A.foo() depends on A.bar() 517 // a depends on A.foo() depends on A.bar()
517 _assertProperDependencies(r''' 518 await _assertProperDependencies2(r'''
518 const A a = const A.foo(); 519 const A a = const A.foo();
519 class A { 520 class A {
520 const A.foo() : this.bar(); 521 const A.foo() : this.bar();
521 const A.bar(); 522 const A.bar();
522 }'''); 523 }''');
523 } 524 }
524 525
525 void test_dependencyOnNonFactoryRedirect_arg() { 526 test_dependencyOnNonFactoryRedirect_arg() async {
526 // a depends on A.foo() depends on b 527 // a depends on A.foo() depends on b
527 _assertProperDependencies(r''' 528 await _assertProperDependencies2(r'''
528 const A a = const A.foo(); 529 const A a = const A.foo();
529 const int b = 1; 530 const int b = 1;
530 class A { 531 class A {
531 const A.foo() : this.bar(b); 532 const A.foo() : this.bar(b);
532 const A.bar(x) : y = x; 533 const A.bar(x) : y = x;
533 final int y; 534 final int y;
534 }'''); 535 }''');
535 } 536 }
536 537
537 void test_dependencyOnNonFactoryRedirect_defaultValue() { 538 test_dependencyOnNonFactoryRedirect_defaultValue() async {
538 // a depends on A.foo() depends on A.bar() depends on b 539 // a depends on A.foo() depends on A.bar() depends on b
539 _assertProperDependencies(r''' 540 await _assertProperDependencies2(r'''
540 const A a = const A.foo(); 541 const A a = const A.foo();
541 const int b = 1; 542 const int b = 1;
542 class A { 543 class A {
543 const A.foo() : this.bar(); 544 const A.foo() : this.bar();
544 const A.bar([x = b]) : y = x; 545 const A.bar([x = b]) : y = x;
545 final int y; 546 final int y;
546 }'''); 547 }''');
547 } 548 }
548 549
549 void test_dependencyOnNonFactoryRedirect_toMissing() { 550 test_dependencyOnNonFactoryRedirect_toMissing() async {
550 // a depends on A.foo() which depends on nothing, since A.bar() is 551 // a depends on A.foo() which depends on nothing, since A.bar() is
551 // missing. 552 // missing.
552 _assertProperDependencies( 553 await _assertProperDependencies2(
553 r''' 554 r'''
554 const A a = const A.foo(); 555 const A a = const A.foo();
555 class A { 556 class A {
556 const A.foo() : this.bar(); 557 const A.foo() : this.bar();
557 }''', 558 }''',
558 [CompileTimeErrorCode.REDIRECT_GENERATIVE_TO_MISSING_CONSTRUCTOR]); 559 [CompileTimeErrorCode.REDIRECT_GENERATIVE_TO_MISSING_CONSTRUCTOR]);
559 } 560 }
560 561
561 void test_dependencyOnNonFactoryRedirect_toNonConst() { 562 test_dependencyOnNonFactoryRedirect_toNonConst() async {
562 // a depends on A.foo() which depends on nothing, since A.bar() is 563 // a depends on A.foo() which depends on nothing, since A.bar() is
563 // non-const. 564 // non-const.
564 _assertProperDependencies(r''' 565 await _assertProperDependencies2(r'''
565 const A a = const A.foo(); 566 const A a = const A.foo();
566 class A { 567 class A {
567 const A.foo() : this.bar(); 568 const A.foo() : this.bar();
568 A.bar(); 569 A.bar();
569 }'''); 570 }''');
570 } 571 }
571 572
572 void test_dependencyOnNonFactoryRedirect_unnamed() { 573 test_dependencyOnNonFactoryRedirect_unnamed() async {
573 // a depends on A.foo() depends on A() 574 // a depends on A.foo() depends on A()
574 _assertProperDependencies(r''' 575 await _assertProperDependencies2(r'''
575 const A a = const A.foo(); 576 const A a = const A.foo();
576 class A { 577 class A {
577 const A.foo() : this(); 578 const A.foo() : this();
578 const A(); 579 const A();
579 }'''); 580 }''');
580 } 581 }
581 582
582 void test_dependencyOnOptionalParameterDefault() { 583 test_dependencyOnOptionalParameterDefault() async {
583 // a depends on A() depends on B() 584 // a depends on A() depends on B()
584 _assertProperDependencies(r''' 585 await _assertProperDependencies2(r'''
585 class A { 586 class A {
586 const A([x = const B()]) : b = x; 587 const A([x = const B()]) : b = x;
587 final B b; 588 final B b;
588 } 589 }
589 class B { 590 class B {
590 const B(); 591 const B();
591 } 592 }
592 const A a = const A();'''); 593 const A a = const A();''');
593 } 594 }
594 595
595 void test_dependencyOnVariable() { 596 test_dependencyOnVariable() async {
596 // x depends on y 597 // x depends on y
597 _assertProperDependencies(r''' 598 await _assertProperDependencies2(r'''
598 const x = y + 1; 599 const x = y + 1;
599 const y = 2;'''); 600 const y = 2;''');
600 } 601 }
601 602
602 void test_final_initialized_at_declaration() { 603 test_final_initialized_at_declaration() async {
603 CompilationUnit compilationUnit = resolveSource(''' 604 CompilationUnit compilationUnit = resolveSource('''
604 class A { 605 class A {
605 final int i = 123; 606 final int i = 123;
606 const A(); 607 const A();
607 } 608 }
608 609
609 const A a = const A(); 610 const A a = const A();
610 '''); 611 ''');
611 EvaluationResultImpl result = 612 EvaluationResultImpl result =
612 _evaluateTopLevelVariable(compilationUnit, 'a'); 613 _evaluateTopLevelVariable(compilationUnit, 'a');
613 Map<String, DartObjectImpl> fields = _assertType(result, "A"); 614 Map<String, DartObjectImpl> fields = _assertType(result, "A");
614 expect(fields, hasLength(1)); 615 expect(fields, hasLength(1));
615 _assertIntField(fields, "i", 123); 616 _assertIntField(fields, "i", 123);
616 } 617 }
617 618
618 void test_fromEnvironment_bool_default_false() { 619 test_fromEnvironment_bool_default_false() async {
619 expect(_assertValidBool(_check_fromEnvironment_bool(null, "false")), false); 620 expect(_assertValidBool(_check_fromEnvironment_bool(null, "false")), false);
620 } 621 }
621 622
622 void test_fromEnvironment_bool_default_overridden() { 623 test_fromEnvironment_bool_default_overridden() async {
623 expect( 624 expect(
624 _assertValidBool(_check_fromEnvironment_bool("false", "true")), false); 625 _assertValidBool(_check_fromEnvironment_bool("false", "true")), false);
625 } 626 }
626 627
627 void test_fromEnvironment_bool_default_parseError() { 628 test_fromEnvironment_bool_default_parseError() async {
628 expect(_assertValidBool(_check_fromEnvironment_bool("parseError", "true")), 629 expect(_assertValidBool(_check_fromEnvironment_bool("parseError", "true")),
629 true); 630 true);
630 } 631 }
631 632
632 void test_fromEnvironment_bool_default_true() { 633 test_fromEnvironment_bool_default_true() async {
633 expect(_assertValidBool(_check_fromEnvironment_bool(null, "true")), true); 634 expect(_assertValidBool(_check_fromEnvironment_bool(null, "true")), true);
634 } 635 }
635 636
636 void test_fromEnvironment_bool_false() { 637 test_fromEnvironment_bool_false() async {
637 expect(_assertValidBool(_check_fromEnvironment_bool("false", null)), false); 638 expect(_assertValidBool(_check_fromEnvironment_bool("false", null)), false);
638 } 639 }
639 640
640 void test_fromEnvironment_bool_parseError() { 641 test_fromEnvironment_bool_parseError() async {
641 expect(_assertValidBool(_check_fromEnvironment_bool("parseError", null)), 642 expect(_assertValidBool(_check_fromEnvironment_bool("parseError", null)),
642 false); 643 false);
643 } 644 }
644 645
645 void test_fromEnvironment_bool_true() { 646 test_fromEnvironment_bool_true() async {
646 expect(_assertValidBool(_check_fromEnvironment_bool("true", null)), true); 647 expect(_assertValidBool(_check_fromEnvironment_bool("true", null)), true);
647 } 648 }
648 649
649 void test_fromEnvironment_bool_undeclared() { 650 test_fromEnvironment_bool_undeclared() async {
650 _assertValidUnknown(_check_fromEnvironment_bool(null, null)); 651 _assertValidUnknown(_check_fromEnvironment_bool(null, null));
651 } 652 }
652 653
653 void test_fromEnvironment_int_default_overridden() { 654 test_fromEnvironment_int_default_overridden() async {
654 expect(_assertValidInt(_check_fromEnvironment_int("234", "123")), 234); 655 expect(_assertValidInt(_check_fromEnvironment_int("234", "123")), 234);
655 } 656 }
656 657
657 void test_fromEnvironment_int_default_parseError() { 658 test_fromEnvironment_int_default_parseError() async {
658 expect( 659 expect(
659 _assertValidInt(_check_fromEnvironment_int("parseError", "123")), 123); 660 _assertValidInt(_check_fromEnvironment_int("parseError", "123")), 123);
660 } 661 }
661 662
662 void test_fromEnvironment_int_default_undeclared() { 663 test_fromEnvironment_int_default_undeclared() async {
663 expect(_assertValidInt(_check_fromEnvironment_int(null, "123")), 123); 664 expect(_assertValidInt(_check_fromEnvironment_int(null, "123")), 123);
664 } 665 }
665 666
666 void test_fromEnvironment_int_ok() { 667 test_fromEnvironment_int_ok() async {
667 expect(_assertValidInt(_check_fromEnvironment_int("234", null)), 234); 668 expect(_assertValidInt(_check_fromEnvironment_int("234", null)), 234);
668 } 669 }
669 670
670 void test_fromEnvironment_int_parseError() { 671 test_fromEnvironment_int_parseError() async {
671 _assertValidNull(_check_fromEnvironment_int("parseError", null)); 672 _assertValidNull(_check_fromEnvironment_int("parseError", null));
672 } 673 }
673 674
674 void test_fromEnvironment_int_parseError_nullDefault() { 675 test_fromEnvironment_int_parseError_nullDefault() async {
675 _assertValidNull(_check_fromEnvironment_int("parseError", "null")); 676 _assertValidNull(_check_fromEnvironment_int("parseError", "null"));
676 } 677 }
677 678
678 void test_fromEnvironment_int_undeclared() { 679 test_fromEnvironment_int_undeclared() async {
679 _assertValidUnknown(_check_fromEnvironment_int(null, null)); 680 _assertValidUnknown(_check_fromEnvironment_int(null, null));
680 } 681 }
681 682
682 void test_fromEnvironment_int_undeclared_nullDefault() { 683 test_fromEnvironment_int_undeclared_nullDefault() async {
683 _assertValidNull(_check_fromEnvironment_int(null, "null")); 684 _assertValidNull(_check_fromEnvironment_int(null, "null"));
684 } 685 }
685 686
686 void test_fromEnvironment_string_default_overridden() { 687 test_fromEnvironment_string_default_overridden() async {
687 expect(_assertValidString(_check_fromEnvironment_string("abc", "'def'")), 688 expect(_assertValidString(_check_fromEnvironment_string("abc", "'def'")),
688 "abc"); 689 "abc");
689 } 690 }
690 691
691 void test_fromEnvironment_string_default_undeclared() { 692 test_fromEnvironment_string_default_undeclared() async {
692 expect(_assertValidString(_check_fromEnvironment_string(null, "'def'")), 693 expect(_assertValidString(_check_fromEnvironment_string(null, "'def'")),
693 "def"); 694 "def");
694 } 695 }
695 696
696 void test_fromEnvironment_string_empty() { 697 test_fromEnvironment_string_empty() async {
697 expect(_assertValidString(_check_fromEnvironment_string("", null)), ""); 698 expect(_assertValidString(_check_fromEnvironment_string("", null)), "");
698 } 699 }
699 700
700 void test_fromEnvironment_string_ok() { 701 test_fromEnvironment_string_ok() async {
701 expect( 702 expect(
702 _assertValidString(_check_fromEnvironment_string("abc", null)), "abc"); 703 _assertValidString(_check_fromEnvironment_string("abc", null)), "abc");
703 } 704 }
704 705
705 void test_fromEnvironment_string_undeclared() { 706 test_fromEnvironment_string_undeclared() async {
706 _assertValidUnknown(_check_fromEnvironment_string(null, null)); 707 _assertValidUnknown(_check_fromEnvironment_string(null, null));
707 } 708 }
708 709
709 void test_fromEnvironment_string_undeclared_nullDefault() { 710 test_fromEnvironment_string_undeclared_nullDefault() async {
710 _assertValidNull(_check_fromEnvironment_string(null, "null")); 711 _assertValidNull(_check_fromEnvironment_string(null, "null"));
711 } 712 }
712 713
713 void test_instanceCreationExpression_computedField() { 714 test_instanceCreationExpression_computedField() async {
714 CompilationUnit compilationUnit = resolveSource(r''' 715 CompilationUnit compilationUnit = resolveSource(r'''
715 const foo = const A(4, 5); 716 const foo = const A(4, 5);
716 class A { 717 class A {
717 const A(int i, int j) : k = 2 * i + j; 718 const A(int i, int j) : k = 2 * i + j;
718 final int k; 719 final int k;
719 }'''); 720 }''');
720 EvaluationResultImpl result = 721 EvaluationResultImpl result =
721 _evaluateTopLevelVariable(compilationUnit, "foo"); 722 _evaluateTopLevelVariable(compilationUnit, "foo");
722 Map<String, DartObjectImpl> fields = _assertType(result, "A"); 723 Map<String, DartObjectImpl> fields = _assertType(result, "A");
723 expect(fields, hasLength(1)); 724 expect(fields, hasLength(1));
724 _assertIntField(fields, "k", 13); 725 _assertIntField(fields, "k", 13);
725 } 726 }
726 727
727 void 728 test_instanceCreationExpression_computedField_namedOptionalWithDefault() async {
728 test_instanceCreationExpression_computedField_namedOptionalWithDefault() {
729 _checkInstanceCreationOptionalParams(false, true, true); 729 _checkInstanceCreationOptionalParams(false, true, true);
730 } 730 }
731 731
732 void 732 test_instanceCreationExpression_computedField_namedOptionalWithoutDefault() as ync {
733 test_instanceCreationExpression_computedField_namedOptionalWithoutDefault( ) {
734 _checkInstanceCreationOptionalParams(false, true, false); 733 _checkInstanceCreationOptionalParams(false, true, false);
735 } 734 }
736 735
737 void 736 test_instanceCreationExpression_computedField_unnamedOptionalWithDefault() asy nc {
738 test_instanceCreationExpression_computedField_unnamedOptionalWithDefault() {
739 _checkInstanceCreationOptionalParams(false, false, true); 737 _checkInstanceCreationOptionalParams(false, false, true);
740 } 738 }
741 739
742 void 740 test_instanceCreationExpression_computedField_unnamedOptionalWithoutDefault() async {
743 test_instanceCreationExpression_computedField_unnamedOptionalWithoutDefaul t() {
744 _checkInstanceCreationOptionalParams(false, false, false); 741 _checkInstanceCreationOptionalParams(false, false, false);
745 } 742 }
746 743
747 void test_instanceCreationExpression_computedField_usesConstConstructor() { 744 test_instanceCreationExpression_computedField_usesConstConstructor() async {
748 CompilationUnit compilationUnit = resolveSource(r''' 745 CompilationUnit compilationUnit = resolveSource(r'''
749 const foo = const A(3); 746 const foo = const A(3);
750 class A { 747 class A {
751 const A(int i) : b = const B(4); 748 const A(int i) : b = const B(4);
752 final int b; 749 final int b;
753 } 750 }
754 class B { 751 class B {
755 const B(this.k); 752 const B(this.k);
756 final int k; 753 final int k;
757 }'''); 754 }''');
758 EvaluationResultImpl result = 755 EvaluationResultImpl result =
759 _evaluateTopLevelVariable(compilationUnit, "foo"); 756 _evaluateTopLevelVariable(compilationUnit, "foo");
760 Map<String, DartObjectImpl> fieldsOfA = _assertType(result, "A"); 757 Map<String, DartObjectImpl> fieldsOfA = _assertType(result, "A");
761 expect(fieldsOfA, hasLength(1)); 758 expect(fieldsOfA, hasLength(1));
762 Map<String, DartObjectImpl> fieldsOfB = 759 Map<String, DartObjectImpl> fieldsOfB =
763 _assertFieldType(fieldsOfA, "b", "B"); 760 _assertFieldType(fieldsOfA, "b", "B");
764 expect(fieldsOfB, hasLength(1)); 761 expect(fieldsOfB, hasLength(1));
765 _assertIntField(fieldsOfB, "k", 4); 762 _assertIntField(fieldsOfB, "k", 4);
766 } 763 }
767 764
768 void test_instanceCreationExpression_computedField_usesStaticConst() { 765 test_instanceCreationExpression_computedField_usesStaticConst() async {
769 CompilationUnit compilationUnit = resolveSource(r''' 766 CompilationUnit compilationUnit = resolveSource(r'''
770 const foo = const A(3); 767 const foo = const A(3);
771 class A { 768 class A {
772 const A(int i) : k = i + B.bar; 769 const A(int i) : k = i + B.bar;
773 final int k; 770 final int k;
774 } 771 }
775 class B { 772 class B {
776 static const bar = 4; 773 static const bar = 4;
777 }'''); 774 }''');
778 EvaluationResultImpl result = 775 EvaluationResultImpl result =
779 _evaluateTopLevelVariable(compilationUnit, "foo"); 776 _evaluateTopLevelVariable(compilationUnit, "foo");
780 Map<String, DartObjectImpl> fields = _assertType(result, "A"); 777 Map<String, DartObjectImpl> fields = _assertType(result, "A");
781 expect(fields, hasLength(1)); 778 expect(fields, hasLength(1));
782 _assertIntField(fields, "k", 7); 779 _assertIntField(fields, "k", 7);
783 } 780 }
784 781
785 void test_instanceCreationExpression_computedField_usesTopLevelConst() { 782 test_instanceCreationExpression_computedField_usesTopLevelConst() async {
786 CompilationUnit compilationUnit = resolveSource(r''' 783 CompilationUnit compilationUnit = resolveSource(r'''
787 const foo = const A(3); 784 const foo = const A(3);
788 const bar = 4; 785 const bar = 4;
789 class A { 786 class A {
790 const A(int i) : k = i + bar; 787 const A(int i) : k = i + bar;
791 final int k; 788 final int k;
792 }'''); 789 }''');
793 EvaluationResultImpl result = 790 EvaluationResultImpl result =
794 _evaluateTopLevelVariable(compilationUnit, "foo"); 791 _evaluateTopLevelVariable(compilationUnit, "foo");
795 Map<String, DartObjectImpl> fields = _assertType(result, "A"); 792 Map<String, DartObjectImpl> fields = _assertType(result, "A");
796 expect(fields, hasLength(1)); 793 expect(fields, hasLength(1));
797 _assertIntField(fields, "k", 7); 794 _assertIntField(fields, "k", 7);
798 } 795 }
799 796
800 void test_instanceCreationExpression_explicitSuper() { 797 test_instanceCreationExpression_explicitSuper() async {
801 CompilationUnit compilationUnit = resolveSource(r''' 798 CompilationUnit compilationUnit = resolveSource(r'''
802 const foo = const B(4, 5); 799 const foo = const B(4, 5);
803 class A { 800 class A {
804 const A(this.x); 801 const A(this.x);
805 final int x; 802 final int x;
806 } 803 }
807 class B extends A { 804 class B extends A {
808 const B(int x, this.y) : super(x * 2); 805 const B(int x, this.y) : super(x * 2);
809 final int y; 806 final int y;
810 }'''); 807 }''');
811 EvaluationResultImpl result = 808 EvaluationResultImpl result =
812 _evaluateTopLevelVariable(compilationUnit, "foo"); 809 _evaluateTopLevelVariable(compilationUnit, "foo");
813 Map<String, DartObjectImpl> fields = _assertType(result, "B"); 810 Map<String, DartObjectImpl> fields = _assertType(result, "B");
814 expect(fields, hasLength(2)); 811 expect(fields, hasLength(2));
815 _assertIntField(fields, "y", 5); 812 _assertIntField(fields, "y", 5);
816 Map<String, DartObjectImpl> superclassFields = 813 Map<String, DartObjectImpl> superclassFields =
817 _assertFieldType(fields, GenericState.SUPERCLASS_FIELD, "A"); 814 _assertFieldType(fields, GenericState.SUPERCLASS_FIELD, "A");
818 expect(superclassFields, hasLength(1)); 815 expect(superclassFields, hasLength(1));
819 _assertIntField(superclassFields, "x", 8); 816 _assertIntField(superclassFields, "x", 8);
820 } 817 }
821 818
822 void test_instanceCreationExpression_fieldFormalParameter() { 819 test_instanceCreationExpression_fieldFormalParameter() async {
823 CompilationUnit compilationUnit = resolveSource(r''' 820 CompilationUnit compilationUnit = resolveSource(r'''
824 const foo = const A(42); 821 const foo = const A(42);
825 class A { 822 class A {
826 int x; 823 int x;
827 const A(this.x) 824 const A(this.x)
828 }'''); 825 }''');
829 EvaluationResultImpl result = 826 EvaluationResultImpl result =
830 _evaluateTopLevelVariable(compilationUnit, "foo"); 827 _evaluateTopLevelVariable(compilationUnit, "foo");
831 Map<String, DartObjectImpl> fields = _assertType(result, "A"); 828 Map<String, DartObjectImpl> fields = _assertType(result, "A");
832 expect(fields, hasLength(1)); 829 expect(fields, hasLength(1));
833 _assertIntField(fields, "x", 42); 830 _assertIntField(fields, "x", 42);
834 } 831 }
835 832
836 void 833 test_instanceCreationExpression_fieldFormalParameter_namedOptionalWithDefault( ) async {
837 test_instanceCreationExpression_fieldFormalParameter_namedOptionalWithDefa ult() {
838 _checkInstanceCreationOptionalParams(true, true, true); 834 _checkInstanceCreationOptionalParams(true, true, true);
839 } 835 }
840 836
841 void 837 test_instanceCreationExpression_fieldFormalParameter_namedOptionalWithoutDefau lt() async {
842 test_instanceCreationExpression_fieldFormalParameter_namedOptionalWithoutD efault() {
843 _checkInstanceCreationOptionalParams(true, true, false); 838 _checkInstanceCreationOptionalParams(true, true, false);
844 } 839 }
845 840
846 void 841 test_instanceCreationExpression_fieldFormalParameter_unnamedOptionalWithDefaul t() async {
847 test_instanceCreationExpression_fieldFormalParameter_unnamedOptionalWithDe fault() {
848 _checkInstanceCreationOptionalParams(true, false, true); 842 _checkInstanceCreationOptionalParams(true, false, true);
849 } 843 }
850 844
851 void 845 test_instanceCreationExpression_fieldFormalParameter_unnamedOptionalWithoutDef ault() async {
852 test_instanceCreationExpression_fieldFormalParameter_unnamedOptionalWithou tDefault() {
853 _checkInstanceCreationOptionalParams(true, false, false); 846 _checkInstanceCreationOptionalParams(true, false, false);
854 } 847 }
855 848
856 void test_instanceCreationExpression_implicitSuper() { 849 test_instanceCreationExpression_implicitSuper() async {
857 CompilationUnit compilationUnit = resolveSource(r''' 850 CompilationUnit compilationUnit = resolveSource(r'''
858 const foo = const B(4); 851 const foo = const B(4);
859 class A { 852 class A {
860 const A() : x = 3; 853 const A() : x = 3;
861 final int x; 854 final int x;
862 } 855 }
863 class B extends A { 856 class B extends A {
864 const B(this.y); 857 const B(this.y);
865 final int y; 858 final int y;
866 }'''); 859 }''');
867 EvaluationResultImpl result = 860 EvaluationResultImpl result =
868 _evaluateTopLevelVariable(compilationUnit, "foo"); 861 _evaluateTopLevelVariable(compilationUnit, "foo");
869 Map<String, DartObjectImpl> fields = _assertType(result, "B"); 862 Map<String, DartObjectImpl> fields = _assertType(result, "B");
870 expect(fields, hasLength(2)); 863 expect(fields, hasLength(2));
871 _assertIntField(fields, "y", 4); 864 _assertIntField(fields, "y", 4);
872 Map<String, DartObjectImpl> superclassFields = 865 Map<String, DartObjectImpl> superclassFields =
873 _assertFieldType(fields, GenericState.SUPERCLASS_FIELD, "A"); 866 _assertFieldType(fields, GenericState.SUPERCLASS_FIELD, "A");
874 expect(superclassFields, hasLength(1)); 867 expect(superclassFields, hasLength(1));
875 _assertIntField(superclassFields, "x", 3); 868 _assertIntField(superclassFields, "x", 3);
876 } 869 }
877 870
878 void test_instanceCreationExpression_nonFactoryRedirect() { 871 test_instanceCreationExpression_nonFactoryRedirect() async {
879 CompilationUnit compilationUnit = resolveSource(r''' 872 CompilationUnit compilationUnit = resolveSource(r'''
880 const foo = const A.a1(); 873 const foo = const A.a1();
881 class A { 874 class A {
882 const A.a1() : this.a2(); 875 const A.a1() : this.a2();
883 const A.a2() : x = 5; 876 const A.a2() : x = 5;
884 final int x; 877 final int x;
885 }'''); 878 }''');
886 Map<String, DartObjectImpl> aFields = 879 Map<String, DartObjectImpl> aFields =
887 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); 880 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
888 _assertIntField(aFields, 'x', 5); 881 _assertIntField(aFields, 'x', 5);
889 } 882 }
890 883
891 void test_instanceCreationExpression_nonFactoryRedirect_arg() { 884 test_instanceCreationExpression_nonFactoryRedirect_arg() async {
892 CompilationUnit compilationUnit = resolveSource(r''' 885 CompilationUnit compilationUnit = resolveSource(r'''
893 const foo = const A.a1(1); 886 const foo = const A.a1(1);
894 class A { 887 class A {
895 const A.a1(x) : this.a2(x + 100); 888 const A.a1(x) : this.a2(x + 100);
896 const A.a2(x) : y = x + 10; 889 const A.a2(x) : y = x + 10;
897 final int y; 890 final int y;
898 }'''); 891 }''');
899 Map<String, DartObjectImpl> aFields = 892 Map<String, DartObjectImpl> aFields =
900 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); 893 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
901 _assertIntField(aFields, 'y', 111); 894 _assertIntField(aFields, 'y', 111);
902 } 895 }
903 896
904 void test_instanceCreationExpression_nonFactoryRedirect_cycle() { 897 test_instanceCreationExpression_nonFactoryRedirect_cycle() async {
905 // It is an error to have a cycle in non-factory redirects; however, we 898 // It is an error to have a cycle in non-factory redirects; however, we
906 // need to make sure that even if the error occurs, attempting to evaluate 899 // need to make sure that even if the error occurs, attempting to evaluate
907 // the constant will terminate. 900 // the constant will terminate.
908 CompilationUnit compilationUnit = resolveSource(r''' 901 CompilationUnit compilationUnit = resolveSource(r'''
909 const foo = const A(); 902 const foo = const A();
910 class A { 903 class A {
911 const A() : this.b(); 904 const A() : this.b();
912 const A.b() : this(); 905 const A.b() : this();
913 }'''); 906 }''');
914 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); 907 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo"));
915 } 908 }
916 909
917 void test_instanceCreationExpression_nonFactoryRedirect_defaultArg() { 910 test_instanceCreationExpression_nonFactoryRedirect_defaultArg() async {
918 CompilationUnit compilationUnit = resolveSource(r''' 911 CompilationUnit compilationUnit = resolveSource(r'''
919 const foo = const A.a1(); 912 const foo = const A.a1();
920 class A { 913 class A {
921 const A.a1() : this.a2(); 914 const A.a1() : this.a2();
922 const A.a2([x = 100]) : y = x + 10; 915 const A.a2([x = 100]) : y = x + 10;
923 final int y; 916 final int y;
924 }'''); 917 }''');
925 Map<String, DartObjectImpl> aFields = 918 Map<String, DartObjectImpl> aFields =
926 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); 919 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
927 _assertIntField(aFields, 'y', 110); 920 _assertIntField(aFields, 'y', 110);
928 } 921 }
929 922
930 void test_instanceCreationExpression_nonFactoryRedirect_toMissing() { 923 test_instanceCreationExpression_nonFactoryRedirect_toMissing() async {
931 CompilationUnit compilationUnit = resolveSource(r''' 924 CompilationUnit compilationUnit = resolveSource(r'''
932 const foo = const A.a1(); 925 const foo = const A.a1();
933 class A { 926 class A {
934 const A.a1() : this.a2(); 927 const A.a1() : this.a2();
935 }'''); 928 }''');
936 // We don't care what value foo evaluates to (since there is a compile 929 // We don't care what value foo evaluates to (since there is a compile
937 // error), but we shouldn't crash, and we should figure 930 // error), but we shouldn't crash, and we should figure
938 // out that it evaluates to an instance of class A. 931 // out that it evaluates to an instance of class A.
939 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); 932 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
940 } 933 }
941 934
942 void test_instanceCreationExpression_nonFactoryRedirect_toNonConst() { 935 test_instanceCreationExpression_nonFactoryRedirect_toNonConst() async {
943 CompilationUnit compilationUnit = resolveSource(r''' 936 CompilationUnit compilationUnit = resolveSource(r'''
944 const foo = const A.a1(); 937 const foo = const A.a1();
945 class A { 938 class A {
946 const A.a1() : this.a2(); 939 const A.a1() : this.a2();
947 A.a2(); 940 A.a2();
948 }'''); 941 }''');
949 // We don't care what value foo evaluates to (since there is a compile 942 // We don't care what value foo evaluates to (since there is a compile
950 // error), but we shouldn't crash, and we should figure 943 // error), but we shouldn't crash, and we should figure
951 // out that it evaluates to an instance of class A. 944 // out that it evaluates to an instance of class A.
952 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); 945 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
953 } 946 }
954 947
955 void test_instanceCreationExpression_nonFactoryRedirect_unnamed() { 948 test_instanceCreationExpression_nonFactoryRedirect_unnamed() async {
956 CompilationUnit compilationUnit = resolveSource(r''' 949 CompilationUnit compilationUnit = resolveSource(r'''
957 const foo = const A.a1(); 950 const foo = const A.a1();
958 class A { 951 class A {
959 const A.a1() : this(); 952 const A.a1() : this();
960 const A() : x = 5; 953 const A() : x = 5;
961 final int x; 954 final int x;
962 }'''); 955 }''');
963 Map<String, DartObjectImpl> aFields = 956 Map<String, DartObjectImpl> aFields =
964 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A"); 957 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "A");
965 _assertIntField(aFields, 'x', 5); 958 _assertIntField(aFields, 'x', 5);
966 } 959 }
967 960
968 void test_instanceCreationExpression_redirect() { 961 test_instanceCreationExpression_redirect() async {
969 CompilationUnit compilationUnit = resolveSource(r''' 962 CompilationUnit compilationUnit = resolveSource(r'''
970 const foo = const A(); 963 const foo = const A();
971 class A { 964 class A {
972 const factory A() = B; 965 const factory A() = B;
973 } 966 }
974 class B implements A { 967 class B implements A {
975 const B(); 968 const B();
976 }'''); 969 }''');
977 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "B"); 970 _assertType(_evaluateTopLevelVariable(compilationUnit, "foo"), "B");
978 } 971 }
979 972
980 void test_instanceCreationExpression_redirect_cycle() { 973 test_instanceCreationExpression_redirect_cycle() async {
981 // It is an error to have a cycle in factory redirects; however, we need 974 // It is an error to have a cycle in factory redirects; however, we need
982 // to make sure that even if the error occurs, attempting to evaluate the 975 // to make sure that even if the error occurs, attempting to evaluate the
983 // constant will terminate. 976 // constant will terminate.
984 CompilationUnit compilationUnit = resolveSource(r''' 977 CompilationUnit compilationUnit = resolveSource(r'''
985 const foo = const A(); 978 const foo = const A();
986 class A { 979 class A {
987 const factory A() = A.b; 980 const factory A() = A.b;
988 const factory A.b() = A; 981 const factory A.b() = A;
989 }'''); 982 }''');
990 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); 983 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo"));
991 } 984 }
992 985
993 void test_instanceCreationExpression_redirect_external() { 986 test_instanceCreationExpression_redirect_external() async {
994 CompilationUnit compilationUnit = resolveSource(r''' 987 CompilationUnit compilationUnit = resolveSource(r'''
995 const foo = const A(); 988 const foo = const A();
996 class A { 989 class A {
997 external const factory A(); 990 external const factory A();
998 }'''); 991 }''');
999 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); 992 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo"));
1000 } 993 }
1001 994
1002 void test_instanceCreationExpression_redirect_nonConst() { 995 test_instanceCreationExpression_redirect_nonConst() async {
1003 // It is an error for a const factory constructor redirect to a non-const 996 // It is an error for a const factory constructor redirect to a non-const
1004 // constructor; however, we need to make sure that even if the error 997 // constructor; however, we need to make sure that even if the error
1005 // attempting to evaluate the constant won't cause a crash. 998 // attempting to evaluate the constant won't cause a crash.
1006 CompilationUnit compilationUnit = resolveSource(r''' 999 CompilationUnit compilationUnit = resolveSource(r'''
1007 const foo = const A(); 1000 const foo = const A();
1008 class A { 1001 class A {
1009 const factory A() = A.b; 1002 const factory A() = A.b;
1010 A.b(); 1003 A.b();
1011 }'''); 1004 }''');
1012 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo")); 1005 _assertValidUnknown(_evaluateTopLevelVariable(compilationUnit, "foo"));
1013 } 1006 }
1014 1007
1015 void test_instanceCreationExpression_redirectWithTypeParams() { 1008 test_instanceCreationExpression_redirectWithTypeParams() async {
1016 CompilationUnit compilationUnit = resolveSource(r''' 1009 CompilationUnit compilationUnit = resolveSource(r'''
1017 class A { 1010 class A {
1018 const factory A(var a) = B<int>; 1011 const factory A(var a) = B<int>;
1019 } 1012 }
1020 1013
1021 class B<T> implements A { 1014 class B<T> implements A {
1022 final T x; 1015 final T x;
1023 const B(this.x); 1016 const B(this.x);
1024 } 1017 }
1025 1018
1026 const A a = const A(10);'''); 1019 const A a = const A(10);''');
1027 EvaluationResultImpl result = 1020 EvaluationResultImpl result =
1028 _evaluateTopLevelVariable(compilationUnit, "a"); 1021 _evaluateTopLevelVariable(compilationUnit, "a");
1029 Map<String, DartObjectImpl> fields = _assertType(result, "B<int>"); 1022 Map<String, DartObjectImpl> fields = _assertType(result, "B<int>");
1030 expect(fields, hasLength(1)); 1023 expect(fields, hasLength(1));
1031 _assertIntField(fields, "x", 10); 1024 _assertIntField(fields, "x", 10);
1032 } 1025 }
1033 1026
1034 void test_instanceCreationExpression_redirectWithTypeSubstitution() { 1027 test_instanceCreationExpression_redirectWithTypeSubstitution() async {
1035 // To evaluate the redirection of A<int>, 1028 // To evaluate the redirection of A<int>,
1036 // A's template argument (T=int) must be substituted 1029 // A's template argument (T=int) must be substituted
1037 // into B's template argument (B<U> where U=T) to get B<int>. 1030 // into B's template argument (B<U> where U=T) to get B<int>.
1038 CompilationUnit compilationUnit = resolveSource(r''' 1031 CompilationUnit compilationUnit = resolveSource(r'''
1039 class A<T> { 1032 class A<T> {
1040 const factory A(var a) = B<T>; 1033 const factory A(var a) = B<T>;
1041 } 1034 }
1042 1035
1043 class B<U> implements A { 1036 class B<U> implements A {
1044 final U x; 1037 final U x;
1045 const B(this.x); 1038 const B(this.x);
1046 } 1039 }
1047 1040
1048 const A<int> a = const A<int>(10);'''); 1041 const A<int> a = const A<int>(10);''');
1049 EvaluationResultImpl result = 1042 EvaluationResultImpl result =
1050 _evaluateTopLevelVariable(compilationUnit, "a"); 1043 _evaluateTopLevelVariable(compilationUnit, "a");
1051 Map<String, DartObjectImpl> fields = _assertType(result, "B<int>"); 1044 Map<String, DartObjectImpl> fields = _assertType(result, "B<int>");
1052 expect(fields, hasLength(1)); 1045 expect(fields, hasLength(1));
1053 _assertIntField(fields, "x", 10); 1046 _assertIntField(fields, "x", 10);
1054 } 1047 }
1055 1048
1056 void test_instanceCreationExpression_symbol() { 1049 test_instanceCreationExpression_symbol() async {
1057 CompilationUnit compilationUnit = 1050 CompilationUnit compilationUnit =
1058 resolveSource("const foo = const Symbol('a');"); 1051 resolveSource("const foo = const Symbol('a');");
1059 EvaluationResultImpl evaluationResult = 1052 EvaluationResultImpl evaluationResult =
1060 _evaluateTopLevelVariable(compilationUnit, "foo"); 1053 _evaluateTopLevelVariable(compilationUnit, "foo");
1061 expect(evaluationResult.value, isNotNull); 1054 expect(evaluationResult.value, isNotNull);
1062 DartObjectImpl value = evaluationResult.value; 1055 DartObjectImpl value = evaluationResult.value;
1063 expect(value.type, typeProvider.symbolType); 1056 expect(value.type, typeProvider.symbolType);
1064 expect(value.toSymbolValue(), "a"); 1057 expect(value.toSymbolValue(), "a");
1065 } 1058 }
1066 1059
1067 void test_instanceCreationExpression_withSupertypeParams_explicit() { 1060 test_instanceCreationExpression_withSupertypeParams_explicit() async {
1068 _checkInstanceCreation_withSupertypeParams(true); 1061 _checkInstanceCreation_withSupertypeParams(true);
1069 } 1062 }
1070 1063
1071 void test_instanceCreationExpression_withSupertypeParams_implicit() { 1064 test_instanceCreationExpression_withSupertypeParams_implicit() async {
1072 _checkInstanceCreation_withSupertypeParams(false); 1065 _checkInstanceCreation_withSupertypeParams(false);
1073 } 1066 }
1074 1067
1075 void test_instanceCreationExpression_withTypeParams() { 1068 test_instanceCreationExpression_withTypeParams() async {
1076 CompilationUnit compilationUnit = resolveSource(r''' 1069 CompilationUnit compilationUnit = resolveSource(r'''
1077 class C<E> { 1070 class C<E> {
1078 const C(); 1071 const C();
1079 } 1072 }
1080 const c_int = const C<int>(); 1073 const c_int = const C<int>();
1081 const c_num = const C<num>();'''); 1074 const c_num = const C<num>();''');
1082 EvaluationResultImpl c_int = 1075 EvaluationResultImpl c_int =
1083 _evaluateTopLevelVariable(compilationUnit, "c_int"); 1076 _evaluateTopLevelVariable(compilationUnit, "c_int");
1084 _assertType(c_int, "C<int>"); 1077 _assertType(c_int, "C<int>");
1085 DartObjectImpl c_int_value = c_int.value; 1078 DartObjectImpl c_int_value = c_int.value;
1086 EvaluationResultImpl c_num = 1079 EvaluationResultImpl c_num =
1087 _evaluateTopLevelVariable(compilationUnit, "c_num"); 1080 _evaluateTopLevelVariable(compilationUnit, "c_num");
1088 _assertType(c_num, "C<num>"); 1081 _assertType(c_num, "C<num>");
1089 DartObjectImpl c_num_value = c_num.value; 1082 DartObjectImpl c_num_value = c_num.value;
1090 expect(c_int_value == c_num_value, isFalse); 1083 expect(c_int_value == c_num_value, isFalse);
1091 } 1084 }
1092 1085
1093 void test_isValidSymbol() { 1086 test_isValidSymbol() async {
1094 expect(ConstantEvaluationEngine.isValidPublicSymbol(""), isTrue); 1087 expect(ConstantEvaluationEngine.isValidPublicSymbol(""), isTrue);
1095 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo"), isTrue); 1088 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo"), isTrue);
1096 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.bar"), isTrue); 1089 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.bar"), isTrue);
1097 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo\$"), isTrue); 1090 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo\$"), isTrue);
1098 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo\$bar"), isTrue); 1091 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo\$bar"), isTrue);
1099 expect(ConstantEvaluationEngine.isValidPublicSymbol("iff"), isTrue); 1092 expect(ConstantEvaluationEngine.isValidPublicSymbol("iff"), isTrue);
1100 expect(ConstantEvaluationEngine.isValidPublicSymbol("gif"), isTrue); 1093 expect(ConstantEvaluationEngine.isValidPublicSymbol("gif"), isTrue);
1101 expect(ConstantEvaluationEngine.isValidPublicSymbol("if\$"), isTrue); 1094 expect(ConstantEvaluationEngine.isValidPublicSymbol("if\$"), isTrue);
1102 expect(ConstantEvaluationEngine.isValidPublicSymbol("\$if"), isTrue); 1095 expect(ConstantEvaluationEngine.isValidPublicSymbol("\$if"), isTrue);
1103 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo="), isTrue); 1096 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo="), isTrue);
1104 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.bar="), isTrue); 1097 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.bar="), isTrue);
1105 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.+"), isTrue); 1098 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.+"), isTrue);
1106 expect(ConstantEvaluationEngine.isValidPublicSymbol("void"), isTrue); 1099 expect(ConstantEvaluationEngine.isValidPublicSymbol("void"), isTrue);
1107 expect(ConstantEvaluationEngine.isValidPublicSymbol("_foo"), isFalse); 1100 expect(ConstantEvaluationEngine.isValidPublicSymbol("_foo"), isFalse);
1108 expect(ConstantEvaluationEngine.isValidPublicSymbol("_foo.bar"), isFalse); 1101 expect(ConstantEvaluationEngine.isValidPublicSymbol("_foo.bar"), isFalse);
1109 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo._bar"), isFalse); 1102 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo._bar"), isFalse);
1110 expect(ConstantEvaluationEngine.isValidPublicSymbol("if"), isFalse); 1103 expect(ConstantEvaluationEngine.isValidPublicSymbol("if"), isFalse);
1111 expect(ConstantEvaluationEngine.isValidPublicSymbol("if.foo"), isFalse); 1104 expect(ConstantEvaluationEngine.isValidPublicSymbol("if.foo"), isFalse);
1112 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.if"), isFalse); 1105 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.if"), isFalse);
1113 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo=.bar"), isFalse); 1106 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo=.bar"), isFalse);
1114 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo."), isFalse); 1107 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo."), isFalse);
1115 expect(ConstantEvaluationEngine.isValidPublicSymbol("+.foo"), isFalse); 1108 expect(ConstantEvaluationEngine.isValidPublicSymbol("+.foo"), isFalse);
1116 expect(ConstantEvaluationEngine.isValidPublicSymbol("void.foo"), isFalse); 1109 expect(ConstantEvaluationEngine.isValidPublicSymbol("void.foo"), isFalse);
1117 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.void"), isFalse); 1110 expect(ConstantEvaluationEngine.isValidPublicSymbol("foo.void"), isFalse);
1118 } 1111 }
1119 1112
1120 void test_length_of_improperly_typed_string_expression() { 1113 test_length_of_improperly_typed_string_expression() async {
1121 // Since type annotations are ignored in unchecked mode, the improper 1114 // Since type annotations are ignored in unchecked mode, the improper
1122 // types on s1 and s2 shouldn't prevent us from evaluating i to 1115 // types on s1 and s2 shouldn't prevent us from evaluating i to
1123 // 'alpha'.length. 1116 // 'alpha'.length.
1124 CompilationUnit compilationUnit = resolveSource(''' 1117 CompilationUnit compilationUnit = resolveSource('''
1125 const int s1 = 'alpha'; 1118 const int s1 = 'alpha';
1126 const int s2 = 'beta'; 1119 const int s2 = 'beta';
1127 const int i = (true ? s1 : s2).length; 1120 const int i = (true ? s1 : s2).length;
1128 '''); 1121 ''');
1129 ConstTopLevelVariableElementImpl element = 1122 ConstTopLevelVariableElementImpl element =
1130 findTopLevelDeclaration(compilationUnit, 'i').element; 1123 findTopLevelDeclaration(compilationUnit, 'i').element;
1131 EvaluationResultImpl result = element.evaluationResult; 1124 EvaluationResultImpl result = element.evaluationResult;
1132 expect(_assertValidInt(result), 5); 1125 expect(_assertValidInt(result), 5);
1133 } 1126 }
1134 1127
1135 void test_length_of_improperly_typed_string_identifier() { 1128 test_length_of_improperly_typed_string_identifier() async {
1136 // Since type annotations are ignored in unchecked mode, the improper type 1129 // Since type annotations are ignored in unchecked mode, the improper type
1137 // on s shouldn't prevent us from evaluating i to 'alpha'.length. 1130 // on s shouldn't prevent us from evaluating i to 'alpha'.length.
1138 CompilationUnit compilationUnit = resolveSource(''' 1131 CompilationUnit compilationUnit = resolveSource('''
1139 const int s = 'alpha'; 1132 const int s = 'alpha';
1140 const int i = s.length; 1133 const int i = s.length;
1141 '''); 1134 ''');
1142 ConstTopLevelVariableElementImpl element = 1135 ConstTopLevelVariableElementImpl element =
1143 findTopLevelDeclaration(compilationUnit, 'i').element; 1136 findTopLevelDeclaration(compilationUnit, 'i').element;
1144 EvaluationResultImpl result = element.evaluationResult; 1137 EvaluationResultImpl result = element.evaluationResult;
1145 expect(_assertValidInt(result), 5); 1138 expect(_assertValidInt(result), 5);
1146 } 1139 }
1147 1140
1148 void test_non_static_const_initialized_at_declaration() { 1141 test_non_static_const_initialized_at_declaration() async {
1149 // Even though non-static consts are not allowed by the language, we need 1142 // Even though non-static consts are not allowed by the language, we need
1150 // to handle them for error recovery purposes. 1143 // to handle them for error recovery purposes.
1151 CompilationUnit compilationUnit = resolveSource(''' 1144 CompilationUnit compilationUnit = resolveSource('''
1152 class A { 1145 class A {
1153 const int i = 123; 1146 const int i = 123;
1154 const A(); 1147 const A();
1155 } 1148 }
1156 1149
1157 const A a = const A(); 1150 const A a = const A();
1158 '''); 1151 ''');
1159 EvaluationResultImpl result = 1152 EvaluationResultImpl result =
1160 _evaluateTopLevelVariable(compilationUnit, 'a'); 1153 _evaluateTopLevelVariable(compilationUnit, 'a');
1161 Map<String, DartObjectImpl> fields = _assertType(result, "A"); 1154 Map<String, DartObjectImpl> fields = _assertType(result, "A");
1162 expect(fields, hasLength(1)); 1155 expect(fields, hasLength(1));
1163 _assertIntField(fields, "i", 123); 1156 _assertIntField(fields, "i", 123);
1164 } 1157 }
1165 1158
1166 void test_symbolLiteral_void() { 1159 test_symbolLiteral_void() async {
1167 CompilationUnit compilationUnit = 1160 CompilationUnit compilationUnit =
1168 resolveSource("const voidSymbol = #void;"); 1161 resolveSource("const voidSymbol = #void;");
1169 VariableDeclaration voidSymbol = 1162 VariableDeclaration voidSymbol =
1170 findTopLevelDeclaration(compilationUnit, "voidSymbol"); 1163 findTopLevelDeclaration(compilationUnit, "voidSymbol");
1171 EvaluationResultImpl voidSymbolResult = 1164 EvaluationResultImpl voidSymbolResult =
1172 (voidSymbol.element as VariableElementImpl).evaluationResult; 1165 (voidSymbol.element as VariableElementImpl).evaluationResult;
1173 DartObjectImpl value = voidSymbolResult.value; 1166 DartObjectImpl value = voidSymbolResult.value;
1174 expect(value.type, typeProvider.symbolType); 1167 expect(value.type, typeProvider.symbolType);
1175 expect(value.toSymbolValue(), "void"); 1168 expect(value.toSymbolValue(), "void");
1176 } 1169 }
(...skipping 12 matching lines...) Expand all
1189 DartObjectImpl field = fields[fieldName]; 1182 DartObjectImpl field = fields[fieldName];
1190 expect(field.type.name, "int"); 1183 expect(field.type.name, "int");
1191 expect(field.toIntValue(), expectedValue); 1184 expect(field.toIntValue(), expectedValue);
1192 } 1185 }
1193 1186
1194 void _assertNullField(Map<String, DartObjectImpl> fields, String fieldName) { 1187 void _assertNullField(Map<String, DartObjectImpl> fields, String fieldName) {
1195 DartObjectImpl field = fields[fieldName]; 1188 DartObjectImpl field = fields[fieldName];
1196 expect(field.isNull, isTrue); 1189 expect(field.isNull, isTrue);
1197 } 1190 }
1198 1191
1199 void _assertProperDependencies(String sourceText, 1192 Future<Null> _assertProperDependencies2(String sourceText,
1200 [List<ErrorCode> expectedErrorCodes = const <ErrorCode>[]]) { 1193 [List<ErrorCode> expectedErrorCodes = const <ErrorCode>[]]) async {
1201 Source source = addSource(sourceText); 1194 Source source = addSource(sourceText);
1202 LibraryElement element = resolve2(source); 1195 LibraryElement element = resolve2(source);
1203 CompilationUnit unit = 1196 CompilationUnit unit =
1204 analysisContext.resolveCompilationUnit(source, element); 1197 analysisContext.resolveCompilationUnit(source, element);
1205 expect(unit, isNotNull); 1198 expect(unit, isNotNull);
1206 ConstantValueComputer computer = _makeConstantValueComputer(); 1199 ConstantValueComputer computer = _makeConstantValueComputer();
1207 computer.add(unit); 1200 computer.add(unit);
1208 computer.computeValues(); 1201 computer.computeValues();
1209 assertErrors(source, expectedErrorCodes); 1202 await assertErrors(source, expectedErrorCodes);
1210 } 1203 }
1211 1204
1212 Map<String, DartObjectImpl> _assertType( 1205 Map<String, DartObjectImpl> _assertType(
1213 EvaluationResultImpl result, String typeName) { 1206 EvaluationResultImpl result, String typeName) {
1214 expect(result.value, isNotNull); 1207 expect(result.value, isNotNull);
1215 DartObjectImpl value = result.value; 1208 DartObjectImpl value = result.value;
1216 expect(value.type.displayName, typeName); 1209 expect(value.type.displayName, typeName);
1217 return value.fields; 1210 return value.fields;
1218 } 1211 }
1219 1212
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1400 expect(result.value, isNotNull); 1393 expect(result.value, isNotNull);
1401 } else { 1394 } else {
1402 expect(result.value, isNull); 1395 expect(result.value, isNull);
1403 } 1396 }
1404 } 1397 }
1405 } 1398 }
1406 } 1399 }
1407 1400
1408 @reflectiveTest 1401 @reflectiveTest
1409 class ConstantVisitorTest extends ResolverTestCase { 1402 class ConstantVisitorTest extends ResolverTestCase {
1410 void test_visitBinaryExpression_questionQuestion_notNull_notNull() { 1403 test_visitBinaryExpression_questionQuestion_notNull_notNull() async {
1411 Expression left = AstTestFactory.string2('a'); 1404 Expression left = AstTestFactory.string2('a');
1412 Expression right = AstTestFactory.string2('b'); 1405 Expression right = AstTestFactory.string2('b');
1413 Expression expression = AstTestFactory.binaryExpression( 1406 Expression expression = AstTestFactory.binaryExpression(
1414 left, TokenType.QUESTION_QUESTION, right); 1407 left, TokenType.QUESTION_QUESTION, right);
1415 1408
1416 GatheringErrorListener errorListener = new GatheringErrorListener(); 1409 GatheringErrorListener errorListener = new GatheringErrorListener();
1417 ErrorReporter errorReporter = 1410 ErrorReporter errorReporter =
1418 new ErrorReporter(errorListener, _dummySource()); 1411 new ErrorReporter(errorListener, _dummySource());
1419 DartObjectImpl result = _evaluate(expression, errorReporter); 1412 DartObjectImpl result = _evaluate(expression, errorReporter);
1420 expect(result, isNotNull); 1413 expect(result, isNotNull);
1421 expect(result.isNull, isFalse); 1414 expect(result.isNull, isFalse);
1422 expect(result.toStringValue(), 'a'); 1415 expect(result.toStringValue(), 'a');
1423 errorListener.assertNoErrors(); 1416 errorListener.assertNoErrors();
1424 } 1417 }
1425 1418
1426 void test_visitBinaryExpression_questionQuestion_null_notNull() { 1419 test_visitBinaryExpression_questionQuestion_null_notNull() async {
1427 Expression left = AstTestFactory.nullLiteral(); 1420 Expression left = AstTestFactory.nullLiteral();
1428 Expression right = AstTestFactory.string2('b'); 1421 Expression right = AstTestFactory.string2('b');
1429 Expression expression = AstTestFactory.binaryExpression( 1422 Expression expression = AstTestFactory.binaryExpression(
1430 left, TokenType.QUESTION_QUESTION, right); 1423 left, TokenType.QUESTION_QUESTION, right);
1431 1424
1432 GatheringErrorListener errorListener = new GatheringErrorListener(); 1425 GatheringErrorListener errorListener = new GatheringErrorListener();
1433 ErrorReporter errorReporter = 1426 ErrorReporter errorReporter =
1434 new ErrorReporter(errorListener, _dummySource()); 1427 new ErrorReporter(errorListener, _dummySource());
1435 DartObjectImpl result = _evaluate(expression, errorReporter); 1428 DartObjectImpl result = _evaluate(expression, errorReporter);
1436 expect(result, isNotNull); 1429 expect(result, isNotNull);
1437 expect(result.isNull, isFalse); 1430 expect(result.isNull, isFalse);
1438 expect(result.toStringValue(), 'b'); 1431 expect(result.toStringValue(), 'b');
1439 errorListener.assertNoErrors(); 1432 errorListener.assertNoErrors();
1440 } 1433 }
1441 1434
1442 void test_visitBinaryExpression_questionQuestion_null_null() { 1435 test_visitBinaryExpression_questionQuestion_null_null() async {
1443 Expression left = AstTestFactory.nullLiteral(); 1436 Expression left = AstTestFactory.nullLiteral();
1444 Expression right = AstTestFactory.nullLiteral(); 1437 Expression right = AstTestFactory.nullLiteral();
1445 Expression expression = AstTestFactory.binaryExpression( 1438 Expression expression = AstTestFactory.binaryExpression(
1446 left, TokenType.QUESTION_QUESTION, right); 1439 left, TokenType.QUESTION_QUESTION, right);
1447 1440
1448 GatheringErrorListener errorListener = new GatheringErrorListener(); 1441 GatheringErrorListener errorListener = new GatheringErrorListener();
1449 ErrorReporter errorReporter = 1442 ErrorReporter errorReporter =
1450 new ErrorReporter(errorListener, _dummySource()); 1443 new ErrorReporter(errorListener, _dummySource());
1451 DartObjectImpl result = _evaluate(expression, errorReporter); 1444 DartObjectImpl result = _evaluate(expression, errorReporter);
1452 expect(result, isNotNull); 1445 expect(result, isNotNull);
1453 expect(result.isNull, isTrue); 1446 expect(result.isNull, isTrue);
1454 errorListener.assertNoErrors(); 1447 errorListener.assertNoErrors();
1455 } 1448 }
1456 1449
1457 void test_visitConditionalExpression_false() { 1450 test_visitConditionalExpression_false() async {
1458 Expression thenExpression = AstTestFactory.integer(1); 1451 Expression thenExpression = AstTestFactory.integer(1);
1459 Expression elseExpression = AstTestFactory.integer(0); 1452 Expression elseExpression = AstTestFactory.integer(0);
1460 ConditionalExpression expression = AstTestFactory.conditionalExpression( 1453 ConditionalExpression expression = AstTestFactory.conditionalExpression(
1461 AstTestFactory.booleanLiteral(false), thenExpression, elseExpression); 1454 AstTestFactory.booleanLiteral(false), thenExpression, elseExpression);
1462 GatheringErrorListener errorListener = new GatheringErrorListener(); 1455 GatheringErrorListener errorListener = new GatheringErrorListener();
1463 ErrorReporter errorReporter = 1456 ErrorReporter errorReporter =
1464 new ErrorReporter(errorListener, _dummySource()); 1457 new ErrorReporter(errorListener, _dummySource());
1465 _assertValue(0, _evaluate(expression, errorReporter)); 1458 _assertValue(0, _evaluate(expression, errorReporter));
1466 errorListener.assertNoErrors(); 1459 errorListener.assertNoErrors();
1467 } 1460 }
1468 1461
1469 void test_visitConditionalExpression_nonBooleanCondition() { 1462 test_visitConditionalExpression_nonBooleanCondition() async {
1470 Expression thenExpression = AstTestFactory.integer(1); 1463 Expression thenExpression = AstTestFactory.integer(1);
1471 Expression elseExpression = AstTestFactory.integer(0); 1464 Expression elseExpression = AstTestFactory.integer(0);
1472 NullLiteral conditionExpression = AstTestFactory.nullLiteral(); 1465 NullLiteral conditionExpression = AstTestFactory.nullLiteral();
1473 ConditionalExpression expression = AstTestFactory.conditionalExpression( 1466 ConditionalExpression expression = AstTestFactory.conditionalExpression(
1474 conditionExpression, thenExpression, elseExpression); 1467 conditionExpression, thenExpression, elseExpression);
1475 GatheringErrorListener errorListener = new GatheringErrorListener(); 1468 GatheringErrorListener errorListener = new GatheringErrorListener();
1476 ErrorReporter errorReporter = 1469 ErrorReporter errorReporter =
1477 new ErrorReporter(errorListener, _dummySource()); 1470 new ErrorReporter(errorListener, _dummySource());
1478 DartObjectImpl result = _evaluate(expression, errorReporter); 1471 DartObjectImpl result = _evaluate(expression, errorReporter);
1479 expect(result, isNull); 1472 expect(result, isNull);
1480 errorListener 1473 errorListener
1481 .assertErrorsWithCodes([CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL]); 1474 .assertErrorsWithCodes([CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL]);
1482 } 1475 }
1483 1476
1484 void test_visitConditionalExpression_nonConstantElse() { 1477 test_visitConditionalExpression_nonConstantElse() async {
1485 Expression thenExpression = AstTestFactory.integer(1); 1478 Expression thenExpression = AstTestFactory.integer(1);
1486 Expression elseExpression = AstTestFactory.identifier3("x"); 1479 Expression elseExpression = AstTestFactory.identifier3("x");
1487 ConditionalExpression expression = AstTestFactory.conditionalExpression( 1480 ConditionalExpression expression = AstTestFactory.conditionalExpression(
1488 AstTestFactory.booleanLiteral(true), thenExpression, elseExpression); 1481 AstTestFactory.booleanLiteral(true), thenExpression, elseExpression);
1489 GatheringErrorListener errorListener = new GatheringErrorListener(); 1482 GatheringErrorListener errorListener = new GatheringErrorListener();
1490 ErrorReporter errorReporter = 1483 ErrorReporter errorReporter =
1491 new ErrorReporter(errorListener, _dummySource()); 1484 new ErrorReporter(errorListener, _dummySource());
1492 DartObjectImpl result = _evaluate(expression, errorReporter); 1485 DartObjectImpl result = _evaluate(expression, errorReporter);
1493 expect(result, isNull); 1486 expect(result, isNull);
1494 errorListener 1487 errorListener
1495 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]); 1488 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]);
1496 } 1489 }
1497 1490
1498 void test_visitConditionalExpression_nonConstantThen() { 1491 test_visitConditionalExpression_nonConstantThen() async {
1499 Expression thenExpression = AstTestFactory.identifier3("x"); 1492 Expression thenExpression = AstTestFactory.identifier3("x");
1500 Expression elseExpression = AstTestFactory.integer(0); 1493 Expression elseExpression = AstTestFactory.integer(0);
1501 ConditionalExpression expression = AstTestFactory.conditionalExpression( 1494 ConditionalExpression expression = AstTestFactory.conditionalExpression(
1502 AstTestFactory.booleanLiteral(true), thenExpression, elseExpression); 1495 AstTestFactory.booleanLiteral(true), thenExpression, elseExpression);
1503 GatheringErrorListener errorListener = new GatheringErrorListener(); 1496 GatheringErrorListener errorListener = new GatheringErrorListener();
1504 ErrorReporter errorReporter = 1497 ErrorReporter errorReporter =
1505 new ErrorReporter(errorListener, _dummySource()); 1498 new ErrorReporter(errorListener, _dummySource());
1506 DartObjectImpl result = _evaluate(expression, errorReporter); 1499 DartObjectImpl result = _evaluate(expression, errorReporter);
1507 expect(result, isNull); 1500 expect(result, isNull);
1508 errorListener 1501 errorListener
1509 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]); 1502 .assertErrorsWithCodes([CompileTimeErrorCode.INVALID_CONSTANT]);
1510 } 1503 }
1511 1504
1512 void test_visitConditionalExpression_true() { 1505 test_visitConditionalExpression_true() async {
1513 Expression thenExpression = AstTestFactory.integer(1); 1506 Expression thenExpression = AstTestFactory.integer(1);
1514 Expression elseExpression = AstTestFactory.integer(0); 1507 Expression elseExpression = AstTestFactory.integer(0);
1515 ConditionalExpression expression = AstTestFactory.conditionalExpression( 1508 ConditionalExpression expression = AstTestFactory.conditionalExpression(
1516 AstTestFactory.booleanLiteral(true), thenExpression, elseExpression); 1509 AstTestFactory.booleanLiteral(true), thenExpression, elseExpression);
1517 GatheringErrorListener errorListener = new GatheringErrorListener(); 1510 GatheringErrorListener errorListener = new GatheringErrorListener();
1518 ErrorReporter errorReporter = 1511 ErrorReporter errorReporter =
1519 new ErrorReporter(errorListener, _dummySource()); 1512 new ErrorReporter(errorListener, _dummySource());
1520 _assertValue(1, _evaluate(expression, errorReporter)); 1513 _assertValue(1, _evaluate(expression, errorReporter));
1521 errorListener.assertNoErrors(); 1514 errorListener.assertNoErrors();
1522 } 1515 }
1523 1516
1524 void test_visitSimpleIdentifier_className() { 1517 test_visitSimpleIdentifier_className() async {
1525 CompilationUnit compilationUnit = resolveSource(''' 1518 CompilationUnit compilationUnit = resolveSource('''
1526 const a = C; 1519 const a = C;
1527 class C {} 1520 class C {}
1528 '''); 1521 ''');
1529 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null); 1522 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null);
1530 expect(result.type, typeProvider.typeType); 1523 expect(result.type, typeProvider.typeType);
1531 expect(result.toTypeValue().name, 'C'); 1524 expect(result.toTypeValue().name, 'C');
1532 } 1525 }
1533 1526
1534 void test_visitSimpleIdentifier_dynamic() { 1527 test_visitSimpleIdentifier_dynamic() async {
1535 CompilationUnit compilationUnit = resolveSource(''' 1528 CompilationUnit compilationUnit = resolveSource('''
1536 const a = dynamic; 1529 const a = dynamic;
1537 '''); 1530 ''');
1538 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null); 1531 DartObjectImpl result = _evaluateConstant(compilationUnit, 'a', null);
1539 expect(result.type, typeProvider.typeType); 1532 expect(result.type, typeProvider.typeType);
1540 expect(result.toTypeValue(), typeProvider.dynamicType); 1533 expect(result.toTypeValue(), typeProvider.dynamicType);
1541 } 1534 }
1542 1535
1543 void test_visitSimpleIdentifier_inEnvironment() { 1536 test_visitSimpleIdentifier_inEnvironment() async {
1544 CompilationUnit compilationUnit = resolveSource(r''' 1537 CompilationUnit compilationUnit = resolveSource(r'''
1545 const a = b; 1538 const a = b;
1546 const b = 3;'''); 1539 const b = 3;''');
1547 Map<String, DartObjectImpl> environment = new Map<String, DartObjectImpl>(); 1540 Map<String, DartObjectImpl> environment = new Map<String, DartObjectImpl>();
1548 DartObjectImpl six = 1541 DartObjectImpl six =
1549 new DartObjectImpl(typeProvider.intType, new IntState(6)); 1542 new DartObjectImpl(typeProvider.intType, new IntState(6));
1550 environment["b"] = six; 1543 environment["b"] = six;
1551 _assertValue(6, _evaluateConstant(compilationUnit, "a", environment)); 1544 _assertValue(6, _evaluateConstant(compilationUnit, "a", environment));
1552 } 1545 }
1553 1546
1554 void test_visitSimpleIdentifier_notInEnvironment() { 1547 test_visitSimpleIdentifier_notInEnvironment() async {
1555 CompilationUnit compilationUnit = resolveSource(r''' 1548 CompilationUnit compilationUnit = resolveSource(r'''
1556 const a = b; 1549 const a = b;
1557 const b = 3;'''); 1550 const b = 3;''');
1558 Map<String, DartObjectImpl> environment = new Map<String, DartObjectImpl>(); 1551 Map<String, DartObjectImpl> environment = new Map<String, DartObjectImpl>();
1559 DartObjectImpl six = 1552 DartObjectImpl six =
1560 new DartObjectImpl(typeProvider.intType, new IntState(6)); 1553 new DartObjectImpl(typeProvider.intType, new IntState(6));
1561 environment["c"] = six; 1554 environment["c"] = six;
1562 _assertValue(3, _evaluateConstant(compilationUnit, "a", environment)); 1555 _assertValue(3, _evaluateConstant(compilationUnit, "a", environment));
1563 } 1556 }
1564 1557
1565 void test_visitSimpleIdentifier_withoutEnvironment() { 1558 test_visitSimpleIdentifier_withoutEnvironment() async {
1566 CompilationUnit compilationUnit = resolveSource(r''' 1559 CompilationUnit compilationUnit = resolveSource(r'''
1567 const a = b; 1560 const a = b;
1568 const b = 3;'''); 1561 const b = 3;''');
1569 _assertValue(3, _evaluateConstant(compilationUnit, "a", null)); 1562 _assertValue(3, _evaluateConstant(compilationUnit, "a", null));
1570 } 1563 }
1571 1564
1572 void _assertValue(int expectedValue, DartObjectImpl result) { 1565 void _assertValue(int expectedValue, DartObjectImpl result) {
1573 expect(result, isNotNull); 1566 expect(result, isNotNull);
1574 expect(result.type.name, "int"); 1567 expect(result.type.name, "int");
1575 expect(result.toIntValue(), expectedValue); 1568 expect(result.toIntValue(), expectedValue);
(...skipping 30 matching lines...) Expand all
1606 } 1599 }
1607 } 1600 }
1608 1601
1609 @reflectiveTest 1602 @reflectiveTest
1610 class StrongConstantValueComputerTest extends ConstantValueComputerTest { 1603 class StrongConstantValueComputerTest extends ConstantValueComputerTest {
1611 void setUp() { 1604 void setUp() {
1612 super.setUp(); 1605 super.setUp();
1613 resetWithOptions(new AnalysisOptionsImpl()..strongMode = true); 1606 resetWithOptions(new AnalysisOptionsImpl()..strongMode = true);
1614 } 1607 }
1615 } 1608 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698