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

Side by Side Diff: pkg/analyzer/test/generated/all_the_rest_test.dart

Issue 772133002: Evaluate annotations using ConstantValueComputer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years 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 | Annotate | Revision Log
« no previous file with comments | « pkg/analyzer/lib/src/generated/element.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library engine.all_the_rest_test; 8 library engine.all_the_rest_test;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
(...skipping 3361 matching lines...) Expand 10 before | Expand all | Expand 10 after
3372 source, 3372 source,
3373 (analysisContext as AnalysisContextImpl).typeProvider); 3373 (analysisContext as AnalysisContextImpl).typeProvider);
3374 return evaluator.evaluate(variables[0].initializer); 3374 return evaluator.evaluate(variables[0].initializer);
3375 } 3375 }
3376 } 3376 }
3377 3377
3378 3378
3379 class ConstantFinderTest extends EngineTestCase { 3379 class ConstantFinderTest extends EngineTestCase {
3380 AstNode _node; 3380 AstNode _node;
3381 3381
3382 /**
3383 * Test an annotation that consists solely of an identifier (and hence
3384 * represents a reference to a compile-time constant variable).
3385 */
3386 void test_visitAnnotation_constantVariable() {
3387 _node = AstFactory.annotation(AstFactory.identifier3('x'));
3388 expect(_findAnnotations(), contains(_node));
3389 }
3390
3391 /**
3392 * Test an annotation that represents the invocation of a constant
3393 * constructor.
3394 */
3395 void test_visitAnnotation_invocation() {
3396 _node = AstFactory.annotation2(
3397 AstFactory.identifier3('A'),
3398 null,
3399 AstFactory.argumentList());
3400 expect(_findAnnotations(), contains(_node));
3401 }
3402
3382 void test_visitConstructorDeclaration_const() { 3403 void test_visitConstructorDeclaration_const() {
3383 ConstructorElement element = _setupConstructorDeclaration("A", true); 3404 ConstructorElement element = _setupConstructorDeclaration("A", true);
3384 expect(_findConstantDeclarations()[element], same(_node)); 3405 expect(_findConstantDeclarations()[element], same(_node));
3385 } 3406 }
3386 3407
3387 void test_visitConstructorDeclaration_nonConst() { 3408 void test_visitConstructorDeclaration_nonConst() {
3388 _setupConstructorDeclaration("A", false); 3409 _setupConstructorDeclaration("A", false);
3389 expect(_findConstantDeclarations().isEmpty, isTrue); 3410 expect(_findConstantDeclarations().isEmpty, isTrue);
3390 } 3411 }
3391 3412
(...skipping 15 matching lines...) Expand all
3407 void test_visitVariableDeclaration_noInitializer() { 3428 void test_visitVariableDeclaration_noInitializer() {
3408 _setupVariableDeclaration("v", true, false); 3429 _setupVariableDeclaration("v", true, false);
3409 expect(_findVariableDeclarations().isEmpty, isTrue); 3430 expect(_findVariableDeclarations().isEmpty, isTrue);
3410 } 3431 }
3411 3432
3412 void test_visitVariableDeclaration_nonConst() { 3433 void test_visitVariableDeclaration_nonConst() {
3413 _setupVariableDeclaration("v", false, true); 3434 _setupVariableDeclaration("v", false, true);
3414 expect(_findVariableDeclarations().isEmpty, isTrue); 3435 expect(_findVariableDeclarations().isEmpty, isTrue);
3415 } 3436 }
3416 3437
3438 List<Annotation> _findAnnotations() {
3439 ConstantFinder finder = new ConstantFinder();
3440 _node.accept(finder);
3441 List<Annotation> annotations = finder.annotations;
3442 expect(annotations, isNotNull);
3443 return annotations;
3444 }
3445
3417 Map<ConstructorElement, ConstructorDeclaration> _findConstantDeclarations() { 3446 Map<ConstructorElement, ConstructorDeclaration> _findConstantDeclarations() {
3418 ConstantFinder finder = new ConstantFinder(); 3447 ConstantFinder finder = new ConstantFinder();
3419 _node.accept(finder); 3448 _node.accept(finder);
3420 Map<ConstructorElement, ConstructorDeclaration> constructorMap = 3449 Map<ConstructorElement, ConstructorDeclaration> constructorMap =
3421 finder.constructorMap; 3450 finder.constructorMap;
3422 expect(constructorMap, isNotNull); 3451 expect(constructorMap, isNotNull);
3423 return constructorMap; 3452 return constructorMap;
3424 } 3453 }
3425 3454
3426 List<InstanceCreationExpression> _findConstructorInvocations() { 3455 List<InstanceCreationExpression> _findConstructorInvocations() {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
3476 AstFactory.variableDeclarationList2( 3505 AstFactory.variableDeclarationList2(
3477 isConst ? Keyword.CONST : null, 3506 isConst ? Keyword.CONST : null,
3478 [variableDeclaration]); 3507 [variableDeclaration]);
3479 _node = variableDeclaration; 3508 _node = variableDeclaration;
3480 return element; 3509 return element;
3481 } 3510 }
3482 } 3511 }
3483 3512
3484 3513
3485 class ConstantValueComputerTest extends ResolverTestCase { 3514 class ConstantValueComputerTest extends ResolverTestCase {
3515 void test_annotation_constConstructor() {
3516 CompilationUnit compilationUnit = resolveSource(r'''
3517 class A {
3518 final int i;
3519 const A(this.i);
3520 }
3521
3522 class C {
3523 @A(5)
3524 f() {}
3525 }
3526 ''');
3527 EvaluationResultImpl result =
3528 _evaluateAnnotation(compilationUnit, "C", "f");
3529 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A');
3530 _assertIntField(annotationFields, 'i', 5);
3531 }
3532
3533 void test_annotation_constConstructor_named() {
3534 CompilationUnit compilationUnit = resolveSource(r'''
3535 class A {
3536 final int i;
3537 const A.named(this.i);
3538 }
3539
3540 class C {
3541 @A.named(5)
3542 f() {}
3543 }
3544 ''');
3545 EvaluationResultImpl result =
3546 _evaluateAnnotation(compilationUnit, "C", "f");
3547 Map<String, DartObjectImpl> annotationFields = _assertType(result, 'A');
3548 _assertIntField(annotationFields, 'i', 5);
3549 }
3550
3551 void test_annotation_constConstructor_noArgs() {
3552 // Failing to pass arguments to an annotation which is a constant
3553 // constructor is illegal, but shouldn't crash analysis.
3554 CompilationUnit compilationUnit = resolveSource(r'''
3555 class A {
3556 final int i;
3557 const A(this.i);
3558 }
3559
3560 class C {
3561 @A
3562 f() {}
3563 }
3564 ''');
3565 _evaluateAnnotation(compilationUnit, "C", "f");
3566 }
3567
3568 void test_annotation_constConstructor_noArgs_named() {
3569 // Failing to pass arguments to an annotation which is a constant
3570 // constructor is illegal, but shouldn't crash analysis.
3571 CompilationUnit compilationUnit = resolveSource(r'''
3572 class A {
3573 final int i;
3574 const A.named(this.i);
3575 }
3576
3577 class C {
3578 @A.named
3579 f() {}
3580 }
3581 ''');
3582 _evaluateAnnotation(compilationUnit, "C", "f");
3583 }
3584
3585 void test_annotation_nonConstConstructor() {
3586 // Calling a non-const constructor from an annotation that is illegal, but
3587 // shouldn't crash analysis.
3588 CompilationUnit compilationUnit = resolveSource(r'''
3589 class A {
3590 final int i;
3591 A(this.i);
3592 }
3593
3594 class C {
3595 @A(5)
3596 f() {}
3597 }
3598 ''');
3599 _evaluateAnnotation(compilationUnit, "C", "f");
3600 }
3601
3602 void test_annotation_staticConst() {
3603 CompilationUnit compilationUnit = resolveSource(r'''
3604 class C {
3605 static const int i = 5;
3606
3607 @i
3608 f() {}
3609 }
3610 ''');
3611 EvaluationResultImpl result =
3612 _evaluateAnnotation(compilationUnit, "C", "f");
3613 expect(_assertValidInt(result), 5);
3614 }
3615
3616 void test_annotation_staticConst_args() {
3617 // Applying arguments to an annotation that is a static const is
3618 // illegal, but shouldn't crash analysis.
3619 CompilationUnit compilationUnit = resolveSource(r'''
3620 class C {
3621 static const int i = 5;
3622
3623 @i(1)
3624 f() {}
3625 }
3626 ''');
3627 _evaluateAnnotation(compilationUnit, "C", "f");
3628 }
3629
3630 void test_annotation_staticConst_otherClass() {
3631 CompilationUnit compilationUnit = resolveSource(r'''
3632 class A {
3633 static const int i = 5;
3634 }
3635
3636 class C {
3637 @A.i
3638 f() {}
3639 }
3640 ''');
3641 EvaluationResultImpl result =
3642 _evaluateAnnotation(compilationUnit, "C", "f");
3643 expect(_assertValidInt(result), 5);
3644 }
3645
3646 void test_annotation_staticConst_otherClass_args() {
3647 // Applying arguments to an annotation that is a static const is
3648 // illegal, but shouldn't crash analysis.
3649 CompilationUnit compilationUnit = resolveSource(r'''
3650 class A {
3651 static const int i = 5;
3652 }
3653
3654 class C {
3655 @A.i(1)
3656 f() {}
3657 }
3658 ''');
3659 _evaluateAnnotation(compilationUnit, "C", "f");
3660 }
3661
3662 void test_annotation_toplevelVariable() {
3663 CompilationUnit compilationUnit = resolveSource(r'''
3664 const int i = 5;
3665 class C {
3666 @i
3667 f() {}
3668 }
3669 ''');
3670 EvaluationResultImpl result =
3671 _evaluateAnnotation(compilationUnit, "C", "f");
3672 expect(_assertValidInt(result), 5);
3673 }
3674
3675 void test_annotation_toplevelVariable_args() {
3676 // Applying arguments to an annotation that is a toplevel variable is
3677 // illegal, but shouldn't crash analysis.
3678 CompilationUnit compilationUnit = resolveSource(r'''
3679 const int i = 5;
3680 class C {
3681 @i(1)
3682 f() {}
3683 }
3684 ''');
3685 _evaluateAnnotation(compilationUnit, "C", "f");
3686 }
3687
3486 void test_computeValues_cycle() { 3688 void test_computeValues_cycle() {
3487 TestLogger logger = new TestLogger(); 3689 TestLogger logger = new TestLogger();
3488 AnalysisEngine.instance.logger = logger; 3690 AnalysisEngine.instance.logger = logger;
3489 Source librarySource = addSource(r''' 3691 Source librarySource = addSource(r'''
3490 const int a = c; 3692 const int a = c;
3491 const int b = a; 3693 const int b = a;
3492 const int c = b;'''); 3694 const int c = b;''');
3493 LibraryElement libraryElement = resolve(librarySource); 3695 LibraryElement libraryElement = resolve(librarySource);
3494 CompilationUnit unit = 3696 CompilationUnit unit =
3495 analysisContext.resolveCompilationUnit(librarySource, libraryElement); 3697 analysisContext.resolveCompilationUnit(librarySource, libraryElement);
(...skipping 965 matching lines...) Expand 10 before | Expand all | Expand 10 after
4461 } else { 4663 } else {
4462 _assertNullField(fieldsOfX, fieldName); 4664 _assertNullField(fieldsOfX, fieldName);
4463 } 4665 }
4464 EvaluationResultImpl y = 4666 EvaluationResultImpl y =
4465 _evaluateInstanceCreationExpression(compilationUnit, "y"); 4667 _evaluateInstanceCreationExpression(compilationUnit, "y");
4466 Map<String, DartObjectImpl> fieldsOfY = _assertType(y, "A"); 4668 Map<String, DartObjectImpl> fieldsOfY = _assertType(y, "A");
4467 expect(fieldsOfY, hasLength(1)); 4669 expect(fieldsOfY, hasLength(1));
4468 _assertIntField(fieldsOfY, fieldName, 10); 4670 _assertIntField(fieldsOfY, fieldName, 10);
4469 } 4671 }
4470 4672
4673 /**
4674 * Search [compilationUnit] for a class named [className], containing a
4675 * method [methodName], with exactly one annotation. Return the constant
4676 * value of the annotation.
4677 */
4678 EvaluationResultImpl _evaluateAnnotation(CompilationUnit compilationUnit,
4679 String className, String memberName) {
4680 for (CompilationUnitMember member in compilationUnit.declarations) {
4681 if (member is ClassDeclaration && member.name.name == className) {
4682 for (ClassMember classMember in member.members) {
4683 if (classMember is MethodDeclaration &&
4684 classMember.name.name == memberName) {
4685 expect(classMember.metadata, hasLength(1));
4686 ElementAnnotationImpl elementAnnotation =
4687 classMember.metadata[0].elementAnnotation;
4688 return elementAnnotation.evaluationResult;
4689 }
4690 }
4691 }
4692 }
4693 fail('Class member not found');
4694 return null;
4695 }
4696
4471 EvaluationResultImpl 4697 EvaluationResultImpl
4472 _evaluateInstanceCreationExpression(CompilationUnit compilationUnit, 4698 _evaluateInstanceCreationExpression(CompilationUnit compilationUnit,
4473 String name) { 4699 String name) {
4474 Expression expression = 4700 Expression expression =
4475 findTopLevelConstantExpression(compilationUnit, name); 4701 findTopLevelConstantExpression(compilationUnit, name);
4476 return (expression as InstanceCreationExpression).evaluationResult; 4702 return (expression as InstanceCreationExpression).evaluationResult;
4477 } 4703 }
4478 4704
4479 ConstantValueComputer _makeConstantValueComputer() { 4705 ConstantValueComputer _makeConstantValueComputer() {
4480 return new ValidatingConstantValueComputer( 4706 return new ValidatingConstantValueComputer(
(...skipping 6664 matching lines...) Expand 10 before | Expand all | Expand 10 after
11145 } else { 11371 } else {
11146 expect(scriptSource, isNotNull, reason: "script $scriptIndex"); 11372 expect(scriptSource, isNotNull, reason: "script $scriptIndex");
11147 String actualExternalScriptName = scriptSource.shortName; 11373 String actualExternalScriptName = scriptSource.shortName;
11148 expect( 11374 expect(
11149 actualExternalScriptName, 11375 actualExternalScriptName,
11150 _expectedExternalScriptName, 11376 _expectedExternalScriptName,
11151 reason: "script $scriptIndex"); 11377 reason: "script $scriptIndex");
11152 } 11378 }
11153 } 11379 }
11154 } 11380 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/element.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698