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

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

Issue 1323653002: Task to infer static variables (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: 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 29 matching lines...) Expand all
40 runReflectiveTests(BuildTypeProviderTaskTest); 40 runReflectiveTests(BuildTypeProviderTaskTest);
41 runReflectiveTests(ComputeConstantDependenciesTaskTest); 41 runReflectiveTests(ComputeConstantDependenciesTaskTest);
42 runReflectiveTests(ComputeConstantValueTaskTest); 42 runReflectiveTests(ComputeConstantValueTaskTest);
43 runReflectiveTests(ComputeInferableStaticVariableDependenciesTaskTest); 43 runReflectiveTests(ComputeInferableStaticVariableDependenciesTaskTest);
44 runReflectiveTests(ContainingLibrariesTaskTest); 44 runReflectiveTests(ContainingLibrariesTaskTest);
45 runReflectiveTests(DartErrorsTaskTest); 45 runReflectiveTests(DartErrorsTaskTest);
46 runReflectiveTests(EvaluateUnitConstantsTaskTest); 46 runReflectiveTests(EvaluateUnitConstantsTaskTest);
47 runReflectiveTests(GatherUsedImportedElementsTaskTest); 47 runReflectiveTests(GatherUsedImportedElementsTaskTest);
48 runReflectiveTests(GatherUsedLocalElementsTaskTest); 48 runReflectiveTests(GatherUsedLocalElementsTaskTest);
49 runReflectiveTests(GenerateHintsTaskTest); 49 runReflectiveTests(GenerateHintsTaskTest);
50 runReflectiveTests(InferStaticVariableTypesInUnitTaskTest);
51 runReflectiveTests(InferStaticVariableTypeTaskTest);
50 runReflectiveTests(LibraryErrorsReadyTaskTest); 52 runReflectiveTests(LibraryErrorsReadyTaskTest);
51 runReflectiveTests(LibraryUnitErrorsTaskTest); 53 runReflectiveTests(LibraryUnitErrorsTaskTest);
52 runReflectiveTests(ParseDartTaskTest); 54 runReflectiveTests(ParseDartTaskTest);
53 runReflectiveTests(PartiallyResolveReferencesTaskTest); 55 runReflectiveTests(PartiallyResolveReferencesTaskTest);
54 runReflectiveTests(ResolveUnitTypeNamesTaskTest); 56 runReflectiveTests(ResolveUnitTypeNamesTaskTest);
55 runReflectiveTests(ResolveLibraryTypeNamesTaskTest); 57 runReflectiveTests(ResolveLibraryTypeNamesTaskTest);
56 runReflectiveTests(ResolveReferencesTaskTest); 58 runReflectiveTests(ResolveReferencesTaskTest);
57 runReflectiveTests(ResolveVariableReferencesTaskTest); 59 runReflectiveTests(ResolveVariableReferencesTaskTest);
58 runReflectiveTests(ScanDartTaskTest); 60 runReflectiveTests(ScanDartTaskTest);
59 runReflectiveTests(VerifyUnitTaskTest); 61 runReflectiveTests(VerifyUnitTaskTest);
(...skipping 1442 matching lines...) Expand 10 before | Expand all | Expand 10 after
1502 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 1504 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
1503 computeResult(target, RESOLVED_UNIT5); 1505 computeResult(target, RESOLVED_UNIT5);
1504 CompilationUnit unit = outputs[RESOLVED_UNIT5]; 1506 CompilationUnit unit = outputs[RESOLVED_UNIT5];
1505 TopLevelVariableElement elementA = unit.element.topLevelVariables[0]; 1507 TopLevelVariableElement elementA = unit.element.topLevelVariables[0];
1506 TopLevelVariableElement elementB = unit.element.topLevelVariables[1]; 1508 TopLevelVariableElement elementB = unit.element.topLevelVariables[1];
1507 1509
1508 computeResult(elementA, INFERABLE_STATIC_VARIABLE_DEPENDENCIES); 1510 computeResult(elementA, INFERABLE_STATIC_VARIABLE_DEPENDENCIES);
1509 expect(task, 1511 expect(task,
1510 new isInstanceOf<ComputeInferableStaticVariableDependenciesTask>()); 1512 new isInstanceOf<ComputeInferableStaticVariableDependenciesTask>());
1511 expect(outputs, hasLength(1)); 1513 expect(outputs, hasLength(1));
1512 Set<VariableElement> dependencies = 1514 List<VariableElement> dependencies =
1513 outputs[INFERABLE_STATIC_VARIABLE_DEPENDENCIES]; 1515 outputs[INFERABLE_STATIC_VARIABLE_DEPENDENCIES];
1514 expect(dependencies, unorderedEquals([elementB])); 1516 expect(dependencies, unorderedEquals([elementB]));
1515 } 1517 }
1516 } 1518 }
1517 1519
1518 @reflectiveTest 1520 @reflectiveTest
1519 class ContainingLibrariesTaskTest extends _AbstractDartTaskTest { 1521 class ContainingLibrariesTaskTest extends _AbstractDartTaskTest {
1520 test_buildInputs() { 1522 test_buildInputs() {
1521 Map<String, TaskInput> inputs = 1523 Map<String, TaskInput> inputs =
1522 ContainingLibrariesTask.buildInputs(emptySource); 1524 ContainingLibrariesTask.buildInputs(emptySource);
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
2025 computeResult(target, HINTS); 2027 computeResult(target, HINTS);
2026 expect(task, new isInstanceOf<GenerateHintsTask>()); 2028 expect(task, new isInstanceOf<GenerateHintsTask>());
2027 // validate 2029 // validate
2028 _fillErrorListener(HINTS); 2030 _fillErrorListener(HINTS);
2029 errorListener.assertErrorsWithCodes( 2031 errorListener.assertErrorsWithCodes(
2030 <ErrorCode>[HintCode.UNUSED_ELEMENT, HintCode.UNUSED_ELEMENT]); 2032 <ErrorCode>[HintCode.UNUSED_ELEMENT, HintCode.UNUSED_ELEMENT]);
2031 } 2033 }
2032 } 2034 }
2033 2035
2034 @reflectiveTest 2036 @reflectiveTest
2037 class InferStaticVariableTypesInUnitTaskTest extends _AbstractDartTaskTest {
2038 void test_perform() {
2039 enableStrongMode();
2040 AnalysisTarget firstSource = newSource(
2041 '/first.dart',
2042 '''
2043 import 'second.dart';
2044
2045 var a = new M();
2046 var c = b;
2047 ''');
2048 AnalysisTarget secondSource = newSource(
2049 '/second.dart',
2050 '''
2051 import 'first.dart';
2052
2053 var b = a;
2054 class M {}
2055 ''');
2056 computeResult(new LibrarySpecificUnit(firstSource, firstSource),
2057 RESOLVED_UNIT6); // new isInstanceOf<InferStaticVariableTypesInUnitTask >()
2058 CompilationUnit firstUnit = outputs[RESOLVED_UNIT6];
2059 computeResult(
2060 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT6);
2061 CompilationUnit secondUnit = outputs[RESOLVED_UNIT6];
2062
2063 VariableDeclaration variableA = getTopLevelVariable(firstUnit, 'a');
2064 VariableDeclaration variableB = getTopLevelVariable(secondUnit, 'b');
2065 VariableDeclaration variableC = getTopLevelVariable(firstUnit, 'c');
2066 ClassDeclaration classM = getClass(secondUnit, 'M');
2067 DartType typeM = classM.element.type;
2068
2069 expect(variableA.element.type, typeM);
2070 expect(variableB.element.type, typeM);
2071 expect(variableB.initializer.staticType, typeM);
2072 expect(variableC.element.type, typeM);
2073 expect(variableC.initializer.staticType, typeM);
2074 }
2075 }
2076
2077 @reflectiveTest
2078 class InferStaticVariableTypeTaskTest extends _AbstractDartTaskTest {
2079 void test_getDeclaration_staticField() {
2080 AnalysisTarget source = newSource(
2081 '/test.dart',
2082 '''
2083 class C {
2084 var field = '';
2085 }
2086 ''');
2087 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
2088 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2089 VariableDeclaration declaration = getFieldInClass(unit, 'C', 'field');
2090 VariableElement variable = declaration.name.staticElement;
2091 InferStaticVariableTypeTask inferTask =
2092 new InferStaticVariableTypeTask(task.context, variable);
2093 expect(inferTask.getDeclaration(unit), declaration);
2094 }
2095
2096 void test_getDeclaration_topLevel() {
2097 AnalysisTarget source = newSource(
2098 '/test.dart',
2099 '''
2100 var topLevel = '';
2101 ''');
2102 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
2103 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2104 VariableDeclaration declaration = getTopLevelVariable(unit, 'topLevel');
2105 VariableElement variable = declaration.name.staticElement;
2106 InferStaticVariableTypeTask inferTask =
2107 new InferStaticVariableTypeTask(task.context, variable);
2108 expect(inferTask.getDeclaration(unit), declaration);
2109 }
2110
2111 void test_perform() {
2112 AnalysisTarget source = newSource(
2113 '/test.dart',
2114 '''
2115 var topLevel = '';
2116 class C {
2117 var field = topLevel;
2118 }
2119 ''');
2120 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
2121 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2122 VariableElement topLevel =
2123 getTopLevelVariable(unit, 'topLevel').name.staticElement;
2124 VariableElement field =
2125 getFieldInClass(unit, 'C', 'field').name.staticElement;
2126
2127 computeResult(field, INFERRED_STATIC_VARIABLE);
2128 InterfaceType stringType = context.typeProvider.stringType;
2129 expect(topLevel.type, stringType);
2130 expect(field.type, stringType);
2131 }
2132 }
2133
2134 @reflectiveTest
2035 class LibraryErrorsReadyTaskTest extends _AbstractDartTaskTest { 2135 class LibraryErrorsReadyTaskTest extends _AbstractDartTaskTest {
2036 test_perform() { 2136 test_perform() {
2037 Source library = newSource( 2137 Source library = newSource(
2038 '/lib.dart', 2138 '/lib.dart',
2039 r''' 2139 r'''
2040 library lib; 2140 library lib;
2041 part 'part1.dart'; 2141 part 'part1.dart';
2042 part 'part2.dart'; 2142 part 'part2.dart';
2043 X v1; 2143 X v1;
2044 '''); 2144 ''');
(...skipping 829 matching lines...) Expand 10 before | Expand all | Expand 10 after
2874 2974
2875 /** 2975 /**
2876 * Enable strong mode in the current analysis context. 2976 * Enable strong mode in the current analysis context.
2877 */ 2977 */
2878 void enableStrongMode() { 2978 void enableStrongMode() {
2879 AnalysisOptionsImpl options = context.analysisOptions; 2979 AnalysisOptionsImpl options = context.analysisOptions;
2880 options.strongMode = true; 2980 options.strongMode = true;
2881 context.analysisOptions = options; 2981 context.analysisOptions = options;
2882 } 2982 }
2883 2983
2984 /**
2985 * Return the declaration of the class with the given [className] in the given
2986 * compilation [unit].
2987 */
2988 ClassDeclaration getClass(CompilationUnit unit, String className) {
2989 NodeList<CompilationUnitMember> unitMembers = unit.declarations;
2990 for (CompilationUnitMember unitMember in unitMembers) {
2991 if (unitMember is ClassDeclaration && unitMember.name.name == className) {
2992 return unitMember;
2993 }
2994 }
2995 return null;
2996 }
2997
2998 /**
2999 * Return the declaration of the field with the given [fieldName] in the class
3000 * with the given [className] in the given compilation [unit].
3001 */
3002 VariableDeclaration getFieldInClass(
3003 CompilationUnit unit, String className, String fieldName) {
3004 ClassDeclaration unitMember = getClass(unit, className);
3005 if (unitMember == null) {
3006 return null;
3007 }
3008 NodeList<ClassMember> classMembers = unitMember.members;
3009 for (ClassMember classMember in classMembers) {
3010 if (classMember is FieldDeclaration) {
3011 NodeList<VariableDeclaration> fields = classMember.fields.variables;
3012 for (VariableDeclaration field in fields) {
3013 if (field.name.name == fieldName) {
3014 return field;
3015 }
3016 }
3017 }
3018 }
3019 return null;
3020 }
3021
3022 /**
3023 * Return the declaration of the top-level variable with the given
3024 * [variableName] in the given compilation [unit].
3025 */
3026 VariableDeclaration getTopLevelVariable(
3027 CompilationUnit unit, String variableName) {
3028 NodeList<CompilationUnitMember> unitMembers = unit.declarations;
3029 for (CompilationUnitMember unitMember in unitMembers) {
3030 if (unitMember is TopLevelVariableDeclaration) {
3031 NodeList<VariableDeclaration> variables =
3032 unitMember.variables.variables;
3033 for (VariableDeclaration variable in variables) {
3034 if (variable.name.name == variableName) {
3035 return variable;
3036 }
3037 }
3038 }
3039 }
3040 return null;
3041 }
3042
2884 void setUp() { 3043 void setUp() {
2885 super.setUp(); 3044 super.setUp();
2886 emptySource = newSource('/test.dart'); 3045 emptySource = newSource('/test.dart');
2887 } 3046 }
2888 3047
2889 /** 3048 /**
2890 * Fill [errorListener] with [result] errors in the current [task]. 3049 * Fill [errorListener] with [result] errors in the current [task].
2891 */ 3050 */
2892 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) { 3051 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) {
2893 List<AnalysisError> errors = task.outputs[result]; 3052 List<AnalysisError> errors = task.outputs[result];
2894 expect(errors, isNotNull, reason: result.name); 3053 expect(errors, isNotNull, reason: result.name);
2895 errorListener = new GatheringErrorListener(); 3054 errorListener = new GatheringErrorListener();
2896 errorListener.addAll(errors); 3055 errorListener.addAll(errors);
2897 } 3056 }
2898 } 3057 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698