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

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

Issue 1385293002: Lint support for the new task model. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Tests and tweaks. Created 5 years, 2 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';
11 import 'package:analyzer/src/generated/engine.dart' 11 import 'package:analyzer/src/generated/engine.dart'
12 show AnalysisOptionsImpl, CacheState; 12 show AnalysisOptionsImpl, CacheState;
13 import 'package:analyzer/src/generated/error.dart'; 13 import 'package:analyzer/src/generated/error.dart';
14 import 'package:analyzer/src/generated/resolver.dart'; 14 import 'package:analyzer/src/generated/resolver.dart';
15 import 'package:analyzer/src/generated/scanner.dart'; 15 import 'package:analyzer/src/generated/scanner.dart';
16 import 'package:analyzer/src/generated/sdk.dart'; 16 import 'package:analyzer/src/generated/sdk.dart';
17 import 'package:analyzer/src/generated/source.dart'; 17 import 'package:analyzer/src/generated/source.dart';
18 import 'package:analyzer/src/services/lint.dart';
18 import 'package:analyzer/src/task/dart.dart'; 19 import 'package:analyzer/src/task/dart.dart';
19 import 'package:analyzer/src/task/html.dart'; 20 import 'package:analyzer/src/task/html.dart';
20 import 'package:analyzer/task/dart.dart'; 21 import 'package:analyzer/task/dart.dart';
21 import 'package:analyzer/task/general.dart'; 22 import 'package:analyzer/task/general.dart';
22 import 'package:analyzer/task/model.dart'; 23 import 'package:analyzer/task/model.dart';
23 import 'package:unittest/unittest.dart'; 24 import 'package:unittest/unittest.dart';
24 25
25 import '../../generated/resolver_test.dart'; 26 import '../../generated/resolver_test.dart';
26 import '../../generated/test_support.dart'; 27 import '../../generated/test_support.dart';
27 import '../../reflective_tests.dart'; 28 import '../../reflective_tests.dart';
(...skipping 13 matching lines...) Expand all
41 runReflectiveTests(BuildTypeProviderTaskTest); 42 runReflectiveTests(BuildTypeProviderTaskTest);
42 runReflectiveTests(ComputeConstantDependenciesTaskTest); 43 runReflectiveTests(ComputeConstantDependenciesTaskTest);
43 runReflectiveTests(ComputeConstantValueTaskTest); 44 runReflectiveTests(ComputeConstantValueTaskTest);
44 runReflectiveTests(ComputeInferableStaticVariableDependenciesTaskTest); 45 runReflectiveTests(ComputeInferableStaticVariableDependenciesTaskTest);
45 runReflectiveTests(ContainingLibrariesTaskTest); 46 runReflectiveTests(ContainingLibrariesTaskTest);
46 runReflectiveTests(DartErrorsTaskTest); 47 runReflectiveTests(DartErrorsTaskTest);
47 runReflectiveTests(EvaluateUnitConstantsTaskTest); 48 runReflectiveTests(EvaluateUnitConstantsTaskTest);
48 runReflectiveTests(GatherUsedImportedElementsTaskTest); 49 runReflectiveTests(GatherUsedImportedElementsTaskTest);
49 runReflectiveTests(GatherUsedLocalElementsTaskTest); 50 runReflectiveTests(GatherUsedLocalElementsTaskTest);
50 runReflectiveTests(GenerateHintsTaskTest); 51 runReflectiveTests(GenerateHintsTaskTest);
52 runReflectiveTests(GenerateLintsTaskTest);
51 runReflectiveTests(InferInstanceMembersInUnitTaskTest); 53 runReflectiveTests(InferInstanceMembersInUnitTaskTest);
52 runReflectiveTests(InferStaticVariableTypesInUnitTaskTest); 54 runReflectiveTests(InferStaticVariableTypesInUnitTaskTest);
53 runReflectiveTests(InferStaticVariableTypeTaskTest); 55 runReflectiveTests(InferStaticVariableTypeTaskTest);
54 runReflectiveTests(LibraryErrorsReadyTaskTest); 56 runReflectiveTests(LibraryErrorsReadyTaskTest);
55 runReflectiveTests(LibraryUnitErrorsTaskTest); 57 runReflectiveTests(LibraryUnitErrorsTaskTest);
56 runReflectiveTests(ParseDartTaskTest); 58 runReflectiveTests(ParseDartTaskTest);
57 runReflectiveTests(PartiallyResolveUnitReferencesTaskTest); 59 runReflectiveTests(PartiallyResolveUnitReferencesTaskTest);
58 runReflectiveTests(ResolveFunctionBodiesInUnitTaskTest); 60 runReflectiveTests(ResolveFunctionBodiesInUnitTaskTest);
59 runReflectiveTests(ResolveLibraryTypeNamesTaskTest); 61 runReflectiveTests(ResolveLibraryTypeNamesTaskTest);
60 runReflectiveTests(ResolveUnitTypeNamesTaskTest); 62 runReflectiveTests(ResolveUnitTypeNamesTaskTest);
(...skipping 30 matching lines...) Expand all
91 isInstanceOf isContainingLibrariesTask = 93 isInstanceOf isContainingLibrariesTask =
92 new isInstanceOf<ContainingLibrariesTask>(); 94 new isInstanceOf<ContainingLibrariesTask>();
93 isInstanceOf isDartErrorsTask = new isInstanceOf<DartErrorsTask>(); 95 isInstanceOf isDartErrorsTask = new isInstanceOf<DartErrorsTask>();
94 isInstanceOf isEvaluateUnitConstantsTask = 96 isInstanceOf isEvaluateUnitConstantsTask =
95 new isInstanceOf<EvaluateUnitConstantsTask>(); 97 new isInstanceOf<EvaluateUnitConstantsTask>();
96 isInstanceOf isGatherUsedImportedElementsTask = 98 isInstanceOf isGatherUsedImportedElementsTask =
97 new isInstanceOf<GatherUsedImportedElementsTask>(); 99 new isInstanceOf<GatherUsedImportedElementsTask>();
98 isInstanceOf isGatherUsedLocalElementsTask = 100 isInstanceOf isGatherUsedLocalElementsTask =
99 new isInstanceOf<GatherUsedLocalElementsTask>(); 101 new isInstanceOf<GatherUsedLocalElementsTask>();
100 isInstanceOf isGenerateHintsTask = new isInstanceOf<GenerateHintsTask>(); 102 isInstanceOf isGenerateHintsTask = new isInstanceOf<GenerateHintsTask>();
103 isInstanceOf isGenerateLintsTask = new isInstanceOf<GenerateLintsTask>();
101 isInstanceOf isInferInstanceMembersInUnitTask = 104 isInstanceOf isInferInstanceMembersInUnitTask =
102 new isInstanceOf<InferInstanceMembersInUnitTask>(); 105 new isInstanceOf<InferInstanceMembersInUnitTask>();
103 isInstanceOf isInferStaticVariableTypesInUnitTask = 106 isInstanceOf isInferStaticVariableTypesInUnitTask =
104 new isInstanceOf<InferStaticVariableTypesInUnitTask>(); 107 new isInstanceOf<InferStaticVariableTypesInUnitTask>();
105 isInstanceOf isInferStaticVariableTypeTask = 108 isInstanceOf isInferStaticVariableTypeTask =
106 new isInstanceOf<InferStaticVariableTypeTask>(); 109 new isInstanceOf<InferStaticVariableTypeTask>();
107 isInstanceOf isLibraryErrorsReadyTask = 110 isInstanceOf isLibraryErrorsReadyTask =
108 new isInstanceOf<LibraryErrorsReadyTask>(); 111 new isInstanceOf<LibraryErrorsReadyTask>();
109 isInstanceOf isLibraryUnitErrorsTask = 112 isInstanceOf isLibraryUnitErrorsTask =
110 new isInstanceOf<LibraryUnitErrorsTask>(); 113 new isInstanceOf<LibraryUnitErrorsTask>();
111 isInstanceOf isParseDartTask = new isInstanceOf<ParseDartTask>(); 114 isInstanceOf isParseDartTask = new isInstanceOf<ParseDartTask>();
112 isInstanceOf isPartiallyResolveUnitReferencesTask = 115 isInstanceOf isPartiallyResolveUnitReferencesTask =
113 new isInstanceOf<PartiallyResolveUnitReferencesTask>(); 116 new isInstanceOf<PartiallyResolveUnitReferencesTask>();
114 isInstanceOf isResolveFunctionBodiesInUnitTask = 117 isInstanceOf isResolveFunctionBodiesInUnitTask =
115 new isInstanceOf<ResolveFunctionBodiesInUnitTask>(); 118 new isInstanceOf<ResolveFunctionBodiesInUnitTask>();
116 isInstanceOf isResolveLibraryTypeNamesTask = 119 isInstanceOf isResolveLibraryTypeNamesTask =
117 new isInstanceOf<ResolveLibraryTypeNamesTask>(); 120 new isInstanceOf<ResolveLibraryTypeNamesTask>();
118 isInstanceOf isResolveUnitTypeNamesTask = 121 isInstanceOf isResolveUnitTypeNamesTask =
119 new isInstanceOf<ResolveUnitTypeNamesTask>(); 122 new isInstanceOf<ResolveUnitTypeNamesTask>();
120 isInstanceOf isResolveVariableReferencesTask = 123 isInstanceOf isResolveVariableReferencesTask =
121 new isInstanceOf<ResolveVariableReferencesTask>(); 124 new isInstanceOf<ResolveVariableReferencesTask>();
122 isInstanceOf isScanDartTask = new isInstanceOf<ScanDartTask>(); 125 isInstanceOf isScanDartTask = new isInstanceOf<ScanDartTask>();
123 isInstanceOf isVerifyUnitTask = new isInstanceOf<VerifyUnitTask>(); 126 isInstanceOf isVerifyUnitTask = new isInstanceOf<VerifyUnitTask>();
124 127
128 final LintCode _testLintCode = new LintCode('test lint', 'test lint code');
129
125 @reflectiveTest 130 @reflectiveTest
126 class BuildCompilationUnitElementTaskTest extends _AbstractDartTaskTest { 131 class BuildCompilationUnitElementTaskTest extends _AbstractDartTaskTest {
127 Source source; 132 Source source;
128 LibrarySpecificUnit target; 133 LibrarySpecificUnit target;
129 134
130 test_perform_find_constants() { 135 test_perform_find_constants() {
131 _performBuildTask(''' 136 _performBuildTask('''
132 const x = 1; 137 const x = 1;
133 class C { 138 class C {
134 static const y = 1; 139 static const y = 1;
(...skipping 1734 matching lines...) Expand 10 before | Expand all | Expand 10 after
1869 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 1874 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
1870 computeResult(target, HINTS, matcher: isGenerateHintsTask); 1875 computeResult(target, HINTS, matcher: isGenerateHintsTask);
1871 // validate 1876 // validate
1872 _fillErrorListener(HINTS); 1877 _fillErrorListener(HINTS);
1873 errorListener.assertErrorsWithCodes( 1878 errorListener.assertErrorsWithCodes(
1874 <ErrorCode>[HintCode.UNUSED_ELEMENT, HintCode.UNUSED_ELEMENT]); 1879 <ErrorCode>[HintCode.UNUSED_ELEMENT, HintCode.UNUSED_ELEMENT]);
1875 } 1880 }
1876 } 1881 }
1877 1882
1878 @reflectiveTest 1883 @reflectiveTest
1884 class GenerateLintsTaskTest extends _AbstractDartTaskTest {
1885 void enableLints() {
1886 AnalysisOptionsImpl options = context.analysisOptions;
1887 options.lint = true;
1888 context.analysisOptions = options;
1889 }
1890
1891 @override
1892 void setUp() {
1893 super.setUp();
1894 enableLints();
1895 }
1896
1897 @override
1898 void tearDown() {
1899 LintGenerator.LINTERS.clear();
1900 super.tearDown();
1901 }
1902
1903 test_camel_case_types() {
1904 Source source = newSource(
1905 '/test.dart',
1906 '''
1907 class a { }
1908 ''');
1909
1910 LintGenerator.LINTERS.clear();
1911 LintGenerator.LINTERS.add(new GenerateLintsTaskTest_TestLinter());
1912
1913 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
1914 computeResult(target, LINTS, matcher: isGenerateLintsTask);
1915 // validate
1916 _fillErrorListener(LINTS);
1917 errorListener.assertErrorsWithCodes(<ErrorCode>[_testLintCode]);
1918 }
1919 }
1920
1921 class GenerateLintsTaskTest_AstVisitor extends SimpleAstVisitor {
1922 Linter linter;
1923 GenerateLintsTaskTest_AstVisitor(this.linter);
1924
1925 @override
1926 visitClassDeclaration(ClassDeclaration node) {
1927 if (!new RegExp(r'^([_]*)([A-Z]+[a-z0-9]*)+$')
1928 .hasMatch(node.name.toString())) {
1929 linter.reporter.reportErrorForNode(_testLintCode, node, []);
1930 }
1931 }
1932 }
1933
1934 class GenerateLintsTaskTest_TestLinter extends Linter {
1935 @override
1936 AstVisitor getVisitor() => new GenerateLintsTaskTest_AstVisitor(this);
1937 }
1938
1939 @reflectiveTest
1879 class InferInstanceMembersInUnitTaskTest extends _AbstractDartTaskTest { 1940 class InferInstanceMembersInUnitTaskTest extends _AbstractDartTaskTest {
1880 void test_perform() { 1941 void test_perform() {
1881 enableStrongMode(); 1942 enableStrongMode();
1882 AnalysisTarget source = newSource( 1943 AnalysisTarget source = newSource(
1883 '/test.dart', 1944 '/test.dart',
1884 ''' 1945 '''
1885 class A { 1946 class A {
1886 X f; 1947 X f;
1887 Y m(Z x) {} 1948 Y m(Z x) {}
1888 } 1949 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1966 2027
1967 InterfaceType stringType = context.typeProvider.stringType; 2028 InterfaceType stringType = context.typeProvider.stringType;
1968 expect(topLevel.type, stringType); 2029 expect(topLevel.type, stringType);
1969 expect(field.type, stringType); 2030 expect(field.type, stringType);
1970 expect(fieldDecl.initializer.staticType, stringType); 2031 expect(fieldDecl.initializer.staticType, stringType);
1971 } 2032 }
1972 } 2033 }
1973 2034
1974 @reflectiveTest 2035 @reflectiveTest
1975 class InferStaticVariableTypesInUnitTaskTest extends _AbstractDartTaskTest { 2036 class InferStaticVariableTypesInUnitTaskTest extends _AbstractDartTaskTest {
2037 void test_perform_const_field() {
2038 enableStrongMode();
2039 AnalysisTarget source = newSource(
2040 '/test.dart',
2041 '''
2042 class M {
2043 static const X = "";
2044 }
2045 ''');
2046 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6,
2047 matcher: isInferStaticVariableTypesInUnitTask);
2048 CompilationUnit unit = outputs[RESOLVED_UNIT6];
2049 VariableDeclaration declaration = getFieldInClass(unit, 'M', 'X');
2050 InterfaceType stringType = context.typeProvider.stringType;
2051 expect(declaration.element.type, stringType);
2052 }
2053
1976 void test_perform_nestedDeclarations() { 2054 void test_perform_nestedDeclarations() {
1977 enableStrongMode(); 2055 enableStrongMode();
1978 AnalysisTarget source = newSource( 2056 AnalysisTarget source = newSource(
1979 '/test.dart', 2057 '/test.dart',
1980 ''' 2058 '''
1981 var f = (int x) { 2059 var f = (int x) {
1982 int squared(int value) => value * value; 2060 int squared(int value) => value * value;
1983 var xSquared = squared(x); 2061 var xSquared = squared(x);
1984 return xSquared; 2062 return xSquared;
1985 }; 2063 };
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
2043 CompilationUnit unit = outputs[RESOLVED_UNIT6]; 2121 CompilationUnit unit = outputs[RESOLVED_UNIT6];
2044 TopLevelVariableDeclaration declaration = unit.declarations[1]; 2122 TopLevelVariableDeclaration declaration = unit.declarations[1];
2045 FunctionExpression function = 2123 FunctionExpression function =
2046 declaration.variables.variables[0].initializer; 2124 declaration.variables.variables[0].initializer;
2047 BlockFunctionBody body = function.body; 2125 BlockFunctionBody body = function.body;
2048 ReturnStatement statement = body.block.statements[0]; 2126 ReturnStatement statement = body.block.statements[0];
2049 Expression expression = statement.expression; 2127 Expression expression = statement.expression;
2050 InterfaceType intType = context.typeProvider.intType; 2128 InterfaceType intType = context.typeProvider.intType;
2051 expect(expression.staticType, intType); 2129 expect(expression.staticType, intType);
2052 } 2130 }
2053
2054 void test_perform_const_field() {
2055 enableStrongMode();
2056 AnalysisTarget source = newSource(
2057 '/test.dart',
2058 '''
2059 class M {
2060 static const X = "";
2061 }
2062 ''');
2063 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6,
2064 matcher: isInferStaticVariableTypesInUnitTask);
2065 CompilationUnit unit = outputs[RESOLVED_UNIT6];
2066 VariableDeclaration declaration = getFieldInClass(unit, 'M', 'X');
2067 InterfaceType stringType = context.typeProvider.stringType;
2068 expect(declaration.element.type, stringType);
2069 }
2070 } 2131 }
2071 2132
2072 @reflectiveTest 2133 @reflectiveTest
2073 class InferStaticVariableTypeTaskTest extends _AbstractDartTaskTest { 2134 class InferStaticVariableTypeTaskTest extends _AbstractDartTaskTest {
2074 void test_getDeclaration_staticField() { 2135 void test_getDeclaration_staticField() {
2075 AnalysisTarget source = newSource( 2136 AnalysisTarget source = newSource(
2076 '/test.dart', 2137 '/test.dart',
2077 ''' 2138 '''
2078 class C { 2139 class C {
2079 var field = ''; 2140 var field = '';
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
2121 2182
2122 computeResult(field, INFERRED_STATIC_VARIABLE, 2183 computeResult(field, INFERRED_STATIC_VARIABLE,
2123 matcher: isInferStaticVariableTypeTask); 2184 matcher: isInferStaticVariableTypeTask);
2124 InterfaceType stringType = context.typeProvider.stringType; 2185 InterfaceType stringType = context.typeProvider.stringType;
2125 expect(topLevel.type, stringType); 2186 expect(topLevel.type, stringType);
2126 expect(field.type, stringType); 2187 expect(field.type, stringType);
2127 expect(fieldDecl.initializer.staticType, stringType); 2188 expect(fieldDecl.initializer.staticType, stringType);
2128 expect(outputs[INFER_STATIC_VARIABLE_ERRORS], hasLength(0)); 2189 expect(outputs[INFER_STATIC_VARIABLE_ERRORS], hasLength(0));
2129 } 2190 }
2130 2191
2131 void test_perform_reresolution() {
2132 AnalysisTarget source = newSource(
2133 '/test.dart',
2134 '''
2135 const topLevel = '';
2136 class C {
2137 String field = topLevel;
2138 }
2139 ''');
2140 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
2141 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2142 VariableDeclaration topLevelDecl = getTopLevelVariable(unit, 'topLevel');
2143 VariableDeclaration fieldDecl = getFieldInClass(unit, 'C', 'field');
2144 VariableElement topLevel = topLevelDecl.name.staticElement;
2145 VariableElement field = fieldDecl.name.staticElement;
2146
2147 computeResult(field, INFERRED_STATIC_VARIABLE,
2148 matcher: isInferStaticVariableTypeTask);
2149 InterfaceType stringType = context.typeProvider.stringType;
2150 expect(topLevel.type, stringType);
2151 expect(field.type, stringType);
2152 expect(fieldDecl.initializer.staticType, stringType);
2153 expect(outputs[INFER_STATIC_VARIABLE_ERRORS], hasLength(0));
2154 }
2155
2156 void test_perform_const() { 2192 void test_perform_const() {
2157 AnalysisTarget source = newSource( 2193 AnalysisTarget source = newSource(
2158 '/test.dart', 2194 '/test.dart',
2159 ''' 2195 '''
2160 const topLevel = "hello"; 2196 const topLevel = "hello";
2161 class C { 2197 class C {
2162 var field = topLevel; 2198 var field = topLevel;
2163 } 2199 }
2164 '''); 2200 ''');
2165 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5); 2201 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2224 '''); 2260 ''');
2225 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5); 2261 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
2226 CompilationUnit unit = outputs[RESOLVED_UNIT5]; 2262 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2227 VariableElement a = getTopLevelVariable(unit, 'a').name.staticElement; 2263 VariableElement a = getTopLevelVariable(unit, 'a').name.staticElement;
2228 2264
2229 computeResult(a, INFERRED_STATIC_VARIABLE, 2265 computeResult(a, INFERRED_STATIC_VARIABLE,
2230 matcher: isInferStaticVariableTypeTask); 2266 matcher: isInferStaticVariableTypeTask);
2231 expect(a.type.isDynamic, isTrue); 2267 expect(a.type.isDynamic, isTrue);
2232 expect(outputs[INFER_STATIC_VARIABLE_ERRORS], hasLength(0)); 2268 expect(outputs[INFER_STATIC_VARIABLE_ERRORS], hasLength(0));
2233 } 2269 }
2270
2271 void test_perform_reresolution() {
2272 AnalysisTarget source = newSource(
2273 '/test.dart',
2274 '''
2275 const topLevel = '';
2276 class C {
2277 String field = topLevel;
2278 }
2279 ''');
2280 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
2281 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2282 VariableDeclaration topLevelDecl = getTopLevelVariable(unit, 'topLevel');
2283 VariableDeclaration fieldDecl = getFieldInClass(unit, 'C', 'field');
2284 VariableElement topLevel = topLevelDecl.name.staticElement;
2285 VariableElement field = fieldDecl.name.staticElement;
2286
2287 computeResult(field, INFERRED_STATIC_VARIABLE,
2288 matcher: isInferStaticVariableTypeTask);
2289 InterfaceType stringType = context.typeProvider.stringType;
2290 expect(topLevel.type, stringType);
2291 expect(field.type, stringType);
2292 expect(fieldDecl.initializer.staticType, stringType);
2293 expect(outputs[INFER_STATIC_VARIABLE_ERRORS], hasLength(0));
2294 }
2234 } 2295 }
2235 2296
2236 @reflectiveTest 2297 @reflectiveTest
2237 class LibraryErrorsReadyTaskTest extends _AbstractDartTaskTest { 2298 class LibraryErrorsReadyTaskTest extends _AbstractDartTaskTest {
2238 test_perform() { 2299 test_perform() {
2239 Source library = newSource( 2300 Source library = newSource(
2240 '/lib.dart', 2301 '/lib.dart',
2241 r''' 2302 r'''
2242 library lib; 2303 library lib;
2243 part 'part1.dart'; 2304 part 'part1.dart';
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
2587 } 2648 }
2588 2649
2589 void _assertResolved(FunctionBody body) { 2650 void _assertResolved(FunctionBody body) {
2590 ResolutionVerifier verifier = new ResolutionVerifier(); 2651 ResolutionVerifier verifier = new ResolutionVerifier();
2591 body.accept(verifier); 2652 body.accept(verifier);
2592 verifier.assertResolved(); 2653 verifier.assertResolved();
2593 } 2654 }
2594 } 2655 }
2595 2656
2596 @reflectiveTest 2657 @reflectiveTest
2658 class ResolveLibraryTypeNamesTaskTest extends _AbstractDartTaskTest {
2659 test_perform() {
2660 Source sourceLib = newSource(
2661 '/my_lib.dart',
2662 '''
2663 library my_lib;
2664 part 'my_part.dart';
2665 class A {}
2666 class B extends A {}
2667 ''');
2668 newSource(
2669 '/my_part.dart',
2670 '''
2671 part of my_lib;
2672 class C extends A {}
2673 ''');
2674 computeResult(sourceLib, LIBRARY_ELEMENT5,
2675 matcher: isResolveLibraryTypeNamesTask);
2676 // validate
2677 LibraryElement library = outputs[LIBRARY_ELEMENT5];
2678 {
2679 ClassElement classB = library.getType('B');
2680 expect(classB.supertype.displayName, 'A');
2681 }
2682 {
2683 ClassElement classC = library.getType('C');
2684 expect(classC.supertype.displayName, 'A');
2685 }
2686 }
2687
2688 test_perform_external() {
2689 Source sourceA = newSource(
2690 '/a.dart',
2691 '''
2692 library a;
2693 import 'b.dart';
2694 class A extends B {}
2695 ''');
2696 newSource(
2697 '/b.dart',
2698 '''
2699 library b;
2700 class B {}
2701 ''');
2702 // The reference A to B should be resolved, but there's no requirement that
2703 // the full class hierarchy be resolved.
2704 computeResult(sourceA, LIBRARY_ELEMENT5,
2705 matcher: isResolveLibraryTypeNamesTask);
2706 // validate
2707 LibraryElement library = outputs[LIBRARY_ELEMENT5];
2708 {
2709 ClassElement clazz = library.getType('A');
2710 expect(clazz.displayName, 'A');
2711 clazz = clazz.supertype.element;
2712 expect(clazz.displayName, 'B');
2713 }
2714 }
2715 }
2716
2717 @reflectiveTest
2718 class ResolveUnitTypeNamesTaskTest extends _AbstractDartTaskTest {
2719 test_perform() {
2720 Source source = newSource(
2721 '/test.dart',
2722 '''
2723 class A {}
2724 class B extends A {}
2725 int f(String p) => p.length;
2726 ''');
2727 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2728 computeResult(target, RESOLVED_UNIT3, matcher: isResolveUnitTypeNamesTask);
2729 // validate
2730 CompilationUnit unit = outputs[RESOLVED_UNIT3];
2731 {
2732 ClassDeclaration nodeA = unit.declarations[0];
2733 ClassDeclaration nodeB = unit.declarations[1];
2734 DartType extendsType = nodeB.extendsClause.superclass.type;
2735 expect(extendsType, nodeA.element.type);
2736 }
2737 {
2738 FunctionDeclaration functionNode = unit.declarations[2];
2739 DartType returnType = functionNode.returnType.type;
2740 List<FormalParameter> parameters =
2741 functionNode.functionExpression.parameters.parameters;
2742 expect(returnType.displayName, 'int');
2743 expect(parameters[0].element.type.displayName, 'String');
2744 }
2745 }
2746
2747 test_perform_errors() {
2748 Source source = newSource(
2749 '/test.dart',
2750 '''
2751 NoSuchClass f() => null;
2752 ''');
2753 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2754 computeResult(target, RESOLVE_TYPE_NAMES_ERRORS,
2755 matcher: isResolveUnitTypeNamesTask);
2756 // validate
2757 _fillErrorListener(RESOLVE_TYPE_NAMES_ERRORS);
2758 errorListener
2759 .assertErrorsWithCodes(<ErrorCode>[StaticWarningCode.UNDEFINED_CLASS]);
2760 }
2761
2762 test_perform_typedef() {
2763 Source source = newSource(
2764 '/test.dart',
2765 '''
2766 typedef int F(G g);
2767 typedef String G(int p);
2768 ''');
2769 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2770 computeResult(target, RESOLVED_UNIT3, matcher: isResolveUnitTypeNamesTask);
2771 // validate
2772 CompilationUnit unit = outputs[RESOLVED_UNIT3];
2773 FunctionTypeAlias nodeF = unit.declarations[0];
2774 FunctionTypeAlias nodeG = unit.declarations[1];
2775 {
2776 FormalParameter parameter = nodeF.parameters.parameters[0];
2777 DartType parameterType = parameter.element.type;
2778 Element returnTypeElement = nodeF.returnType.type.element;
2779 expect(returnTypeElement.displayName, 'int');
2780 expect(parameterType.element, nodeG.element);
2781 }
2782 {
2783 FormalParameter parameter = nodeG.parameters.parameters[0];
2784 DartType parameterType = parameter.element.type;
2785 expect(nodeG.returnType.type.element.displayName, 'String');
2786 expect(parameterType.element.displayName, 'int');
2787 }
2788 }
2789
2790 test_perform_typedef_errors() {
2791 Source source = newSource(
2792 '/test.dart',
2793 '''
2794 typedef int F(NoSuchType p);
2795 ''');
2796 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2797 computeResult(target, RESOLVE_TYPE_NAMES_ERRORS,
2798 matcher: isResolveUnitTypeNamesTask);
2799 // validate
2800 _fillErrorListener(RESOLVE_TYPE_NAMES_ERRORS);
2801 errorListener
2802 .assertErrorsWithCodes(<ErrorCode>[StaticWarningCode.UNDEFINED_CLASS]);
2803 }
2804 }
2805
2806 @reflectiveTest
2807 class ResolveVariableReferencesTaskTest extends _AbstractDartTaskTest {
2808 /**
2809 * Verify that the mutated states of the given [variable] correspond to the
2810 * [mutatedInClosure] and [mutatedInScope] matchers.
2811 */
2812 void expectMutated(VariableElement variable, Matcher mutatedInClosure,
2813 Matcher mutatedInScope) {
2814 expect(variable.isPotentiallyMutatedInClosure, mutatedInClosure);
2815 expect(variable.isPotentiallyMutatedInScope, mutatedInScope);
2816 }
2817
2818 test_perform_buildClosureLibraryElements() {
2819 Source source = newSource(
2820 '/test.dart',
2821 '''
2822 main() {
2823 }
2824 ''');
2825 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2826 computeResult(target, RESOLVED_UNIT4,
2827 matcher: isResolveVariableReferencesTask);
2828 }
2829
2830 test_perform_local() {
2831 Source source = newSource(
2832 '/test.dart',
2833 '''
2834 main() {
2835 var v1 = 1;
2836 var v2 = 1;
2837 var v3 = 1;
2838 var v4 = 1;
2839 v2 = 2;
2840 v4 = 2;
2841 localFunction() {
2842 v3 = 3;
2843 v4 = 3;
2844 }
2845 }
2846 ''');
2847 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2848 computeResult(target, RESOLVED_UNIT4,
2849 matcher: isResolveVariableReferencesTask);
2850 // validate
2851 CompilationUnit unit = outputs[RESOLVED_UNIT4];
2852 FunctionElement main = unit.element.functions[0];
2853 expectMutated(main.localVariables[0], isFalse, isFalse);
2854 expectMutated(main.localVariables[1], isFalse, isTrue);
2855 expectMutated(main.localVariables[2], isTrue, isTrue);
2856 expectMutated(main.localVariables[3], isTrue, isTrue);
2857 }
2858
2859 test_perform_parameter() {
2860 Source source = newSource(
2861 '/test.dart',
2862 '''
2863 main(p1, p2, p3, p4) {
2864 p2 = 2;
2865 p4 = 2;
2866 localFunction() {
2867 p3 = 3;
2868 p4 = 3;
2869 }
2870 }
2871 ''');
2872 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2873 computeResult(target, RESOLVED_UNIT4,
2874 matcher: isResolveVariableReferencesTask);
2875 // validate
2876 CompilationUnit unit = outputs[RESOLVED_UNIT4];
2877 FunctionElement main = unit.element.functions[0];
2878 expectMutated(main.parameters[0], isFalse, isFalse);
2879 expectMutated(main.parameters[1], isFalse, isTrue);
2880 expectMutated(main.parameters[2], isTrue, isTrue);
2881 expectMutated(main.parameters[3], isTrue, isTrue);
2882 }
2883 }
2884
2885 @reflectiveTest
2886 class ScanDartTaskTest extends _AbstractDartTaskTest {
2887 test_perform_errors() {
2888 _performScanTask('import "');
2889 expect(outputs, hasLength(3));
2890 expect(outputs[LINE_INFO], isNotNull);
2891 expect(outputs[SCAN_ERRORS], hasLength(1));
2892 expect(outputs[TOKEN_STREAM], isNotNull);
2893 }
2894
2895 test_perform_noErrors() {
2896 _performScanTask('class A {}');
2897 expect(outputs, hasLength(3));
2898 expect(outputs[LINE_INFO], isNotNull);
2899 expect(outputs[SCAN_ERRORS], hasLength(0));
2900 expect(outputs[TOKEN_STREAM], isNotNull);
2901 }
2902
2903 test_perform_script() {
2904 String scriptContent = '''
2905 void buttonPressed() {
2906 ''';
2907 String htmlContent = '''
2908 <!DOCTYPE html>
2909 <html>
2910 <head>
2911 <title>test page</title>
2912 <script type='application/dart'>$scriptContent</script>
2913 </head>
2914 <body>Test</body>
2915 </html>
2916 ''';
2917 Source source = newSource('/test.html', htmlContent);
2918 DartScript script =
2919 new DartScript(source, [new ScriptFragment(97, 5, 36, scriptContent)]);
2920
2921 computeResult(script, TOKEN_STREAM, matcher: isScanDartTask);
2922 expect(outputs[LINE_INFO], isNotNull);
2923 expect(outputs[SCAN_ERRORS], isEmpty);
2924 Token tokenStream = outputs[TOKEN_STREAM];
2925 expect(tokenStream, isNotNull);
2926 expect(tokenStream.lexeme, 'void');
2927 }
2928
2929 void _performScanTask(String content) {
2930 AnalysisTarget target = newSource('/test.dart', content);
2931 computeResult(target, TOKEN_STREAM, matcher: isScanDartTask);
2932 }
2933 }
2934
2935 @reflectiveTest
2597 class StrongModeInferenceTest extends _AbstractDartTaskTest { 2936 class StrongModeInferenceTest extends _AbstractDartTaskTest {
2598 2937 void assertAssignmentStatementTypes(
2599 @override 2938 Statement stmt, DartType leftType, DartType rightType) {
2600 void setUp() { 2939 AssignmentExpression assgn = (stmt as ExpressionStatement).expression;
2601 super.setUp(); 2940 expect(assgn.leftHandSide.staticType, leftType);
2602 enableStrongMode(); 2941 expect(assgn.rightHandSide.staticType, rightType);
2603 } 2942 }
2604 2943
2605 // Check that even within a static variable cycle, inferred 2944 // Check that even within a static variable cycle, inferred
2606 // types get propagated to the members of the cycle. 2945 // types get propagated to the members of the cycle.
2607 void test_perform_cycle() {
2608 AnalysisTarget source = newSource(
2609 '/test.dart',
2610 '''
2611 var piFirst = true;
2612 var pi = piFirst ? 3.14 : tau / 2;
2613 var tau = piFirst ? pi * 2 : 6.28;
2614 ''');
2615 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2616 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2617 VariableElement piFirst =
2618 getTopLevelVariable(unit, 'piFirst').name.staticElement;
2619 VariableElement pi = getTopLevelVariable(unit, 'pi').name.staticElement;
2620 VariableElement tau = getTopLevelVariable(unit, 'tau').name.staticElement;
2621 Expression piFirstUse = (getTopLevelVariable(unit, 'tau').initializer
2622 as ConditionalExpression).condition;
2623
2624 expect(piFirstUse.staticType, context.typeProvider.boolType);
2625 expect(piFirst.type, context.typeProvider.boolType);
2626 expect(pi.type.isDynamic, isTrue);
2627 expect(tau.type.isDynamic, isTrue);
2628 }
2629
2630 void test_perform_local_explicit_disabled() {
2631 AnalysisTarget source = newSource(
2632 '/test.dart',
2633 '''
2634 test() {
2635 int x = 3;
2636 x = "hi";
2637 }
2638 ''');
2639 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2640 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2641
2642 InterfaceType intType = context.typeProvider.intType;
2643 InterfaceType stringType = context.typeProvider.stringType;
2644
2645 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
2646 VariableDeclaration decl =
2647 (statements[0] as VariableDeclarationStatement).variables.variables[0];
2648 expect(decl.element.type, intType);
2649 expect(decl.initializer.staticType, intType);
2650
2651 ExpressionStatement statement = statements[1];
2652 AssignmentExpression assgn = statement.expression;
2653 expect(assgn.leftHandSide.staticType, intType);
2654 expect(assgn.rightHandSide.staticType, stringType);
2655 }
2656
2657 void assertVariableDeclarationTypes(
2658 VariableDeclaration decl, DartType varType, DartType initializerType) {
2659 expect(decl.element.type, varType);
2660 expect(decl.initializer.staticType, initializerType);
2661 }
2662
2663 void assertVariableDeclarationStatementTypes( 2946 void assertVariableDeclarationStatementTypes(
2664 Statement stmt, DartType varType, DartType initializerType) { 2947 Statement stmt, DartType varType, DartType initializerType) {
2665 VariableDeclaration decl = 2948 VariableDeclaration decl =
2666 (stmt as VariableDeclarationStatement).variables.variables[0]; 2949 (stmt as VariableDeclarationStatement).variables.variables[0];
2667 assertVariableDeclarationTypes(decl, varType, initializerType); 2950 assertVariableDeclarationTypes(decl, varType, initializerType);
2668 } 2951 }
2669 2952
2670 void assertAssignmentStatementTypes( 2953 void assertVariableDeclarationTypes(
2671 Statement stmt, DartType leftType, DartType rightType) { 2954 VariableDeclaration decl, DartType varType, DartType initializerType) {
2672 AssignmentExpression assgn = (stmt as ExpressionStatement).expression; 2955 expect(decl.element.type, varType);
2673 expect(assgn.leftHandSide.staticType, leftType); 2956 expect(decl.initializer.staticType, initializerType);
2674 expect(assgn.rightHandSide.staticType, rightType); 2957 }
2675 } 2958
2676
2677 // Test that local variables in method bodies are inferred appropriately
2678 void test_perform_inference_local_variables() {
2679 AnalysisTarget source = newSource(
2680 '/test.dart',
2681 '''
2682 test() {
2683 int x = 3;
2684 x = "hi";
2685 var y = 3;
2686 y = "hi";
2687 }
2688 ''');
2689 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2690 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2691
2692 InterfaceType intType = context.typeProvider.intType;
2693 InterfaceType stringType = context.typeProvider.stringType;
2694
2695 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
2696
2697 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
2698 assertAssignmentStatementTypes(statements[1], intType, stringType);
2699 assertVariableDeclarationStatementTypes(statements[2], intType, intType);
2700 assertAssignmentStatementTypes(statements[3], intType, stringType);
2701 }
2702
2703 // Test inference interactions between local variables and fields
2704 void test_perform_inference_local_variables_fields() {
2705 AnalysisTarget source = newSource(
2706 '/test.dart',
2707 '''
2708 class A {
2709 int x = 0;
2710
2711 test1() {
2712 var a = x;
2713 a = "hi";
2714 a = 3;
2715 var b = y;
2716 b = "hi";
2717 b = 4;
2718 var c = z;
2719 c = "hi";
2720 c = 4;
2721 }
2722
2723 int y; // field def after use
2724 final z = 42; // should infer `int`
2725 }
2726 ''');
2727 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2728 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2729
2730 InterfaceType intType = context.typeProvider.intType;
2731 InterfaceType stringType = context.typeProvider.stringType;
2732
2733 List<Statement> statements = getStatementsInMethod(unit, "A", "test1");
2734
2735 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
2736 assertAssignmentStatementTypes(statements[1], intType, stringType);
2737 assertAssignmentStatementTypes(statements[2], intType, intType);
2738
2739 assertVariableDeclarationStatementTypes(statements[3], intType, intType);
2740 assertAssignmentStatementTypes(statements[4], intType, stringType);
2741 assertAssignmentStatementTypes(statements[5], intType, intType);
2742
2743 assertVariableDeclarationStatementTypes(statements[6], intType, intType);
2744 assertAssignmentStatementTypes(statements[7], intType, stringType);
2745 assertAssignmentStatementTypes(statements[8], intType, intType);
2746
2747 assertVariableDeclarationTypes(
2748 getFieldInClass(unit, "A", "x"), intType, intType);
2749 assertVariableDeclarationTypes(
2750 getFieldInClass(unit, "A", "z"), intType, intType);
2751 }
2752
2753 // Test inference interactions between local variables and top level
2754 // variables
2755 void test_perform_inference_local_variables_topLevel() {
2756 AnalysisTarget source = newSource(
2757 '/test.dart',
2758 '''
2759 int x = 0;
2760
2761 test1() {
2762 var a = x;
2763 a = /*severe:StaticTypeError*/"hi";
2764 a = 3;
2765 var b = y;
2766 b = /*severe:StaticTypeError*/"hi";
2767 b = 4;
2768 var c = z;
2769 c = /*severe:StaticTypeError*/"hi";
2770 c = 4;
2771 }
2772
2773 int y = 0; // field def after use
2774 final z = 42; // should infer `int`
2775 ''');
2776 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2777 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2778
2779 InterfaceType intType = context.typeProvider.intType;
2780 InterfaceType stringType = context.typeProvider.stringType;
2781
2782 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test1");
2783
2784 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
2785 assertAssignmentStatementTypes(statements[1], intType, stringType);
2786 assertAssignmentStatementTypes(statements[2], intType, intType);
2787
2788 assertVariableDeclarationStatementTypes(statements[3], intType, intType);
2789 assertAssignmentStatementTypes(statements[4], intType, stringType);
2790 assertAssignmentStatementTypes(statements[5], intType, intType);
2791
2792 assertVariableDeclarationStatementTypes(statements[6], intType, intType);
2793 assertAssignmentStatementTypes(statements[7], intType, stringType);
2794 assertAssignmentStatementTypes(statements[8], intType, intType);
2795
2796 assertVariableDeclarationTypes(
2797 getTopLevelVariable(unit, "x"), intType, intType);
2798 assertVariableDeclarationTypes(
2799 getTopLevelVariable(unit, "y"), intType, intType);
2800 assertVariableDeclarationTypes(
2801 getTopLevelVariable(unit, "z"), intType, intType);
2802 }
2803
2804 // Test that inference does not propagate from null
2805 void test_perform_inference_null() {
2806 AnalysisTarget source = newSource(
2807 '/test.dart',
2808 '''
2809 var x = null;
2810 var y = 3;
2811 class A {
2812 static var x = null;
2813 static var y = 3;
2814
2815 var x2 = null;
2816 var y2 = 3;
2817 }
2818
2819 test() {
2820 x = "hi";
2821 y = /*severe:StaticTypeError*/"hi";
2822 A.x = "hi";
2823 A.y = /*severe:StaticTypeError*/"hi";
2824 new A().x2 = "hi";
2825 new A().y2 = /*severe:StaticTypeError*/"hi";
2826 }
2827 ''');
2828 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2829 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2830
2831 InterfaceType intType = context.typeProvider.intType;
2832 InterfaceType stringType = context.typeProvider.stringType;
2833 DartType bottomType = context.typeProvider.bottomType;
2834 DartType dynamicType = context.typeProvider.dynamicType;
2835
2836 assertVariableDeclarationTypes(
2837 getTopLevelVariable(unit, "x"), dynamicType, bottomType);
2838 assertVariableDeclarationTypes(
2839 getTopLevelVariable(unit, "y"), intType, intType);
2840 assertVariableDeclarationTypes(
2841 getFieldInClass(unit, "A", "x"), dynamicType, bottomType);
2842 assertVariableDeclarationTypes(
2843 getFieldInClass(unit, "A", "y"), intType, intType);
2844 assertVariableDeclarationTypes(
2845 getFieldInClass(unit, "A", "x2"), dynamicType, bottomType);
2846 assertVariableDeclarationTypes(
2847 getFieldInClass(unit, "A", "y2"), intType, intType);
2848
2849 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
2850
2851 assertAssignmentStatementTypes(statements[0], dynamicType, stringType);
2852 assertAssignmentStatementTypes(statements[1], intType, stringType);
2853 assertAssignmentStatementTypes(statements[2], dynamicType, stringType);
2854 assertAssignmentStatementTypes(statements[3], intType, stringType);
2855 assertAssignmentStatementTypes(statements[4], dynamicType, stringType);
2856 assertAssignmentStatementTypes(statements[5], intType, stringType);
2857 }
2858
2859 // Test inference across units (non-cyclic)
2860 void test_perform_inference_cross_unit_non_cyclic() {
2861 AnalysisTarget firstSource = newSource(
2862 '/a.dart',
2863 '''
2864 var x = 2;
2865 class A { static var x = 2; }
2866 ''');
2867 AnalysisTarget secondSource = newSource(
2868 '/test.dart',
2869 '''
2870 import 'a.dart';
2871 var y = x;
2872 class B { static var y = A.x; }
2873
2874 test1() {
2875 x = /*severe:StaticTypeError*/"hi";
2876 y = /*severe:StaticTypeError*/"hi";
2877 A.x = /*severe:StaticTypeError*/"hi";
2878 B.y = /*severe:StaticTypeError*/"hi";
2879 }
2880 ''');
2881 computeResult(
2882 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT8);
2883 CompilationUnit unit1 = outputs[RESOLVED_UNIT8];
2884 computeResult(
2885 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT8);
2886 CompilationUnit unit2 = outputs[RESOLVED_UNIT8];
2887
2888 InterfaceType intType = context.typeProvider.intType;
2889 InterfaceType stringType = context.typeProvider.stringType;
2890
2891 assertVariableDeclarationTypes(
2892 getTopLevelVariable(unit1, "x"), intType, intType);
2893 assertVariableDeclarationTypes(
2894 getFieldInClass(unit1, "A", "x"), intType, intType);
2895
2896 assertVariableDeclarationTypes(
2897 getTopLevelVariable(unit2, "y"), intType, intType);
2898 assertVariableDeclarationTypes(
2899 getFieldInClass(unit2, "B", "y"), intType, intType);
2900
2901 List<Statement> statements =
2902 getStatementsInTopLevelFunction(unit2, "test1");
2903
2904 assertAssignmentStatementTypes(statements[0], intType, stringType);
2905 assertAssignmentStatementTypes(statements[1], intType, stringType);
2906 }
2907
2908 // Test inference across units (cyclic)
2909 void test_perform_inference_cross_unit_cyclic() {
2910 AnalysisTarget firstSource = newSource(
2911 '/a.dart',
2912 '''
2913 import 'test.dart';
2914 var x = 2;
2915 class A { static var x = 2; }
2916 ''');
2917 AnalysisTarget secondSource = newSource(
2918 '/test.dart',
2919 '''
2920 import 'a.dart';
2921 var y = x;
2922 class B { static var y = A.x; }
2923
2924 test1() {
2925 int t = 3;
2926 t = x;
2927 t = y;
2928 t = A.x;
2929 t = B.y;
2930 }
2931 ''');
2932 computeResult(
2933 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT8);
2934 CompilationUnit unit1 = outputs[RESOLVED_UNIT8];
2935 computeResult(
2936 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT8);
2937 CompilationUnit unit2 = outputs[RESOLVED_UNIT8];
2938
2939 InterfaceType intType = context.typeProvider.intType;
2940
2941 assertVariableDeclarationTypes(
2942 getTopLevelVariable(unit1, "x"), intType, intType);
2943 assertVariableDeclarationTypes(
2944 getFieldInClass(unit1, "A", "x"), intType, intType);
2945
2946 assertVariableDeclarationTypes(
2947 getTopLevelVariable(unit2, "y"), intType, intType);
2948 assertVariableDeclarationTypes(
2949 getFieldInClass(unit2, "B", "y"), intType, intType);
2950
2951 List<Statement> statements =
2952 getStatementsInTopLevelFunction(unit2, "test1");
2953
2954 assertAssignmentStatementTypes(statements[1], intType, intType);
2955 assertAssignmentStatementTypes(statements[2], intType, intType);
2956 assertAssignmentStatementTypes(statements[3], intType, intType);
2957 assertAssignmentStatementTypes(statements[4], intType, intType);
2958 }
2959
2960 // Test inference of instance fields across units
2961 // TODO(leafp): Fix this
2962 // https://github.com/dart-lang/dev_compiler/issues/354
2963 void fail_perform_inference_cross_unit_instance() { 2959 void fail_perform_inference_cross_unit_instance() {
2964 List<Source> sources = newSources({ 2960 List<Source> sources = newSources({
2965 '/a7.dart': ''' 2961 '/a7.dart': '''
2966 import 'b7.dart'; 2962 import 'b7.dart';
2967 class A { 2963 class A {
2968 final a2 = new B().b2; 2964 final a2 = new B().b2;
2969 } 2965 }
2970 ''', 2966 ''',
2971 '/b7.dart': ''' 2967 '/b7.dart': '''
2972 class B { 2968 class B {
(...skipping 22 matching lines...) Expand all
2995 2991
2996 assertVariableDeclarationTypes( 2992 assertVariableDeclarationTypes(
2997 getFieldInClass(unit1, "B", "b2"), intType, intType); 2993 getFieldInClass(unit1, "B", "b2"), intType, intType);
2998 2994
2999 List<Statement> statements = 2995 List<Statement> statements =
3000 getStatementsInTopLevelFunction(unit2, "test1"); 2996 getStatementsInTopLevelFunction(unit2, "test1");
3001 2997
3002 assertAssignmentStatementTypes(statements[1], intType, intType); 2998 assertAssignmentStatementTypes(statements[1], intType, intType);
3003 } 2999 }
3004 3000
3005 // Test inference between static and instance fields
3006 // TODO(leafp): Fix this
3007 // https://github.com/dart-lang/dev_compiler/issues/354
3008 void fail_perform_inference_cross_unit_static_instance() { 3001 void fail_perform_inference_cross_unit_static_instance() {
3009 List<Source> sources = newSources({ 3002 List<Source> sources = newSources({
3010 '/a.dart': ''' 3003 '/a.dart': '''
3011 import 'b.dart'; 3004 import 'b.dart';
3012 class A { 3005 class A {
3013 static final a1 = B.b1; 3006 static final a1 = B.b1;
3014 final a2 = new B().b2; 3007 final a2 = new B().b2;
3015 } 3008 }
3016 ''', 3009 ''',
3017 '/b.dart': ''' 3010 '/b.dart': '''
(...skipping 30 matching lines...) Expand all
3048 getFieldInClass(unit1, "B", "b1"), intType, intType); 3041 getFieldInClass(unit1, "B", "b1"), intType, intType);
3049 assertVariableDeclarationTypes( 3042 assertVariableDeclarationTypes(
3050 getFieldInClass(unit1, "B", "b2"), intType, intType); 3043 getFieldInClass(unit1, "B", "b2"), intType, intType);
3051 3044
3052 List<Statement> statements = 3045 List<Statement> statements =
3053 getStatementsInTopLevelFunction(unit2, "test1"); 3046 getStatementsInTopLevelFunction(unit2, "test1");
3054 3047
3055 assertAssignmentStatementTypes(statements[1], intType, intType); 3048 assertAssignmentStatementTypes(statements[1], intType, intType);
3056 assertAssignmentStatementTypes(statements[2], intType, intType); 3049 assertAssignmentStatementTypes(statements[2], intType, intType);
3057 } 3050 }
3051
3052 @override
3053 void setUp() {
3054 super.setUp();
3055 enableStrongMode();
3056 }
3057
3058 // Test that local variables in method bodies are inferred appropriately
3059 void test_perform_cycle() {
3060 AnalysisTarget source = newSource(
3061 '/test.dart',
3062 '''
3063 var piFirst = true;
3064 var pi = piFirst ? 3.14 : tau / 2;
3065 var tau = piFirst ? pi * 2 : 6.28;
3066 ''');
3067 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
3068 CompilationUnit unit = outputs[RESOLVED_UNIT8];
3069 VariableElement piFirst =
3070 getTopLevelVariable(unit, 'piFirst').name.staticElement;
3071 VariableElement pi = getTopLevelVariable(unit, 'pi').name.staticElement;
3072 VariableElement tau = getTopLevelVariable(unit, 'tau').name.staticElement;
3073 Expression piFirstUse = (getTopLevelVariable(unit, 'tau').initializer
3074 as ConditionalExpression).condition;
3075
3076 expect(piFirstUse.staticType, context.typeProvider.boolType);
3077 expect(piFirst.type, context.typeProvider.boolType);
3078 expect(pi.type.isDynamic, isTrue);
3079 expect(tau.type.isDynamic, isTrue);
3080 }
3081
3082 // Test inference interactions between local variables and fields
3083 void test_perform_inference_cross_unit_cyclic() {
3084 AnalysisTarget firstSource = newSource(
3085 '/a.dart',
3086 '''
3087 import 'test.dart';
3088 var x = 2;
3089 class A { static var x = 2; }
3090 ''');
3091 AnalysisTarget secondSource = newSource(
3092 '/test.dart',
3093 '''
3094 import 'a.dart';
3095 var y = x;
3096 class B { static var y = A.x; }
3097
3098 test1() {
3099 int t = 3;
3100 t = x;
3101 t = y;
3102 t = A.x;
3103 t = B.y;
3104 }
3105 ''');
3106 computeResult(
3107 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT8);
3108 CompilationUnit unit1 = outputs[RESOLVED_UNIT8];
3109 computeResult(
3110 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT8);
3111 CompilationUnit unit2 = outputs[RESOLVED_UNIT8];
3112
3113 InterfaceType intType = context.typeProvider.intType;
3114
3115 assertVariableDeclarationTypes(
3116 getTopLevelVariable(unit1, "x"), intType, intType);
3117 assertVariableDeclarationTypes(
3118 getFieldInClass(unit1, "A", "x"), intType, intType);
3119
3120 assertVariableDeclarationTypes(
3121 getTopLevelVariable(unit2, "y"), intType, intType);
3122 assertVariableDeclarationTypes(
3123 getFieldInClass(unit2, "B", "y"), intType, intType);
3124
3125 List<Statement> statements =
3126 getStatementsInTopLevelFunction(unit2, "test1");
3127
3128 assertAssignmentStatementTypes(statements[1], intType, intType);
3129 assertAssignmentStatementTypes(statements[2], intType, intType);
3130 assertAssignmentStatementTypes(statements[3], intType, intType);
3131 assertAssignmentStatementTypes(statements[4], intType, intType);
3132 }
3133
3134 // Test inference interactions between local variables and top level
3135 // variables
3136 void test_perform_inference_cross_unit_non_cyclic() {
3137 AnalysisTarget firstSource = newSource(
3138 '/a.dart',
3139 '''
3140 var x = 2;
3141 class A { static var x = 2; }
3142 ''');
3143 AnalysisTarget secondSource = newSource(
3144 '/test.dart',
3145 '''
3146 import 'a.dart';
3147 var y = x;
3148 class B { static var y = A.x; }
3149
3150 test1() {
3151 x = /*severe:StaticTypeError*/"hi";
3152 y = /*severe:StaticTypeError*/"hi";
3153 A.x = /*severe:StaticTypeError*/"hi";
3154 B.y = /*severe:StaticTypeError*/"hi";
3155 }
3156 ''');
3157 computeResult(
3158 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT8);
3159 CompilationUnit unit1 = outputs[RESOLVED_UNIT8];
3160 computeResult(
3161 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT8);
3162 CompilationUnit unit2 = outputs[RESOLVED_UNIT8];
3163
3164 InterfaceType intType = context.typeProvider.intType;
3165 InterfaceType stringType = context.typeProvider.stringType;
3166
3167 assertVariableDeclarationTypes(
3168 getTopLevelVariable(unit1, "x"), intType, intType);
3169 assertVariableDeclarationTypes(
3170 getFieldInClass(unit1, "A", "x"), intType, intType);
3171
3172 assertVariableDeclarationTypes(
3173 getTopLevelVariable(unit2, "y"), intType, intType);
3174 assertVariableDeclarationTypes(
3175 getFieldInClass(unit2, "B", "y"), intType, intType);
3176
3177 List<Statement> statements =
3178 getStatementsInTopLevelFunction(unit2, "test1");
3179
3180 assertAssignmentStatementTypes(statements[0], intType, stringType);
3181 assertAssignmentStatementTypes(statements[1], intType, stringType);
3182 }
3183
3184 // Test that inference does not propagate from null
3185 void test_perform_inference_local_variables() {
3186 AnalysisTarget source = newSource(
3187 '/test.dart',
3188 '''
3189 test() {
3190 int x = 3;
3191 x = "hi";
3192 var y = 3;
3193 y = "hi";
3194 }
3195 ''');
3196 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
3197 CompilationUnit unit = outputs[RESOLVED_UNIT8];
3198
3199 InterfaceType intType = context.typeProvider.intType;
3200 InterfaceType stringType = context.typeProvider.stringType;
3201
3202 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
3203
3204 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
3205 assertAssignmentStatementTypes(statements[1], intType, stringType);
3206 assertVariableDeclarationStatementTypes(statements[2], intType, intType);
3207 assertAssignmentStatementTypes(statements[3], intType, stringType);
3208 }
3209
3210 // Test inference across units (non-cyclic)
3211 void test_perform_inference_local_variables_fields() {
3212 AnalysisTarget source = newSource(
3213 '/test.dart',
3214 '''
3215 class A {
3216 int x = 0;
3217
3218 test1() {
3219 var a = x;
3220 a = "hi";
3221 a = 3;
3222 var b = y;
3223 b = "hi";
3224 b = 4;
3225 var c = z;
3226 c = "hi";
3227 c = 4;
3228 }
3229
3230 int y; // field def after use
3231 final z = 42; // should infer `int`
3232 }
3233 ''');
3234 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
3235 CompilationUnit unit = outputs[RESOLVED_UNIT8];
3236
3237 InterfaceType intType = context.typeProvider.intType;
3238 InterfaceType stringType = context.typeProvider.stringType;
3239
3240 List<Statement> statements = getStatementsInMethod(unit, "A", "test1");
3241
3242 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
3243 assertAssignmentStatementTypes(statements[1], intType, stringType);
3244 assertAssignmentStatementTypes(statements[2], intType, intType);
3245
3246 assertVariableDeclarationStatementTypes(statements[3], intType, intType);
3247 assertAssignmentStatementTypes(statements[4], intType, stringType);
3248 assertAssignmentStatementTypes(statements[5], intType, intType);
3249
3250 assertVariableDeclarationStatementTypes(statements[6], intType, intType);
3251 assertAssignmentStatementTypes(statements[7], intType, stringType);
3252 assertAssignmentStatementTypes(statements[8], intType, intType);
3253
3254 assertVariableDeclarationTypes(
3255 getFieldInClass(unit, "A", "x"), intType, intType);
3256 assertVariableDeclarationTypes(
3257 getFieldInClass(unit, "A", "z"), intType, intType);
3258 }
3259
3260 // Test inference across units (cyclic)
3261 void test_perform_inference_local_variables_topLevel() {
3262 AnalysisTarget source = newSource(
3263 '/test.dart',
3264 '''
3265 int x = 0;
3266
3267 test1() {
3268 var a = x;
3269 a = /*severe:StaticTypeError*/"hi";
3270 a = 3;
3271 var b = y;
3272 b = /*severe:StaticTypeError*/"hi";
3273 b = 4;
3274 var c = z;
3275 c = /*severe:StaticTypeError*/"hi";
3276 c = 4;
3277 }
3278
3279 int y = 0; // field def after use
3280 final z = 42; // should infer `int`
3281 ''');
3282 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
3283 CompilationUnit unit = outputs[RESOLVED_UNIT8];
3284
3285 InterfaceType intType = context.typeProvider.intType;
3286 InterfaceType stringType = context.typeProvider.stringType;
3287
3288 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test1");
3289
3290 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
3291 assertAssignmentStatementTypes(statements[1], intType, stringType);
3292 assertAssignmentStatementTypes(statements[2], intType, intType);
3293
3294 assertVariableDeclarationStatementTypes(statements[3], intType, intType);
3295 assertAssignmentStatementTypes(statements[4], intType, stringType);
3296 assertAssignmentStatementTypes(statements[5], intType, intType);
3297
3298 assertVariableDeclarationStatementTypes(statements[6], intType, intType);
3299 assertAssignmentStatementTypes(statements[7], intType, stringType);
3300 assertAssignmentStatementTypes(statements[8], intType, intType);
3301
3302 assertVariableDeclarationTypes(
3303 getTopLevelVariable(unit, "x"), intType, intType);
3304 assertVariableDeclarationTypes(
3305 getTopLevelVariable(unit, "y"), intType, intType);
3306 assertVariableDeclarationTypes(
3307 getTopLevelVariable(unit, "z"), intType, intType);
3308 }
3309
3310 // Test inference of instance fields across units
3311 // TODO(leafp): Fix this
3312 // https://github.com/dart-lang/dev_compiler/issues/354
3313 void test_perform_inference_null() {
3314 AnalysisTarget source = newSource(
3315 '/test.dart',
3316 '''
3317 var x = null;
3318 var y = 3;
3319 class A {
3320 static var x = null;
3321 static var y = 3;
3322
3323 var x2 = null;
3324 var y2 = 3;
3325 }
3326
3327 test() {
3328 x = "hi";
3329 y = /*severe:StaticTypeError*/"hi";
3330 A.x = "hi";
3331 A.y = /*severe:StaticTypeError*/"hi";
3332 new A().x2 = "hi";
3333 new A().y2 = /*severe:StaticTypeError*/"hi";
3334 }
3335 ''');
3336 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
3337 CompilationUnit unit = outputs[RESOLVED_UNIT8];
3338
3339 InterfaceType intType = context.typeProvider.intType;
3340 InterfaceType stringType = context.typeProvider.stringType;
3341 DartType bottomType = context.typeProvider.bottomType;
3342 DartType dynamicType = context.typeProvider.dynamicType;
3343
3344 assertVariableDeclarationTypes(
3345 getTopLevelVariable(unit, "x"), dynamicType, bottomType);
3346 assertVariableDeclarationTypes(
3347 getTopLevelVariable(unit, "y"), intType, intType);
3348 assertVariableDeclarationTypes(
3349 getFieldInClass(unit, "A", "x"), dynamicType, bottomType);
3350 assertVariableDeclarationTypes(
3351 getFieldInClass(unit, "A", "y"), intType, intType);
3352 assertVariableDeclarationTypes(
3353 getFieldInClass(unit, "A", "x2"), dynamicType, bottomType);
3354 assertVariableDeclarationTypes(
3355 getFieldInClass(unit, "A", "y2"), intType, intType);
3356
3357 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
3358
3359 assertAssignmentStatementTypes(statements[0], dynamicType, stringType);
3360 assertAssignmentStatementTypes(statements[1], intType, stringType);
3361 assertAssignmentStatementTypes(statements[2], dynamicType, stringType);
3362 assertAssignmentStatementTypes(statements[3], intType, stringType);
3363 assertAssignmentStatementTypes(statements[4], dynamicType, stringType);
3364 assertAssignmentStatementTypes(statements[5], intType, stringType);
3365 }
3366
3367 // Test inference between static and instance fields
3368 // TODO(leafp): Fix this
3369 // https://github.com/dart-lang/dev_compiler/issues/354
3370 void test_perform_local_explicit_disabled() {
3371 AnalysisTarget source = newSource(
3372 '/test.dart',
3373 '''
3374 test() {
3375 int x = 3;
3376 x = "hi";
3377 }
3378 ''');
3379 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
3380 CompilationUnit unit = outputs[RESOLVED_UNIT8];
3381
3382 InterfaceType intType = context.typeProvider.intType;
3383 InterfaceType stringType = context.typeProvider.stringType;
3384
3385 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
3386 VariableDeclaration decl =
3387 (statements[0] as VariableDeclarationStatement).variables.variables[0];
3388 expect(decl.element.type, intType);
3389 expect(decl.initializer.staticType, intType);
3390
3391 ExpressionStatement statement = statements[1];
3392 AssignmentExpression assgn = statement.expression;
3393 expect(assgn.leftHandSide.staticType, intType);
3394 expect(assgn.rightHandSide.staticType, stringType);
3395 }
3058 } 3396 }
3059 3397
3060 @reflectiveTest
3061 class ResolveLibraryTypeNamesTaskTest extends _AbstractDartTaskTest {
3062 test_perform() {
3063 Source sourceLib = newSource(
3064 '/my_lib.dart',
3065 '''
3066 library my_lib;
3067 part 'my_part.dart';
3068 class A {}
3069 class B extends A {}
3070 ''');
3071 newSource(
3072 '/my_part.dart',
3073 '''
3074 part of my_lib;
3075 class C extends A {}
3076 ''');
3077 computeResult(sourceLib, LIBRARY_ELEMENT5,
3078 matcher: isResolveLibraryTypeNamesTask);
3079 // validate
3080 LibraryElement library = outputs[LIBRARY_ELEMENT5];
3081 {
3082 ClassElement classB = library.getType('B');
3083 expect(classB.supertype.displayName, 'A');
3084 }
3085 {
3086 ClassElement classC = library.getType('C');
3087 expect(classC.supertype.displayName, 'A');
3088 }
3089 }
3090
3091 test_perform_external() {
3092 Source sourceA = newSource(
3093 '/a.dart',
3094 '''
3095 library a;
3096 import 'b.dart';
3097 class A extends B {}
3098 ''');
3099 newSource(
3100 '/b.dart',
3101 '''
3102 library b;
3103 class B {}
3104 ''');
3105 // The reference A to B should be resolved, but there's no requirement that
3106 // the full class hierarchy be resolved.
3107 computeResult(sourceA, LIBRARY_ELEMENT5,
3108 matcher: isResolveLibraryTypeNamesTask);
3109 // validate
3110 LibraryElement library = outputs[LIBRARY_ELEMENT5];
3111 {
3112 ClassElement clazz = library.getType('A');
3113 expect(clazz.displayName, 'A');
3114 clazz = clazz.supertype.element;
3115 expect(clazz.displayName, 'B');
3116 }
3117 }
3118 }
3119
3120 @reflectiveTest
3121 class ResolveUnitTypeNamesTaskTest extends _AbstractDartTaskTest {
3122 test_perform() {
3123 Source source = newSource(
3124 '/test.dart',
3125 '''
3126 class A {}
3127 class B extends A {}
3128 int f(String p) => p.length;
3129 ''');
3130 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3131 computeResult(target, RESOLVED_UNIT3, matcher: isResolveUnitTypeNamesTask);
3132 // validate
3133 CompilationUnit unit = outputs[RESOLVED_UNIT3];
3134 {
3135 ClassDeclaration nodeA = unit.declarations[0];
3136 ClassDeclaration nodeB = unit.declarations[1];
3137 DartType extendsType = nodeB.extendsClause.superclass.type;
3138 expect(extendsType, nodeA.element.type);
3139 }
3140 {
3141 FunctionDeclaration functionNode = unit.declarations[2];
3142 DartType returnType = functionNode.returnType.type;
3143 List<FormalParameter> parameters =
3144 functionNode.functionExpression.parameters.parameters;
3145 expect(returnType.displayName, 'int');
3146 expect(parameters[0].element.type.displayName, 'String');
3147 }
3148 }
3149
3150 test_perform_errors() {
3151 Source source = newSource(
3152 '/test.dart',
3153 '''
3154 NoSuchClass f() => null;
3155 ''');
3156 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3157 computeResult(target, RESOLVE_TYPE_NAMES_ERRORS,
3158 matcher: isResolveUnitTypeNamesTask);
3159 // validate
3160 _fillErrorListener(RESOLVE_TYPE_NAMES_ERRORS);
3161 errorListener
3162 .assertErrorsWithCodes(<ErrorCode>[StaticWarningCode.UNDEFINED_CLASS]);
3163 }
3164
3165 test_perform_typedef() {
3166 Source source = newSource(
3167 '/test.dart',
3168 '''
3169 typedef int F(G g);
3170 typedef String G(int p);
3171 ''');
3172 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3173 computeResult(target, RESOLVED_UNIT3, matcher: isResolveUnitTypeNamesTask);
3174 // validate
3175 CompilationUnit unit = outputs[RESOLVED_UNIT3];
3176 FunctionTypeAlias nodeF = unit.declarations[0];
3177 FunctionTypeAlias nodeG = unit.declarations[1];
3178 {
3179 FormalParameter parameter = nodeF.parameters.parameters[0];
3180 DartType parameterType = parameter.element.type;
3181 Element returnTypeElement = nodeF.returnType.type.element;
3182 expect(returnTypeElement.displayName, 'int');
3183 expect(parameterType.element, nodeG.element);
3184 }
3185 {
3186 FormalParameter parameter = nodeG.parameters.parameters[0];
3187 DartType parameterType = parameter.element.type;
3188 expect(nodeG.returnType.type.element.displayName, 'String');
3189 expect(parameterType.element.displayName, 'int');
3190 }
3191 }
3192
3193 test_perform_typedef_errors() {
3194 Source source = newSource(
3195 '/test.dart',
3196 '''
3197 typedef int F(NoSuchType p);
3198 ''');
3199 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3200 computeResult(target, RESOLVE_TYPE_NAMES_ERRORS,
3201 matcher: isResolveUnitTypeNamesTask);
3202 // validate
3203 _fillErrorListener(RESOLVE_TYPE_NAMES_ERRORS);
3204 errorListener
3205 .assertErrorsWithCodes(<ErrorCode>[StaticWarningCode.UNDEFINED_CLASS]);
3206 }
3207 }
3208
3209 @reflectiveTest
3210 class ResolveVariableReferencesTaskTest extends _AbstractDartTaskTest {
3211 /**
3212 * Verify that the mutated states of the given [variable] correspond to the
3213 * [mutatedInClosure] and [mutatedInScope] matchers.
3214 */
3215 void expectMutated(VariableElement variable, Matcher mutatedInClosure,
3216 Matcher mutatedInScope) {
3217 expect(variable.isPotentiallyMutatedInClosure, mutatedInClosure);
3218 expect(variable.isPotentiallyMutatedInScope, mutatedInScope);
3219 }
3220
3221 test_perform_buildClosureLibraryElements() {
3222 Source source = newSource(
3223 '/test.dart',
3224 '''
3225 main() {
3226 }
3227 ''');
3228 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3229 computeResult(target, RESOLVED_UNIT4,
3230 matcher: isResolveVariableReferencesTask);
3231 }
3232
3233 test_perform_local() {
3234 Source source = newSource(
3235 '/test.dart',
3236 '''
3237 main() {
3238 var v1 = 1;
3239 var v2 = 1;
3240 var v3 = 1;
3241 var v4 = 1;
3242 v2 = 2;
3243 v4 = 2;
3244 localFunction() {
3245 v3 = 3;
3246 v4 = 3;
3247 }
3248 }
3249 ''');
3250 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3251 computeResult(target, RESOLVED_UNIT4,
3252 matcher: isResolveVariableReferencesTask);
3253 // validate
3254 CompilationUnit unit = outputs[RESOLVED_UNIT4];
3255 FunctionElement main = unit.element.functions[0];
3256 expectMutated(main.localVariables[0], isFalse, isFalse);
3257 expectMutated(main.localVariables[1], isFalse, isTrue);
3258 expectMutated(main.localVariables[2], isTrue, isTrue);
3259 expectMutated(main.localVariables[3], isTrue, isTrue);
3260 }
3261
3262 test_perform_parameter() {
3263 Source source = newSource(
3264 '/test.dart',
3265 '''
3266 main(p1, p2, p3, p4) {
3267 p2 = 2;
3268 p4 = 2;
3269 localFunction() {
3270 p3 = 3;
3271 p4 = 3;
3272 }
3273 }
3274 ''');
3275 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3276 computeResult(target, RESOLVED_UNIT4,
3277 matcher: isResolveVariableReferencesTask);
3278 // validate
3279 CompilationUnit unit = outputs[RESOLVED_UNIT4];
3280 FunctionElement main = unit.element.functions[0];
3281 expectMutated(main.parameters[0], isFalse, isFalse);
3282 expectMutated(main.parameters[1], isFalse, isTrue);
3283 expectMutated(main.parameters[2], isTrue, isTrue);
3284 expectMutated(main.parameters[3], isTrue, isTrue);
3285 }
3286 }
3287
3288 @reflectiveTest
3289 class ScanDartTaskTest extends _AbstractDartTaskTest {
3290 test_perform_errors() {
3291 _performScanTask('import "');
3292 expect(outputs, hasLength(3));
3293 expect(outputs[LINE_INFO], isNotNull);
3294 expect(outputs[SCAN_ERRORS], hasLength(1));
3295 expect(outputs[TOKEN_STREAM], isNotNull);
3296 }
3297
3298 test_perform_noErrors() {
3299 _performScanTask('class A {}');
3300 expect(outputs, hasLength(3));
3301 expect(outputs[LINE_INFO], isNotNull);
3302 expect(outputs[SCAN_ERRORS], hasLength(0));
3303 expect(outputs[TOKEN_STREAM], isNotNull);
3304 }
3305
3306 test_perform_script() {
3307 String scriptContent = '''
3308 void buttonPressed() {
3309 ''';
3310 String htmlContent = '''
3311 <!DOCTYPE html>
3312 <html>
3313 <head>
3314 <title>test page</title>
3315 <script type='application/dart'>$scriptContent</script>
3316 </head>
3317 <body>Test</body>
3318 </html>
3319 ''';
3320 Source source = newSource('/test.html', htmlContent);
3321 DartScript script =
3322 new DartScript(source, [new ScriptFragment(97, 5, 36, scriptContent)]);
3323
3324 computeResult(script, TOKEN_STREAM, matcher: isScanDartTask);
3325 expect(outputs[LINE_INFO], isNotNull);
3326 expect(outputs[SCAN_ERRORS], isEmpty);
3327 Token tokenStream = outputs[TOKEN_STREAM];
3328 expect(tokenStream, isNotNull);
3329 expect(tokenStream.lexeme, 'void');
3330 }
3331
3332 void _performScanTask(String content) {
3333 AnalysisTarget target = newSource('/test.dart', content);
3334 computeResult(target, TOKEN_STREAM, matcher: isScanDartTask);
3335 }
3336 }
3337
3338 @reflectiveTest 3398 @reflectiveTest
3339 class VerifyUnitTaskTest extends _AbstractDartTaskTest { 3399 class VerifyUnitTaskTest extends _AbstractDartTaskTest {
3340 test_perform_constantError() { 3400 test_perform_constantError() {
3341 Source source = newSource( 3401 Source source = newSource(
3342 '/test.dart', 3402 '/test.dart',
3343 ''' 3403 '''
3344 main(int p) { 3404 main(int p) {
3345 const v = p; 3405 const v = p;
3346 } 3406 }
3347 '''); 3407 ''');
(...skipping 13 matching lines...) Expand all
3361 import 'no-such-file.dart'; 3421 import 'no-such-file.dart';
3362 '''); 3422 ''');
3363 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 3423 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3364 computeResult(target, VERIFY_ERRORS, matcher: isVerifyUnitTask); 3424 computeResult(target, VERIFY_ERRORS, matcher: isVerifyUnitTask);
3365 // validate 3425 // validate
3366 _fillErrorListener(VERIFY_ERRORS); 3426 _fillErrorListener(VERIFY_ERRORS);
3367 errorListener.assertErrorsWithCodes( 3427 errorListener.assertErrorsWithCodes(
3368 <ErrorCode>[CompileTimeErrorCode.URI_DOES_NOT_EXIST]); 3428 <ErrorCode>[CompileTimeErrorCode.URI_DOES_NOT_EXIST]);
3369 } 3429 }
3370 3430
3431 void test_perform_reresolution() {
3432 enableStrongMode();
3433 AnalysisTarget source = newSource(
3434 '/test.dart',
3435 '''
3436 const topLevel = 3;
3437 class C {
3438 String field = topLevel;
3439 }
3440 ''');
3441 computeResult(new LibrarySpecificUnit(source, source), VERIFY_ERRORS);
3442 // validate
3443 _fillErrorListener(VERIFY_ERRORS);
3444 errorListener.assertErrorsWithCodes(
3445 <ErrorCode>[StaticTypeWarningCode.INVALID_ASSIGNMENT]);
3446 }
3447
3371 test_perform_verifyError() { 3448 test_perform_verifyError() {
3372 Source source = newSource( 3449 Source source = newSource(
3373 '/test.dart', 3450 '/test.dart',
3374 ''' 3451 '''
3375 main() { 3452 main() {
3376 if (42) { 3453 if (42) {
3377 print('Not bool!'); 3454 print('Not bool!');
3378 } 3455 }
3379 } 3456 }
3380 '''); 3457 ''');
3381 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 3458 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3382 computeResult(target, VERIFY_ERRORS, matcher: isVerifyUnitTask); 3459 computeResult(target, VERIFY_ERRORS, matcher: isVerifyUnitTask);
3383 // validate 3460 // validate
3384 _fillErrorListener(VERIFY_ERRORS); 3461 _fillErrorListener(VERIFY_ERRORS);
3385 errorListener.assertErrorsWithCodes( 3462 errorListener.assertErrorsWithCodes(
3386 <ErrorCode>[StaticTypeWarningCode.NON_BOOL_CONDITION]); 3463 <ErrorCode>[StaticTypeWarningCode.NON_BOOL_CONDITION]);
3387 } 3464 }
3388
3389 void test_perform_reresolution() {
3390 enableStrongMode();
3391 AnalysisTarget source = newSource(
3392 '/test.dart',
3393 '''
3394 const topLevel = 3;
3395 class C {
3396 String field = topLevel;
3397 }
3398 ''');
3399 computeResult(new LibrarySpecificUnit(source, source), VERIFY_ERRORS);
3400 // validate
3401 _fillErrorListener(VERIFY_ERRORS);
3402 errorListener.assertErrorsWithCodes(
3403 <ErrorCode>[StaticTypeWarningCode.INVALID_ASSIGNMENT]);
3404 }
3405 } 3465 }
3406 3466
3407 class _AbstractDartTaskTest extends AbstractContextTest { 3467 class _AbstractDartTaskTest extends AbstractContextTest {
3408 Source emptySource; 3468 Source emptySource;
3409 3469
3410 GatheringErrorListener errorListener = new GatheringErrorListener(); 3470 GatheringErrorListener errorListener = new GatheringErrorListener();
3411 3471
3412 void assertIsInvalid(AnalysisTarget target, ResultDescriptor descriptor) { 3472 void assertIsInvalid(AnalysisTarget target, ResultDescriptor descriptor) {
3413 CacheEntry entry = context.getCacheEntry(target); 3473 CacheEntry entry = context.getCacheEntry(target);
3414 expect(entry.isInvalid(descriptor), isTrue); 3474 expect(entry.isInvalid(descriptor), isTrue);
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
3531 } 3591 }
3532 3592
3533 List<Statement> getStatementsInTopLevelFunction( 3593 List<Statement> getStatementsInTopLevelFunction(
3534 CompilationUnit unit, String functionName) { 3594 CompilationUnit unit, String functionName) {
3535 FunctionDeclaration function = getTopLevelFunction(unit, functionName); 3595 FunctionDeclaration function = getTopLevelFunction(unit, functionName);
3536 BlockFunctionBody body = function.functionExpression.body; 3596 BlockFunctionBody body = function.functionExpression.body;
3537 return body.block.statements; 3597 return body.block.statements;
3538 } 3598 }
3539 3599
3540 /** 3600 /**
3601 * Return the declaration of the top-level function with the given
3602 * [functionName] in the given compilation [unit].
3603 */
3604 FunctionDeclaration getTopLevelFunction(
3605 CompilationUnit unit, String functionName) {
3606 NodeList<CompilationUnitMember> unitMembers = unit.declarations;
3607 for (CompilationUnitMember unitMember in unitMembers) {
3608 if (unitMember is FunctionDeclaration) {
3609 if (unitMember.name.name == functionName) {
3610 return unitMember;
3611 }
3612 }
3613 }
3614 return null;
3615 }
3616
3617 /**
3541 * Return the declaration of the top-level variable with the given 3618 * Return the declaration of the top-level variable with the given
3542 * [variableName] in the given compilation [unit]. 3619 * [variableName] in the given compilation [unit].
3543 */ 3620 */
3544 VariableDeclaration getTopLevelVariable( 3621 VariableDeclaration getTopLevelVariable(
3545 CompilationUnit unit, String variableName) { 3622 CompilationUnit unit, String variableName) {
3546 NodeList<CompilationUnitMember> unitMembers = unit.declarations; 3623 NodeList<CompilationUnitMember> unitMembers = unit.declarations;
3547 for (CompilationUnitMember unitMember in unitMembers) { 3624 for (CompilationUnitMember unitMember in unitMembers) {
3548 if (unitMember is TopLevelVariableDeclaration) { 3625 if (unitMember is TopLevelVariableDeclaration) {
3549 NodeList<VariableDeclaration> variables = 3626 NodeList<VariableDeclaration> variables =
3550 unitMember.variables.variables; 3627 unitMember.variables.variables;
3551 for (VariableDeclaration variable in variables) { 3628 for (VariableDeclaration variable in variables) {
3552 if (variable.name.name == variableName) { 3629 if (variable.name.name == variableName) {
3553 return variable; 3630 return variable;
3554 } 3631 }
3555 } 3632 }
3556 } 3633 }
3557 } 3634 }
3558 return null; 3635 return null;
3559 } 3636 }
3560 3637
3561 /**
3562 * Return the declaration of the top-level function with the given
3563 * [functionName] in the given compilation [unit].
3564 */
3565 FunctionDeclaration getTopLevelFunction(
3566 CompilationUnit unit, String functionName) {
3567 NodeList<CompilationUnitMember> unitMembers = unit.declarations;
3568 for (CompilationUnitMember unitMember in unitMembers) {
3569 if (unitMember is FunctionDeclaration) {
3570 if (unitMember.name.name == functionName) {
3571 return unitMember;
3572 }
3573 }
3574 }
3575 return null;
3576 }
3577
3578 void setUp() { 3638 void setUp() {
3579 super.setUp(); 3639 super.setUp();
3580 emptySource = newSource('/test.dart'); 3640 emptySource = newSource('/test.dart');
3581 } 3641 }
3582 3642
3583 /** 3643 /**
3584 * Fill [errorListener] with [result] errors in the current [task]. 3644 * Fill [errorListener] with [result] errors in the current [task].
3585 */ 3645 */
3586 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) { 3646 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) {
3587 List<AnalysisError> errors = task.outputs[result]; 3647 List<AnalysisError> errors = task.outputs[result];
3588 expect(errors, isNotNull, reason: result.name); 3648 expect(errors, isNotNull, reason: result.name);
3589 errorListener = new GatheringErrorListener(); 3649 errorListener = new GatheringErrorListener();
3590 errorListener.addAll(errors); 3650 errorListener.addAll(errors);
3591 } 3651 }
3592 } 3652 }
OLDNEW
« pkg/analyzer/lib/src/task/dart.dart ('K') | « pkg/analyzer/lib/src/task/dart_work_manager.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698