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

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

Issue 2625783002: Explicitly compute analysis results for sources to check errors. (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 'dart:async';
8
8 import 'package:analyzer/context/declared_variables.dart'; 9 import 'package:analyzer/context/declared_variables.dart';
9 import 'package:analyzer/dart/ast/ast.dart'; 10 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; 11 import 'package:analyzer/dart/ast/standard_resolution_map.dart';
11 import 'package:analyzer/dart/ast/token.dart'; 12 import 'package:analyzer/dart/ast/token.dart';
12 import 'package:analyzer/dart/element/element.dart'; 13 import 'package:analyzer/dart/element/element.dart';
13 import 'package:analyzer/error/error.dart'; 14 import 'package:analyzer/error/error.dart';
14 import 'package:analyzer/error/listener.dart'; 15 import 'package:analyzer/error/listener.dart';
15 import 'package:analyzer/src/dart/element/element.dart'; 16 import 'package:analyzer/src/dart/element/element.dart';
16 import 'package:analyzer/src/error/codes.dart'; 17 import 'package:analyzer/src/error/codes.dart';
17 import 'package:analyzer/src/generated/constant.dart'; 18 import 'package:analyzer/src/generated/constant.dart';
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 ConstantValueComputer computer = _makeConstantValueComputer(); 378 ConstantValueComputer computer = _makeConstantValueComputer();
378 computer.add(unit); 379 computer.add(unit);
379 computer.computeValues(); 380 computer.computeValues();
380 TopLevelVariableDeclaration declaration = unit.declarations 381 TopLevelVariableDeclaration declaration = unit.declarations
381 .firstWhere((member) => member is TopLevelVariableDeclaration); 382 .firstWhere((member) => member is TopLevelVariableDeclaration);
382 _validate(true, declaration.variables); 383 _validate(true, declaration.variables);
383 } 384 }
384 385
385 test_dependencyOnConstructor() async { 386 test_dependencyOnConstructor() async {
386 // x depends on "const A()" 387 // x depends on "const A()"
387 await _assertProperDependencies2(r''' 388 await _assertProperDependencies(r'''
388 class A { 389 class A {
389 const A(); 390 const A();
390 } 391 }
391 const x = const A();'''); 392 const x = const A();''');
392 } 393 }
393 394
394 test_dependencyOnConstructorArgument() async { 395 test_dependencyOnConstructorArgument() async {
395 // "const A(x)" depends on x 396 // "const A(x)" depends on x
396 await _assertProperDependencies2(r''' 397 await _assertProperDependencies(r'''
397 class A { 398 class A {
398 const A(this.next); 399 const A(this.next);
399 final A next; 400 final A next;
400 } 401 }
401 const A x = const A(null); 402 const A x = const A(null);
402 const A y = const A(x);'''); 403 const A y = const A(x);''');
403 } 404 }
404 405
405 test_dependencyOnConstructorArgument_unresolvedConstructor() async { 406 test_dependencyOnConstructorArgument_unresolvedConstructor() async {
406 // "const A.a(x)" depends on x even if the constructor A.a can't be found. 407 // "const A.a(x)" depends on x even if the constructor A.a can't be found.
407 await _assertProperDependencies2( 408 await _assertProperDependencies(
408 r''' 409 r'''
409 class A { 410 class A {
410 } 411 }
411 const int x = 1; 412 const int x = 1;
412 const A y = const A.a(x);''', 413 const A y = const A.a(x);''',
413 [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR]); 414 [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR]);
414 } 415 }
415 416
416 test_dependencyOnConstructorInitializer() async { 417 test_dependencyOnConstructorInitializer() async {
417 // "const A()" depends on x 418 // "const A()" depends on x
418 await _assertProperDependencies2(r''' 419 await _assertProperDependencies(r'''
419 const int x = 1; 420 const int x = 1;
420 class A { 421 class A {
421 const A() : v = x; 422 const A() : v = x;
422 final int v; 423 final int v;
423 }'''); 424 }''');
424 } 425 }
425 426
426 test_dependencyOnExplicitSuperConstructor() async { 427 test_dependencyOnExplicitSuperConstructor() async {
427 // b depends on B() depends on A() 428 // b depends on B() depends on A()
428 await _assertProperDependencies2(r''' 429 await _assertProperDependencies(r'''
429 class A { 430 class A {
430 const A(this.x); 431 const A(this.x);
431 final int x; 432 final int x;
432 } 433 }
433 class B extends A { 434 class B extends A {
434 const B() : super(5); 435 const B() : super(5);
435 } 436 }
436 const B b = const B();'''); 437 const B b = const B();''');
437 } 438 }
438 439
439 test_dependencyOnExplicitSuperConstructorParameters() async { 440 test_dependencyOnExplicitSuperConstructorParameters() async {
440 // b depends on B() depends on i 441 // b depends on B() depends on i
441 await _assertProperDependencies2(r''' 442 await _assertProperDependencies(r'''
442 class A { 443 class A {
443 const A(this.x); 444 const A(this.x);
444 final int x; 445 final int x;
445 } 446 }
446 class B extends A { 447 class B extends A {
447 const B() : super(i); 448 const B() : super(i);
448 } 449 }
449 const B b = const B(); 450 const B b = const B();
450 const int i = 5;'''); 451 const int i = 5;''');
451 } 452 }
452 453
453 test_dependencyOnFactoryRedirect() async { 454 test_dependencyOnFactoryRedirect() async {
454 // a depends on A.foo() depends on A.bar() 455 // a depends on A.foo() depends on A.bar()
455 await _assertProperDependencies2(r''' 456 await _assertProperDependencies(r'''
456 const A a = const A.foo(); 457 const A a = const A.foo();
457 class A { 458 class A {
458 factory const A.foo() = A.bar; 459 factory const A.foo() = A.bar;
459 const A.bar(); 460 const A.bar();
460 }'''); 461 }''');
461 } 462 }
462 463
463 test_dependencyOnFactoryRedirectWithTypeParams() async { 464 test_dependencyOnFactoryRedirectWithTypeParams() async {
464 await _assertProperDependencies2(r''' 465 await _assertProperDependencies(r'''
465 class A { 466 class A {
466 const factory A(var a) = B<int>; 467 const factory A(var a) = B<int>;
467 } 468 }
468 469
469 class B<T> implements A { 470 class B<T> implements A {
470 final T x; 471 final T x;
471 const B(this.x); 472 const B(this.x);
472 } 473 }
473 474
474 const A a = const A(10);'''); 475 const A a = const A(10);''');
475 } 476 }
476 477
477 test_dependencyOnImplicitSuperConstructor() async { 478 test_dependencyOnImplicitSuperConstructor() async {
478 // b depends on B() depends on A() 479 // b depends on B() depends on A()
479 await _assertProperDependencies2(r''' 480 await _assertProperDependencies(r'''
480 class A { 481 class A {
481 const A() : x = 5; 482 const A() : x = 5;
482 final int x; 483 final int x;
483 } 484 }
484 class B extends A { 485 class B extends A {
485 const B(); 486 const B();
486 } 487 }
487 const B b = const B();'''); 488 const B b = const B();''');
488 } 489 }
489 490
490 test_dependencyOnInitializedFinal() async { 491 test_dependencyOnInitializedFinal() async {
491 // a depends on A() depends on A.x 492 // a depends on A() depends on A.x
492 await _assertProperDependencies2(''' 493 await _assertProperDependencies('''
493 class A { 494 class A {
494 const A(); 495 const A();
495 final int x = 1; 496 final int x = 1;
496 } 497 }
497 const A a = const A(); 498 const A a = const A();
498 '''); 499 ''');
499 } 500 }
500 501
501 test_dependencyOnInitializedNonStaticConst() async { 502 test_dependencyOnInitializedNonStaticConst() async {
502 // Even though non-static consts are not allowed by the language, we need 503 // Even though non-static consts are not allowed by the language, we need
503 // to handle them for error recovery purposes. 504 // to handle them for error recovery purposes.
504 // a depends on A() depends on A.x 505 // a depends on A() depends on A.x
505 await _assertProperDependencies2( 506 await _assertProperDependencies(
506 ''' 507 '''
507 class A { 508 class A {
508 const A(); 509 const A();
509 const int x = 1; 510 const int x = 1;
510 } 511 }
511 const A a = const A(); 512 const A a = const A();
512 ''', 513 ''',
513 [CompileTimeErrorCode.CONST_INSTANCE_FIELD]); 514 [CompileTimeErrorCode.CONST_INSTANCE_FIELD]);
514 } 515 }
515 516
516 test_dependencyOnNonFactoryRedirect() async { 517 test_dependencyOnNonFactoryRedirect() async {
517 // a depends on A.foo() depends on A.bar() 518 // a depends on A.foo() depends on A.bar()
518 await _assertProperDependencies2(r''' 519 await _assertProperDependencies(r'''
519 const A a = const A.foo(); 520 const A a = const A.foo();
520 class A { 521 class A {
521 const A.foo() : this.bar(); 522 const A.foo() : this.bar();
522 const A.bar(); 523 const A.bar();
523 }'''); 524 }''');
524 } 525 }
525 526
526 test_dependencyOnNonFactoryRedirect_arg() async { 527 test_dependencyOnNonFactoryRedirect_arg() async {
527 // a depends on A.foo() depends on b 528 // a depends on A.foo() depends on b
528 await _assertProperDependencies2(r''' 529 await _assertProperDependencies(r'''
529 const A a = const A.foo(); 530 const A a = const A.foo();
530 const int b = 1; 531 const int b = 1;
531 class A { 532 class A {
532 const A.foo() : this.bar(b); 533 const A.foo() : this.bar(b);
533 const A.bar(x) : y = x; 534 const A.bar(x) : y = x;
534 final int y; 535 final int y;
535 }'''); 536 }''');
536 } 537 }
537 538
538 test_dependencyOnNonFactoryRedirect_defaultValue() async { 539 test_dependencyOnNonFactoryRedirect_defaultValue() async {
539 // a depends on A.foo() depends on A.bar() depends on b 540 // a depends on A.foo() depends on A.bar() depends on b
540 await _assertProperDependencies2(r''' 541 await _assertProperDependencies(r'''
541 const A a = const A.foo(); 542 const A a = const A.foo();
542 const int b = 1; 543 const int b = 1;
543 class A { 544 class A {
544 const A.foo() : this.bar(); 545 const A.foo() : this.bar();
545 const A.bar([x = b]) : y = x; 546 const A.bar([x = b]) : y = x;
546 final int y; 547 final int y;
547 }'''); 548 }''');
548 } 549 }
549 550
550 test_dependencyOnNonFactoryRedirect_toMissing() async { 551 test_dependencyOnNonFactoryRedirect_toMissing() async {
551 // a depends on A.foo() which depends on nothing, since A.bar() is 552 // a depends on A.foo() which depends on nothing, since A.bar() is
552 // missing. 553 // missing.
553 await _assertProperDependencies2( 554 await _assertProperDependencies(
554 r''' 555 r'''
555 const A a = const A.foo(); 556 const A a = const A.foo();
556 class A { 557 class A {
557 const A.foo() : this.bar(); 558 const A.foo() : this.bar();
558 }''', 559 }''',
559 [CompileTimeErrorCode.REDIRECT_GENERATIVE_TO_MISSING_CONSTRUCTOR]); 560 [CompileTimeErrorCode.REDIRECT_GENERATIVE_TO_MISSING_CONSTRUCTOR]);
560 } 561 }
561 562
562 test_dependencyOnNonFactoryRedirect_toNonConst() async { 563 test_dependencyOnNonFactoryRedirect_toNonConst() async {
563 // a depends on A.foo() which depends on nothing, since A.bar() is 564 // a depends on A.foo() which depends on nothing, since A.bar() is
564 // non-const. 565 // non-const.
565 await _assertProperDependencies2(r''' 566 await _assertProperDependencies(r'''
566 const A a = const A.foo(); 567 const A a = const A.foo();
567 class A { 568 class A {
568 const A.foo() : this.bar(); 569 const A.foo() : this.bar();
569 A.bar(); 570 A.bar();
570 }'''); 571 }''');
571 } 572 }
572 573
573 test_dependencyOnNonFactoryRedirect_unnamed() async { 574 test_dependencyOnNonFactoryRedirect_unnamed() async {
574 // a depends on A.foo() depends on A() 575 // a depends on A.foo() depends on A()
575 await _assertProperDependencies2(r''' 576 await _assertProperDependencies(r'''
576 const A a = const A.foo(); 577 const A a = const A.foo();
577 class A { 578 class A {
578 const A.foo() : this(); 579 const A.foo() : this();
579 const A(); 580 const A();
580 }'''); 581 }''');
581 } 582 }
582 583
583 test_dependencyOnOptionalParameterDefault() async { 584 test_dependencyOnOptionalParameterDefault() async {
584 // a depends on A() depends on B() 585 // a depends on A() depends on B()
585 await _assertProperDependencies2(r''' 586 await _assertProperDependencies(r'''
586 class A { 587 class A {
587 const A([x = const B()]) : b = x; 588 const A([x = const B()]) : b = x;
588 final B b; 589 final B b;
589 } 590 }
590 class B { 591 class B {
591 const B(); 592 const B();
592 } 593 }
593 const A a = const A();'''); 594 const A a = const A();''');
594 } 595 }
595 596
596 test_dependencyOnVariable() async { 597 test_dependencyOnVariable() async {
597 // x depends on y 598 // x depends on y
598 await _assertProperDependencies2(r''' 599 await _assertProperDependencies(r'''
599 const x = y + 1; 600 const x = y + 1;
600 const y = 2;'''); 601 const y = 2;''');
601 } 602 }
602 603
603 test_final_initialized_at_declaration() async { 604 test_final_initialized_at_declaration() async {
604 CompilationUnit compilationUnit = resolveSource(''' 605 CompilationUnit compilationUnit = resolveSource('''
605 class A { 606 class A {
606 final int i = 123; 607 final int i = 123;
607 const A(); 608 const A();
608 } 609 }
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
1182 DartObjectImpl field = fields[fieldName]; 1183 DartObjectImpl field = fields[fieldName];
1183 expect(field.type.name, "int"); 1184 expect(field.type.name, "int");
1184 expect(field.toIntValue(), expectedValue); 1185 expect(field.toIntValue(), expectedValue);
1185 } 1186 }
1186 1187
1187 void _assertNullField(Map<String, DartObjectImpl> fields, String fieldName) { 1188 void _assertNullField(Map<String, DartObjectImpl> fields, String fieldName) {
1188 DartObjectImpl field = fields[fieldName]; 1189 DartObjectImpl field = fields[fieldName];
1189 expect(field.isNull, isTrue); 1190 expect(field.isNull, isTrue);
1190 } 1191 }
1191 1192
1192 Future<Null> _assertProperDependencies2(String sourceText, 1193 Future<Null> _assertProperDependencies(String sourceText,
1193 [List<ErrorCode> expectedErrorCodes = const <ErrorCode>[]]) async { 1194 [List<ErrorCode> expectedErrorCodes = const <ErrorCode>[]]) async {
1194 Source source = addSource(sourceText); 1195 Source source = addSource(sourceText);
1195 LibraryElement element = resolve2(source); 1196 LibraryElement element = resolve2(source);
1196 CompilationUnit unit = 1197 CompilationUnit unit =
1197 analysisContext.resolveCompilationUnit(source, element); 1198 analysisContext.resolveCompilationUnit(source, element);
1198 expect(unit, isNotNull); 1199 expect(unit, isNotNull);
1199 ConstantValueComputer computer = _makeConstantValueComputer(); 1200 ConstantValueComputer computer = _makeConstantValueComputer();
1200 computer.add(unit); 1201 computer.add(unit);
1201 computer.computeValues(); 1202 computer.computeValues();
1203 await computeAnalysisResult(source);
1202 await assertErrors(source, expectedErrorCodes); 1204 await assertErrors(source, expectedErrorCodes);
1203 } 1205 }
1204 1206
1205 Map<String, DartObjectImpl> _assertType( 1207 Map<String, DartObjectImpl> _assertType(
1206 EvaluationResultImpl result, String typeName) { 1208 EvaluationResultImpl result, String typeName) {
1207 expect(result.value, isNotNull); 1209 expect(result.value, isNotNull);
1208 DartObjectImpl value = result.value; 1210 DartObjectImpl value = result.value;
1209 expect(value.type.displayName, typeName); 1211 expect(value.type.displayName, typeName);
1210 return value.fields; 1212 return value.fields;
1211 } 1213 }
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
1599 } 1601 }
1600 } 1602 }
1601 1603
1602 @reflectiveTest 1604 @reflectiveTest
1603 class StrongConstantValueComputerTest extends ConstantValueComputerTest { 1605 class StrongConstantValueComputerTest extends ConstantValueComputerTest {
1604 void setUp() { 1606 void setUp() {
1605 super.setUp(); 1607 super.setUp();
1606 resetWithOptions(new AnalysisOptionsImpl()..strongMode = true); 1608 resetWithOptions(new AnalysisOptionsImpl()..strongMode = true);
1607 } 1609 }
1608 } 1610 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698