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

Side by Side Diff: pkg/analyzer/test/src/task/dart_test.dart

Issue 1320923003: Implement a task to compute the dependencies between static variables (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address comments Created 5 years, 3 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) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 test.src.task.dart_test; 5 library test.src.task.dart_test;
6 6
7 import 'package:analyzer/src/context/cache.dart'; 7 import 'package:analyzer/src/context/cache.dart';
8 import 'package:analyzer/src/generated/ast.dart'; 8 import 'package:analyzer/src/generated/ast.dart';
9 import 'package:analyzer/src/generated/constant.dart'; 9 import 'package:analyzer/src/generated/constant.dart';
10 import 'package:analyzer/src/generated/element.dart'; 10 import 'package:analyzer/src/generated/element.dart';
(...skipping 22 matching lines...) Expand all
33 runReflectiveTests(BuildDirectiveElementsTaskTest); 33 runReflectiveTests(BuildDirectiveElementsTaskTest);
34 runReflectiveTests(BuildEnumMemberElementsTaskTest); 34 runReflectiveTests(BuildEnumMemberElementsTaskTest);
35 runReflectiveTests(BuildSourceExportClosureTaskTest); 35 runReflectiveTests(BuildSourceExportClosureTaskTest);
36 runReflectiveTests(BuildSourceImportExportClosureTaskTest); 36 runReflectiveTests(BuildSourceImportExportClosureTaskTest);
37 runReflectiveTests(BuildExportNamespaceTaskTest); 37 runReflectiveTests(BuildExportNamespaceTaskTest);
38 runReflectiveTests(BuildLibraryElementTaskTest); 38 runReflectiveTests(BuildLibraryElementTaskTest);
39 runReflectiveTests(BuildPublicNamespaceTaskTest); 39 runReflectiveTests(BuildPublicNamespaceTaskTest);
40 runReflectiveTests(BuildTypeProviderTaskTest); 40 runReflectiveTests(BuildTypeProviderTaskTest);
41 runReflectiveTests(ComputeConstantDependenciesTaskTest); 41 runReflectiveTests(ComputeConstantDependenciesTaskTest);
42 runReflectiveTests(ComputeConstantValueTaskTest); 42 runReflectiveTests(ComputeConstantValueTaskTest);
43 runReflectiveTests(ComputeInferableStaticVariableDependenciesTaskTest);
43 runReflectiveTests(ContainingLibrariesTaskTest); 44 runReflectiveTests(ContainingLibrariesTaskTest);
44 runReflectiveTests(DartErrorsTaskTest); 45 runReflectiveTests(DartErrorsTaskTest);
45 runReflectiveTests(EvaluateUnitConstantsTaskTest); 46 runReflectiveTests(EvaluateUnitConstantsTaskTest);
46 runReflectiveTests(GatherUsedImportedElementsTaskTest); 47 runReflectiveTests(GatherUsedImportedElementsTaskTest);
47 runReflectiveTests(GatherUsedLocalElementsTaskTest); 48 runReflectiveTests(GatherUsedLocalElementsTaskTest);
48 runReflectiveTests(GenerateHintsTaskTest); 49 runReflectiveTests(GenerateHintsTaskTest);
49 runReflectiveTests(LibraryErrorsReadyTaskTest); 50 runReflectiveTests(LibraryErrorsReadyTaskTest);
50 runReflectiveTests(LibraryUnitErrorsTaskTest); 51 runReflectiveTests(LibraryUnitErrorsTaskTest);
51 runReflectiveTests(ParseDartTaskTest); 52 runReflectiveTests(ParseDartTaskTest);
52 runReflectiveTests(PartiallyResolveReferencesTaskTest); 53 runReflectiveTests(PartiallyResolveReferencesTaskTest);
53 runReflectiveTests(ResolveUnitTypeNamesTaskTest); 54 runReflectiveTests(ResolveUnitTypeNamesTaskTest);
54 runReflectiveTests(ResolveLibraryTypeNamesTaskTest); 55 runReflectiveTests(ResolveLibraryTypeNamesTaskTest);
55 runReflectiveTests(ResolveReferencesTaskTest); 56 runReflectiveTests(ResolveReferencesTaskTest);
56 runReflectiveTests(ResolveVariableReferencesTaskTest); 57 runReflectiveTests(ResolveVariableReferencesTaskTest);
57 runReflectiveTests(ScanDartTaskTest); 58 runReflectiveTests(ScanDartTaskTest);
58 runReflectiveTests(VerifyUnitTaskTest); 59 runReflectiveTests(VerifyUnitTaskTest);
59 } 60 }
60 61
61 @reflectiveTest 62 @reflectiveTest
62 class BuildCompilationUnitElementTaskTest extends _AbstractDartTaskTest { 63 class BuildCompilationUnitElementTaskTest extends _AbstractDartTaskTest {
63 Source source; 64 Source source;
64 LibrarySpecificUnit target; 65 LibrarySpecificUnit target;
65 66
66 /**
67 * Enable strong mode in the current analysis context.
68 */
69 void enableStrongMode() {
70 AnalysisOptionsImpl options = context.analysisOptions;
71 options.strongMode = true;
72 context.analysisOptions = options;
73 }
74
75 test_buildInputs() { 67 test_buildInputs() {
76 LibrarySpecificUnit target = 68 LibrarySpecificUnit target =
77 new LibrarySpecificUnit(emptySource, emptySource); 69 new LibrarySpecificUnit(emptySource, emptySource);
78 Map<String, TaskInput> inputs = 70 Map<String, TaskInput> inputs =
79 BuildCompilationUnitElementTask.buildInputs(target); 71 BuildCompilationUnitElementTask.buildInputs(target);
80 expect(inputs, isNotNull); 72 expect(inputs, isNotNull);
81 expect( 73 expect(
82 inputs.keys, 74 inputs.keys,
83 unorderedEquals( 75 unorderedEquals(
84 [BuildCompilationUnitElementTask.PARSED_UNIT_INPUT_NAME])); 76 [BuildCompilationUnitElementTask.PARSED_UNIT_INPUT_NAME]));
(...skipping 19 matching lines...) Expand all
104 BuildCompilationUnitElementTask task = 96 BuildCompilationUnitElementTask task =
105 new BuildCompilationUnitElementTask(null, emptySource); 97 new BuildCompilationUnitElementTask(null, emptySource);
106 expect(task.description, isNotNull); 98 expect(task.description, isNotNull);
107 } 99 }
108 100
109 test_descriptor() { 101 test_descriptor() {
110 TaskDescriptor descriptor = BuildCompilationUnitElementTask.DESCRIPTOR; 102 TaskDescriptor descriptor = BuildCompilationUnitElementTask.DESCRIPTOR;
111 expect(descriptor, isNotNull); 103 expect(descriptor, isNotNull);
112 } 104 }
113 105
114 test_perform_find_constants_strong() { 106 test_perform_find_constants() {
115 enableStrongMode();
116 _performBuildTask(''' 107 _performBuildTask('''
117 const x = 1; 108 const x = 1;
118 class C { 109 class C {
119 static const y = 1;
120 const C([p = 1]);
121 }
122 @x
123 f() {
124 const z = 1;
125 }
126 ''');
127 CompilationUnit unit = outputs[RESOLVED_UNIT1];
128 CompilationUnitElement unitElement = outputs[COMPILATION_UNIT_ELEMENT];
129 Annotation annotation = unit.declarations
130 .firstWhere((m) => m is FunctionDeclaration)
131 .metadata[0];
132 List<ConstantEvaluationTarget> expectedConstants = [
133 unitElement.accessors.firstWhere((e) => e.isGetter).variable,
134 unitElement.types[0].fields[0],
135 unitElement.functions[0].localVariables[0],
136 unitElement.types[0].constructors[0],
137 new ConstantEvaluationTarget_Annotation(
138 context, source, source, annotation),
139 unitElement.types[0].constructors[0].parameters[0]
140 ];
141 expect(outputs[CLASSES_IN_UNIT], hasLength(1));
142 expect(
143 outputs[COMPILATION_UNIT_CONSTANTS].toSet(), expectedConstants.toSet());
144 expect(outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT], hasLength(2));
145 }
146
147 test_perform_find_constants_weak() {
148 _performBuildTask('''
149 const x = 1;
150 class C {
151 static const y = 1; 110 static const y = 1;
152 const C([p = 1]); 111 const C([p = 1]);
153 } 112 }
154 @x 113 @x
155 f() { 114 f() {
156 const z = 1; 115 const z = 1;
157 } 116 }
158 '''); 117 ''');
159 CompilationUnit unit = outputs[RESOLVED_UNIT1]; 118 CompilationUnit unit = outputs[RESOLVED_UNIT1];
160 CompilationUnitElement unitElement = outputs[COMPILATION_UNIT_ELEMENT]; 119 CompilationUnitElement unitElement = outputs[COMPILATION_UNIT_ELEMENT];
161 Annotation annotation = unit.declarations 120 Annotation annotation = unit.declarations
162 .firstWhere((m) => m is FunctionDeclaration) 121 .firstWhere((m) => m is FunctionDeclaration)
163 .metadata[0]; 122 .metadata[0];
164 List<ConstantEvaluationTarget> expectedConstants = [ 123 List<ConstantEvaluationTarget> expectedConstants = [
165 unitElement.accessors.firstWhere((e) => e.isGetter).variable, 124 unitElement.accessors.firstWhere((e) => e.isGetter).variable,
166 unitElement.types[0].fields[0], 125 unitElement.types[0].fields[0],
167 unitElement.functions[0].localVariables[0], 126 unitElement.functions[0].localVariables[0],
168 unitElement.types[0].constructors[0], 127 unitElement.types[0].constructors[0],
169 new ConstantEvaluationTarget_Annotation( 128 new ConstantEvaluationTarget_Annotation(
170 context, source, source, annotation), 129 context, source, source, annotation),
171 unitElement.types[0].constructors[0].parameters[0] 130 unitElement.types[0].constructors[0].parameters[0]
172 ]; 131 ];
173 expect(outputs[CLASSES_IN_UNIT], hasLength(0));
174 expect( 132 expect(
175 outputs[COMPILATION_UNIT_CONSTANTS].toSet(), expectedConstants.toSet()); 133 outputs[COMPILATION_UNIT_CONSTANTS].toSet(), expectedConstants.toSet());
176 expect(outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT], hasLength(0));
177 } 134 }
178 135
179 test_perform_library_strong() { 136 test_perform_library() {
180 enableStrongMode();
181 _performBuildTask(r''' 137 _performBuildTask(r'''
182 library lib; 138 library lib;
183 import 'lib2.dart'; 139 import 'lib2.dart';
184 export 'lib3.dart';
185 part 'part.dart';
186 final x = '';
187 class A {
188 static final y = 0;
189 }
190 class B = Object with A;
191 ''');
192 expect(outputs, hasLength(5));
193 expect(outputs[CLASSES_IN_UNIT], hasLength(2));
194 expect(outputs[COMPILATION_UNIT_CONSTANTS], isNotNull);
195 expect(outputs[COMPILATION_UNIT_ELEMENT], isNotNull);
196 expect(outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT], hasLength(2));
197 expect(outputs[RESOLVED_UNIT1], isNotNull);
198 }
199
200 test_perform_library_weak() {
201 _performBuildTask(r'''
202 library lib;
203 import 'lib2.dart';
204 export 'lib3.dart'; 140 export 'lib3.dart';
205 part 'part.dart'; 141 part 'part.dart';
206 final x = ''; 142 final x = '';
207 class A { 143 class A {
208 static final y = 0; 144 static final y = 0;
209 } 145 }
210 class B = Object with A; 146 class B = Object with A;
211 '''); 147 ''');
212 expect(outputs, hasLength(5)); 148 expect(outputs, hasLength(3));
213 expect(outputs[CLASSES_IN_UNIT], hasLength(0));
214 expect(outputs[COMPILATION_UNIT_CONSTANTS], isNotNull); 149 expect(outputs[COMPILATION_UNIT_CONSTANTS], isNotNull);
215 expect(outputs[COMPILATION_UNIT_ELEMENT], isNotNull); 150 expect(outputs[COMPILATION_UNIT_ELEMENT], isNotNull);
216 expect(outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT], hasLength(0));
217 expect(outputs[RESOLVED_UNIT1], isNotNull); 151 expect(outputs[RESOLVED_UNIT1], isNotNull);
218 } 152 }
219 153
220 test_perform_reuseElement() { 154 test_perform_reuseElement() {
221 _performBuildTask(r''' 155 _performBuildTask(r'''
222 library lib; 156 library lib;
223 class A {} 157 class A {}
224 class B = Object with A; 158 class B = Object with A;
225 '''); 159 ''');
226 CompilationUnit unit = outputs[RESOLVED_UNIT1]; 160 CompilationUnit unit = outputs[RESOLVED_UNIT1];
(...skipping 1322 matching lines...) Expand 10 before | Expand all | Expand 10 after
1549 computeResult(librarySpecificUnit, RESOLVED_UNIT1); 1483 computeResult(librarySpecificUnit, RESOLVED_UNIT1);
1550 CompilationUnit unit = outputs[RESOLVED_UNIT1]; 1484 CompilationUnit unit = outputs[RESOLVED_UNIT1];
1551 return unit; 1485 return unit;
1552 } 1486 }
1553 1487
1554 CompilationUnit _resolveUnit(String content) => 1488 CompilationUnit _resolveUnit(String content) =>
1555 _resolveSource(newSource('/test.dart', content)); 1489 _resolveSource(newSource('/test.dart', content));
1556 } 1490 }
1557 1491
1558 @reflectiveTest 1492 @reflectiveTest
1493 class ComputeInferableStaticVariableDependenciesTaskTest
1494 extends _AbstractDartTaskTest {
1495 test_perform() {
1496 AnalysisTarget source = newSource(
1497 '/test.dart',
1498 '''
1499 const a = b;
1500 const b = 0;
1501 ''');
1502 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
1503 computeResult(target, RESOLVED_UNIT5);
1504 CompilationUnit unit = outputs[RESOLVED_UNIT5];
1505 TopLevelVariableElement elementA = unit.element.topLevelVariables[0];
1506 TopLevelVariableElement elementB = unit.element.topLevelVariables[1];
1507
1508 computeResult(elementA, INFERABLE_STATIC_VARIABLE_DEPENDENCIES);
1509 expect(task,
1510 new isInstanceOf<ComputeInferableStaticVariableDependenciesTask>());
1511 expect(outputs, hasLength(1));
1512 Set<VariableElement> dependencies =
1513 outputs[INFERABLE_STATIC_VARIABLE_DEPENDENCIES];
1514 expect(dependencies, unorderedEquals([elementB]));
1515 }
1516 }
1517
1518 @reflectiveTest
1559 class ContainingLibrariesTaskTest extends _AbstractDartTaskTest { 1519 class ContainingLibrariesTaskTest extends _AbstractDartTaskTest {
1560 test_buildInputs() { 1520 test_buildInputs() {
1561 Map<String, TaskInput> inputs = 1521 Map<String, TaskInput> inputs =
1562 ContainingLibrariesTask.buildInputs(emptySource); 1522 ContainingLibrariesTask.buildInputs(emptySource);
1563 expect(inputs, isNotNull); 1523 expect(inputs, isNotNull);
1564 expect(inputs, isEmpty); 1524 expect(inputs, isEmpty);
1565 } 1525 }
1566 1526
1567 test_constructor() { 1527 test_constructor() {
1568 ContainingLibrariesTask task = 1528 ContainingLibrariesTask task =
(...skipping 760 matching lines...) Expand 10 before | Expand all | Expand 10 after
2329 expect(sources, hasLength(lenght)); 2289 expect(sources, hasLength(lenght));
2330 expect(sources, contains(predicate((Source s) { 2290 expect(sources, contains(predicate((Source s) {
2331 return s.fullName.endsWith('core.dart'); 2291 return s.fullName.endsWith('core.dart');
2332 }))); 2292 })));
2333 } 2293 }
2334 } 2294 }
2335 2295
2336 @reflectiveTest 2296 @reflectiveTest
2337 class PartiallyResolveReferencesTaskTest extends _AbstractDartTaskTest { 2297 class PartiallyResolveReferencesTaskTest extends _AbstractDartTaskTest {
2338 test_perform() { 2298 test_perform() {
2299 enableStrongMode();
2339 Source source = newSource( 2300 Source source = newSource(
2340 '/test.dart', 2301 '/test.dart',
2341 ''' 2302 '''
2342 int a = b; 2303 int a = b;
2343 int b = c; 2304 int b = c;
2305 var d = 0;
2306 class A {}
2307 class C {
2308 static final f = '';
2309 var g = 0;
2310 }
2344 '''); 2311 ''');
2345 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 2312 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2346 computeResult(target, RESOLVED_UNIT5); 2313 computeResult(target, RESOLVED_UNIT5);
2347 expect(task, new isInstanceOf<PartiallyResolveUnitReferencesTask>()); 2314 expect(task, new isInstanceOf<PartiallyResolveUnitReferencesTask>());
2348 // Test the outputs 2315 // Test the outputs
2316 expect(outputs[CLASSES_IN_UNIT], hasLength(2));
2317 expect(outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT], hasLength(2));
2349 CompilationUnit unit = outputs[RESOLVED_UNIT5]; 2318 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2350 expect(unit, same(outputs[RESOLVED_UNIT5])); 2319 expect(unit, same(outputs[RESOLVED_UNIT5]));
2351 // Test the state of the AST 2320 // Test the state of the AST
2352 TopLevelVariableDeclaration a = unit.declarations[0]; 2321 TopLevelVariableDeclaration a = unit.declarations[0];
2353 VariableDeclaration variableA = a.variables.variables[0]; 2322 VariableDeclaration variableA = a.variables.variables[0];
2354 SimpleIdentifier initializer = variableA.initializer; 2323 SimpleIdentifier initializer = variableA.initializer;
2355 expect(initializer.staticElement, isNotNull); 2324 expect(initializer.staticElement, isNotNull);
2356 // Test the error generation 2325 // Test the error generation
2357 _fillErrorListener(PARTIALLY_RESOLVE_REFERENCES_ERRORS); 2326 _fillErrorListener(PARTIALLY_RESOLVE_REFERENCES_ERRORS);
2358 errorListener.assertErrorsWithCodes( 2327 errorListener.assertErrorsWithCodes(
(...skipping 20 matching lines...) Expand all
2379 ''' 2348 '''
2380 library c; 2349 library c;
2381 import 'b.dart'; 2350 import 'b.dart';
2382 main() { 2351 main() {
2383 new A<int>().m(); 2352 new A<int>().m();
2384 } 2353 }
2385 '''); 2354 ''');
2386 computeResult(new LibrarySpecificUnit(sourceC, sourceC), RESOLVED_UNIT5); 2355 computeResult(new LibrarySpecificUnit(sourceC, sourceC), RESOLVED_UNIT5);
2387 expect(task, new isInstanceOf<PartiallyResolveUnitReferencesTask>()); 2356 expect(task, new isInstanceOf<PartiallyResolveUnitReferencesTask>());
2388 // validate 2357 // validate
2358 expect(outputs[CLASSES_IN_UNIT], hasLength(0));
2359 expect(outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT], hasLength(0));
2389 CompilationUnit unit = outputs[RESOLVED_UNIT5]; 2360 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2390 expect(unit, isNotNull); 2361 expect(unit, isNotNull);
2391 2362
2392 FunctionDeclaration functionDeclaration = unit.declarations[0]; 2363 FunctionDeclaration functionDeclaration = unit.declarations[0];
2393 BlockFunctionBody body = functionDeclaration.functionExpression.body; 2364 BlockFunctionBody body = functionDeclaration.functionExpression.body;
2394 List<Statement> statements = body.block.statements; 2365 List<Statement> statements = body.block.statements;
2395 ExpressionStatement statement = statements[0]; 2366 ExpressionStatement statement = statements[0];
2396 MethodInvocation invocation = statement.expression; 2367 MethodInvocation invocation = statement.expression;
2397 MethodElement methodElement = invocation.methodName.staticElement; 2368 MethodElement methodElement = invocation.methodName.staticElement;
2398 expect(methodElement, isNotNull); 2369 expect(methodElement, isNotNull);
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after
2894 <script type='application/dart'>$scriptContent</script> 2865 <script type='application/dart'>$scriptContent</script>
2895 </head> 2866 </head>
2896 <body>Test</body> 2867 <body>Test</body>
2897 </html> 2868 </html>
2898 '''; 2869 ''';
2899 Source source = newSource('/test.html', htmlContent); 2870 Source source = newSource('/test.html', htmlContent);
2900 return new DartScript( 2871 return new DartScript(
2901 source, [new ScriptFragment(97, 5, 36, scriptContent)]); 2872 source, [new ScriptFragment(97, 5, 36, scriptContent)]);
2902 } 2873 }
2903 2874
2875 /**
2876 * Enable strong mode in the current analysis context.
2877 */
2878 void enableStrongMode() {
2879 AnalysisOptionsImpl options = context.analysisOptions;
2880 options.strongMode = true;
2881 context.analysisOptions = options;
2882 }
2883
2904 void setUp() { 2884 void setUp() {
2905 super.setUp(); 2885 super.setUp();
2906 emptySource = newSource('/test.dart'); 2886 emptySource = newSource('/test.dart');
2907 } 2887 }
2908 2888
2909 /** 2889 /**
2910 * Fill [errorListener] with [result] errors in the current [task]. 2890 * Fill [errorListener] with [result] errors in the current [task].
2911 */ 2891 */
2912 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) { 2892 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) {
2913 List<AnalysisError> errors = task.outputs[result]; 2893 List<AnalysisError> errors = task.outputs[result];
2914 expect(errors, isNotNull, reason: result.name); 2894 expect(errors, isNotNull, reason: result.name);
2915 errorListener = new GatheringErrorListener(); 2895 errorListener = new GatheringErrorListener();
2916 errorListener.addAll(errors); 2896 errorListener.addAll(errors);
2917 } 2897 }
2918 } 2898 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/task/strong_mode.dart ('k') | pkg/analyzer/test/src/task/strong_mode_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698