| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer.test.constant_test; | 5 library analyzer.test.constant_test; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/dart/element/type.dart'; | 9 import 'package:analyzer/dart/element/type.dart'; |
| 10 import 'package:analyzer/src/dart/element/element.dart'; | 10 import 'package:analyzer/src/dart/element/element.dart'; |
| 11 import 'package:analyzer/src/generated/constant.dart'; | 11 import 'package:analyzer/src/generated/constant.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 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/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/generated/source_io.dart'; | 17 import 'package:analyzer/src/generated/source_io.dart'; |
| 18 import 'package:analyzer/src/generated/testing/ast_factory.dart'; | 18 import 'package:analyzer/src/generated/testing/ast_factory.dart'; |
| 19 import 'package:analyzer/src/generated/testing/element_factory.dart'; | 19 import 'package:analyzer/src/generated/testing/element_factory.dart'; |
| 20 import 'package:analyzer/src/generated/testing/test_type_provider.dart'; | 20 import 'package:analyzer/src/generated/testing/test_type_provider.dart'; |
| 21 import 'package:analyzer/src/generated/utilities_collection.dart'; |
| 21 import 'package:analyzer/src/task/dart.dart'; | 22 import 'package:analyzer/src/task/dart.dart'; |
| 22 import 'package:path/path.dart'; | 23 import 'package:path/path.dart'; |
| 23 import 'package:unittest/unittest.dart'; | 24 import 'package:unittest/unittest.dart'; |
| 24 | 25 |
| 25 import '../reflective_tests.dart'; | 26 import '../reflective_tests.dart'; |
| 26 import '../utils.dart'; | 27 import '../utils.dart'; |
| 27 import 'engine_test.dart'; | 28 import 'engine_test.dart'; |
| 28 import 'resolver_test.dart'; | 29 import 'resolver_test.dart'; |
| 29 import 'test_support.dart'; | 30 import 'test_support.dart'; |
| 30 | 31 |
| 31 main() { | 32 main() { |
| 32 initializeTestEnvironment(); | 33 initializeTestEnvironment(); |
| 33 runReflectiveTests(ConstantEvaluatorTest); | 34 runReflectiveTests(ConstantEvaluatorTest); |
| 34 runReflectiveTests(ConstantFinderTest); | 35 runReflectiveTests(ConstantFinderTest); |
| 35 runReflectiveTests(ConstantValueComputerTest); | 36 runReflectiveTests(ConstantValueComputerTest); |
| 36 runReflectiveTests(ConstantVisitorTest); | 37 runReflectiveTests(ConstantVisitorTest); |
| 38 runReflectiveTests(ReferenceFinderTest); |
| 37 } | 39 } |
| 38 | 40 |
| 39 /** | 41 /** |
| 40 * Implementation of [ConstantEvaluationValidator] used during unit tests; | 42 * Implementation of [ConstantEvaluationValidator] used during unit tests; |
| 41 * verifies that any nodes referenced during constant evaluation are present in | 43 * verifies that any nodes referenced during constant evaluation are present in |
| 42 * the dependency graph. | 44 * the dependency graph. |
| 43 */ | 45 */ |
| 44 class ConstantEvaluationValidator_ForTest | 46 class ConstantEvaluationValidator_ForTest |
| 45 implements ConstantEvaluationValidator { | 47 implements ConstantEvaluationValidator { |
| 46 final InternalAnalysisContext context; | 48 final InternalAnalysisContext context; |
| (...skipping 2141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2188 DartObjectImpl result = expression.accept(new ConstantVisitor( | 2190 DartObjectImpl result = expression.accept(new ConstantVisitor( |
| 2189 new ConstantEvaluationEngine(typeProvider, new DeclaredVariables(), | 2191 new ConstantEvaluationEngine(typeProvider, new DeclaredVariables(), |
| 2190 typeSystem: typeSystem), | 2192 typeSystem: typeSystem), |
| 2191 errorReporter, | 2193 errorReporter, |
| 2192 lexicalEnvironment: lexicalEnvironment)); | 2194 lexicalEnvironment: lexicalEnvironment)); |
| 2193 errorListener.assertNoErrors(); | 2195 errorListener.assertNoErrors(); |
| 2194 return result; | 2196 return result; |
| 2195 } | 2197 } |
| 2196 } | 2198 } |
| 2197 | 2199 |
| 2200 @reflectiveTest |
| 2201 class ReferenceFinderTest { |
| 2202 DirectedGraph<ConstantEvaluationTarget> _referenceGraph; |
| 2203 VariableElement _head; |
| 2204 Element _tail; |
| 2205 |
| 2206 void setUp() { |
| 2207 _referenceGraph = new DirectedGraph<ConstantEvaluationTarget>(); |
| 2208 _head = ElementFactory.topLevelVariableElement2("v1"); |
| 2209 } |
| 2210 |
| 2211 void test_visitSimpleIdentifier_const() { |
| 2212 _visitNode(_makeTailVariable("v2", true)); |
| 2213 _assertOneArc(_tail); |
| 2214 } |
| 2215 |
| 2216 void test_visitSimpleIdentifier_nonConst() { |
| 2217 _visitNode(_makeTailVariable("v2", false)); |
| 2218 _assertOneArc(_tail); |
| 2219 } |
| 2220 |
| 2221 void test_visitSuperConstructorInvocation_const() { |
| 2222 _visitNode(_makeTailSuperConstructorInvocation("A", true)); |
| 2223 _assertOneArc(_tail); |
| 2224 } |
| 2225 |
| 2226 void test_visitSuperConstructorInvocation_nonConst() { |
| 2227 _visitNode(_makeTailSuperConstructorInvocation("A", false)); |
| 2228 _assertOneArc(_tail); |
| 2229 } |
| 2230 |
| 2231 void test_visitSuperConstructorInvocation_unresolved() { |
| 2232 SuperConstructorInvocation superConstructorInvocation = |
| 2233 AstFactory.superConstructorInvocation(); |
| 2234 _visitNode(superConstructorInvocation); |
| 2235 _assertNoArcs(); |
| 2236 } |
| 2237 |
| 2238 void _assertNoArcs() { |
| 2239 Set<ConstantEvaluationTarget> tails = _referenceGraph.getTails(_head); |
| 2240 expect(tails, hasLength(0)); |
| 2241 } |
| 2242 |
| 2243 void _assertOneArc(Element tail) { |
| 2244 Set<ConstantEvaluationTarget> tails = _referenceGraph.getTails(_head); |
| 2245 expect(tails, hasLength(1)); |
| 2246 expect(tails.first, same(tail)); |
| 2247 } |
| 2248 |
| 2249 ReferenceFinder _createReferenceFinder(ConstantEvaluationTarget source) => |
| 2250 new ReferenceFinder((ConstantEvaluationTarget dependency) { |
| 2251 _referenceGraph.addEdge(source, dependency); |
| 2252 }); |
| 2253 SuperConstructorInvocation _makeTailSuperConstructorInvocation( |
| 2254 String name, bool isConst) { |
| 2255 List<ConstructorInitializer> initializers = |
| 2256 new List<ConstructorInitializer>(); |
| 2257 ConstructorDeclaration constructorDeclaration = |
| 2258 AstFactory.constructorDeclaration(AstFactory.identifier3(name), null, |
| 2259 AstFactory.formalParameterList(), initializers); |
| 2260 if (isConst) { |
| 2261 constructorDeclaration.constKeyword = new KeywordToken(Keyword.CONST, 0); |
| 2262 } |
| 2263 ClassElementImpl classElement = ElementFactory.classElement2(name); |
| 2264 SuperConstructorInvocation superConstructorInvocation = |
| 2265 AstFactory.superConstructorInvocation(); |
| 2266 ConstructorElementImpl constructorElement = |
| 2267 ElementFactory.constructorElement(classElement, name, isConst); |
| 2268 _tail = constructorElement; |
| 2269 superConstructorInvocation.staticElement = constructorElement; |
| 2270 return superConstructorInvocation; |
| 2271 } |
| 2272 |
| 2273 SimpleIdentifier _makeTailVariable(String name, bool isConst) { |
| 2274 VariableDeclaration variableDeclaration = |
| 2275 AstFactory.variableDeclaration(name); |
| 2276 ConstLocalVariableElementImpl variableElement = |
| 2277 ElementFactory.constLocalVariableElement(name); |
| 2278 _tail = variableElement; |
| 2279 variableElement.const3 = isConst; |
| 2280 AstFactory.variableDeclarationList2( |
| 2281 isConst ? Keyword.CONST : Keyword.VAR, [variableDeclaration]); |
| 2282 SimpleIdentifier identifier = AstFactory.identifier3(name); |
| 2283 identifier.staticElement = variableElement; |
| 2284 return identifier; |
| 2285 } |
| 2286 |
| 2287 void _visitNode(AstNode node) { |
| 2288 node.accept(_createReferenceFinder(_head)); |
| 2289 } |
| 2290 } |
| 2291 |
| 2198 class _TestAnalysisContext extends TestAnalysisContext { | 2292 class _TestAnalysisContext extends TestAnalysisContext { |
| 2199 @override | 2293 @override |
| 2200 InternalAnalysisContext getContextFor(Source source) => this; | 2294 InternalAnalysisContext getContextFor(Source source) => this; |
| 2201 } | 2295 } |
| OLD | NEW |