OLD | NEW |
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 library analyzer.test.src.context.context_test; | 5 library analyzer.test.src.context.context_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
(...skipping 3754 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3765 expect(analysisCache.getValue(target, RESOLVED_UNIT1), same(unitA)); | 3765 expect(analysisCache.getValue(target, RESOLVED_UNIT1), same(unitA)); |
3766 expect(unitA.element, same(unitElementA)); | 3766 expect(unitA.element, same(unitElementA)); |
3767 expect(unitElementA.library, same(libraryElementA)); | 3767 expect(unitElementA.library, same(libraryElementA)); |
3768 } | 3768 } |
3769 // Analyze. | 3769 // Analyze. |
3770 _performPendingAnalysisTasks(); | 3770 _performPendingAnalysisTasks(); |
3771 expect(context.getErrors(a).errors, hasLength(0)); | 3771 expect(context.getErrors(a).errors, hasLength(0)); |
3772 expect(context.getErrors(b).errors, hasLength(0)); | 3772 expect(context.getErrors(b).errors, hasLength(0)); |
3773 } | 3773 } |
3774 | 3774 |
| 3775 void test_sequence_reorder_localFunctions() { |
| 3776 Source a = addSource( |
| 3777 '/a.dart', |
| 3778 r''' |
| 3779 f1() { |
| 3780 localFunction() { |
| 3781 const C = 1; |
| 3782 } |
| 3783 } |
| 3784 f2() {} |
| 3785 '''); |
| 3786 _performPendingAnalysisTasks(); |
| 3787 // Update a.dart: reorder functions. |
| 3788 // This should not fail with FrozenHashCodeException, because identifiers |
| 3789 // of local elements should be relative to the enclosing top-level elements. |
| 3790 context.setContents( |
| 3791 a, |
| 3792 r''' |
| 3793 f2() {} |
| 3794 f1() { |
| 3795 localFunction() { |
| 3796 const C = 1; |
| 3797 } |
| 3798 } |
| 3799 '''); |
| 3800 _assertValidForChangedLibrary(a); |
| 3801 _assertInvalid(a, LIBRARY_ERRORS_READY); |
| 3802 } |
| 3803 |
3775 void test_sequence_unitConstants() { | 3804 void test_sequence_unitConstants() { |
3776 Source a = addSource( | 3805 Source a = addSource( |
3777 '/a.dart', | 3806 '/a.dart', |
3778 r''' | 3807 r''' |
3779 const A = 1; | 3808 const A = 1; |
3780 const B = 2; | 3809 const B = 2; |
3781 const C = 3; | 3810 const C = 3; |
| 3811 foo() { |
| 3812 const V1 = 10; |
| 3813 } |
| 3814 bar() { |
| 3815 const V2 = 20; |
| 3816 } |
3782 '''); | 3817 '''); |
3783 _performPendingAnalysisTasks(); | 3818 _performPendingAnalysisTasks(); |
3784 LibrarySpecificUnit unitA = new LibrarySpecificUnit(a, a); | 3819 LibrarySpecificUnit unitA = new LibrarySpecificUnit(a, a); |
3785 expect(context.getResult(unitA, COMPILATION_UNIT_CONSTANTS), hasLength(3)); | 3820 List<ConstantEvaluationTarget> oldConstants = |
3786 // Update and | 3821 context.getResult(unitA, COMPILATION_UNIT_CONSTANTS); |
| 3822 expect(oldConstants, hasLength(5)); |
| 3823 ConstVariableElement oldA = _findConstVariable(oldConstants, 'A'); |
| 3824 ConstVariableElement oldB = _findConstVariable(oldConstants, 'B'); |
| 3825 ConstVariableElement oldC = _findConstVariable(oldConstants, 'C'); |
| 3826 ConstVariableElement oldV1 = _findConstVariable(oldConstants, 'V1'); |
| 3827 ConstVariableElement oldV2 = _findConstVariable(oldConstants, 'V2'); |
| 3828 expect(context.analysisCache.get(oldA), isNotNull); |
| 3829 expect(context.analysisCache.get(oldB), isNotNull); |
| 3830 expect(context.analysisCache.get(oldC), isNotNull); |
| 3831 expect(context.analysisCache.get(oldV1), isNotNull); |
| 3832 // Update and validate new constants. |
3787 context.setContents( | 3833 context.setContents( |
3788 a, | 3834 a, |
3789 r''' | 3835 r''' |
3790 const A = 1; | 3836 const A = 1; |
3791 const B = 2; | 3837 const B = 2; |
3792 const D = 4; | 3838 const D = 4; |
3793 main() { | 3839 foo() { |
3794 const V = 42; | 3840 const V1 = 10; |
| 3841 } |
| 3842 baz() { |
| 3843 const V3 = 30; |
3795 } | 3844 } |
3796 '''); | 3845 '''); |
3797 List<ConstantEvaluationTarget> constants = | 3846 List<ConstantEvaluationTarget> newConstants = |
3798 context.getResult(unitA, COMPILATION_UNIT_CONSTANTS); | 3847 context.getResult(unitA, COMPILATION_UNIT_CONSTANTS); |
3799 expect(constants, hasLength(4)); | 3848 expect(newConstants, hasLength(5)); |
| 3849 expect(newConstants, contains(same(oldA))); |
| 3850 expect(newConstants, contains(same(oldB))); |
| 3851 expect(newConstants, contains(same(oldV1))); |
| 3852 ConstVariableElement newD = _findConstVariable(newConstants, 'D'); |
| 3853 ConstVariableElement newV3 = _findConstVariable(newConstants, 'V3'); |
3800 // Perform analysis, compute constant values. | 3854 // Perform analysis, compute constant values. |
3801 _performPendingAnalysisTasks(); | 3855 _performPendingAnalysisTasks(); |
3802 // Validate const variable values. | 3856 // Validate const variable values. |
3803 Map<String, ConstVariableElement> constVariables = | 3857 expect(context.analysisCache.get(oldA), isNotNull); |
3804 <String, ConstVariableElement>{}; | 3858 expect(context.analysisCache.get(oldB), isNotNull); |
3805 constants.forEach((c) { | 3859 expect(context.analysisCache.get(oldV1), isNotNull); |
3806 if (c is ConstVariableElement) { | 3860 expect(context.analysisCache.get(oldC), isNull); |
3807 constVariables[c.name] = c; | 3861 expect(context.analysisCache.get(oldV2), isNull); |
3808 } | 3862 expect(context.analysisCache.get(newD), isNotNull); |
3809 }); | 3863 expect(context.analysisCache.get(newV3), isNotNull); |
3810 expect(constVariables['A'].evaluationResult.value.toIntValue(), 1); | 3864 expect(oldA.evaluationResult.value.toIntValue(), 1); |
3811 expect(constVariables['B'].evaluationResult.value.toIntValue(), 2); | 3865 expect(oldB.evaluationResult.value.toIntValue(), 2); |
3812 expect(constVariables['D'].evaluationResult.value.toIntValue(), 4); | 3866 expect(newD.evaluationResult.value.toIntValue(), 4); |
3813 expect(constVariables['V'].evaluationResult.value.toIntValue(), 42); | 3867 expect(oldV1.evaluationResult.value.toIntValue(), 10); |
| 3868 expect(newV3.evaluationResult.value.toIntValue(), 30); |
3814 } | 3869 } |
3815 | 3870 |
3816 void test_sequence_useAnyResolvedUnit() { | 3871 void test_sequence_useAnyResolvedUnit() { |
3817 Source a = addSource( | 3872 Source a = addSource( |
3818 '/a.dart', | 3873 '/a.dart', |
3819 r''' | 3874 r''' |
3820 class A {} | 3875 class A {} |
3821 class B {} | 3876 class B {} |
3822 '''); | 3877 '''); |
3823 Source b = addSource( | 3878 Source b = addSource( |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4282 var target = new LibrarySpecificUnit(library ?? unit, unit); | 4337 var target = new LibrarySpecificUnit(library ?? unit, unit); |
4283 bool foundLast = false; | 4338 bool foundLast = false; |
4284 for (ResultDescriptor<CompilationUnit> result in RESOLVED_UNIT_RESULTS) { | 4339 for (ResultDescriptor<CompilationUnit> result in RESOLVED_UNIT_RESULTS) { |
4285 if (!foundLast) { | 4340 if (!foundLast) { |
4286 _assertValid(target, result); | 4341 _assertValid(target, result); |
4287 } | 4342 } |
4288 foundLast = foundLast || result == last; | 4343 foundLast = foundLast || result == last; |
4289 } | 4344 } |
4290 } | 4345 } |
4291 | 4346 |
| 4347 ConstVariableElement _findConstVariable( |
| 4348 List<ConstantEvaluationTarget> constants, String name) { |
| 4349 return constants.singleWhere((c) { |
| 4350 return c is ConstVariableElement && c.name == name; |
| 4351 }); |
| 4352 } |
| 4353 |
4292 void _performPendingAnalysisTasks([int maxTasks = 512]) { | 4354 void _performPendingAnalysisTasks([int maxTasks = 512]) { |
4293 for (int i = 0; context.performAnalysisTask().hasMoreWork; i++) { | 4355 for (int i = 0; context.performAnalysisTask().hasMoreWork; i++) { |
4294 if (i > maxTasks) { | 4356 if (i > maxTasks) { |
4295 fail('Analysis did not terminate.'); | 4357 fail('Analysis did not terminate.'); |
4296 } | 4358 } |
4297 } | 4359 } |
4298 } | 4360 } |
4299 | 4361 |
4300 void _verifyTwoLibrariesAllValid( | 4362 void _verifyTwoLibrariesAllValid( |
4301 String firstCodeA, String secondCodeA, String codeB) { | 4363 String firstCodeA, String secondCodeA, String codeB) { |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4424 * Initialize the visitor. | 4486 * Initialize the visitor. |
4425 */ | 4487 */ |
4426 _ElementGatherer(); | 4488 _ElementGatherer(); |
4427 | 4489 |
4428 @override | 4490 @override |
4429 void visitElement(Element element) { | 4491 void visitElement(Element element) { |
4430 elements[element] = element; | 4492 elements[element] = element; |
4431 super.visitElement(element); | 4493 super.visitElement(element); |
4432 } | 4494 } |
4433 } | 4495 } |
OLD | NEW |