OLD | NEW |
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.strong_mode_test; | 5 library test.src.task.strong_mode_test; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
9 import 'package:analyzer/src/generated/source.dart'; | 9 import 'package:analyzer/src/generated/source.dart'; |
| 10 import 'package:analyzer/src/generated/testing/element_factory.dart'; |
| 11 import 'package:analyzer/src/task/dart.dart'; |
10 import 'package:analyzer/src/task/strong_mode.dart'; | 12 import 'package:analyzer/src/task/strong_mode.dart'; |
11 import 'package:analyzer/task/dart.dart'; | 13 import 'package:analyzer/task/dart.dart'; |
12 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
13 | 15 |
14 import '../../reflective_tests.dart'; | 16 import '../../reflective_tests.dart'; |
15 import '../../utils.dart'; | 17 import '../../utils.dart'; |
16 import '../context/abstract_context.dart'; | 18 import '../context/abstract_context.dart'; |
17 | 19 |
18 main() { | 20 main() { |
19 initializeTestEnvironment(); | 21 initializeTestEnvironment(); |
20 runReflectiveTests(InferrenceFinderTest); | 22 runReflectiveTests(InferrenceFinderTest); |
21 runReflectiveTests(InstanceMemberInferrerTest); | 23 runReflectiveTests(InstanceMemberInferrerTest); |
| 24 runReflectiveTests(VariableGathererTest); |
22 } | 25 } |
23 | 26 |
24 @reflectiveTest | 27 @reflectiveTest |
25 class InferrenceFinderTest extends AbstractContextTest { | 28 class InferrenceFinderTest extends AbstractContextTest { |
26 void test_creation() { | 29 void test_creation() { |
27 InferrenceFinder finder = new InferrenceFinder(); | 30 InferrenceFinder finder = new InferrenceFinder(); |
28 expect(finder, isNotNull); | 31 expect(finder, isNotNull); |
29 expect(finder.classes, isEmpty); | 32 expect(finder.classes, isEmpty); |
30 expect(finder.staticVariables, isEmpty); | 33 expect(finder.staticVariables, isEmpty); |
31 } | 34 } |
32 | 35 |
33 void test_visit() { | 36 void test_visit() { |
34 Source source = addSource( | 37 Source source = addSource( |
35 '/test.dart', | 38 '/test.dart', |
36 r''' | 39 r''' |
37 const c = 1; | 40 const c = 1; |
38 final f = ''; | 41 final f = ''; |
39 var v = const A(); | 42 var v = const A(); |
| 43 int i; |
40 class A { | 44 class A { |
41 static final fa = 0; | 45 static final fa = 0; |
| 46 static int fi; |
42 const A(); | 47 const A(); |
43 } | 48 } |
44 class B extends A { | 49 class B extends A { |
45 static const cb = 1; | 50 static const cb = 1; |
46 static vb = 0; | 51 static vb = 0; |
47 const ci = 2; | 52 const ci = 2; |
48 final fi = ''; | 53 final fi = ''; |
49 var vi; | 54 var vi; |
50 } | 55 } |
51 class C = Object with A; | 56 class C = Object with A; |
52 typedef int F(int x); | 57 typedef int F(int x); |
53 '''); | 58 '''); |
54 computeResult(source, PARSED_UNIT); | 59 LibrarySpecificUnit librarySpecificUnit = |
55 CompilationUnit unit = outputs[PARSED_UNIT]; | 60 new LibrarySpecificUnit(source, source); |
| 61 computeResult(librarySpecificUnit, RESOLVED_UNIT5); |
| 62 CompilationUnit unit = outputs[RESOLVED_UNIT5]; |
56 InferrenceFinder finder = new InferrenceFinder(); | 63 InferrenceFinder finder = new InferrenceFinder(); |
57 unit.accept(finder); | 64 unit.accept(finder); |
58 expect(finder.classes, hasLength(3)); | 65 expect(finder.classes, hasLength(3)); |
59 expect(finder.staticVariables, hasLength(6)); | 66 expect(finder.staticVariables, hasLength(6)); |
60 } | 67 } |
61 } | 68 } |
62 | 69 |
63 @reflectiveTest | 70 @reflectiveTest |
64 class InstanceMemberInferrerTest extends AbstractContextTest { | 71 class InstanceMemberInferrerTest extends AbstractContextTest { |
65 InstanceMemberInferrer get createInferrer => | 72 InstanceMemberInferrer get createInferrer => |
(...skipping 804 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
870 '''); | 877 '''); |
871 ClassElement classB = unit.getType('B'); | 878 ClassElement classB = unit.getType('B'); |
872 MethodElement methodB = classB.getMethod(methodName); | 879 MethodElement methodB = classB.getMethod(methodName); |
873 expect(methodB.returnType.isDynamic, isTrue); | 880 expect(methodB.returnType.isDynamic, isTrue); |
874 | 881 |
875 inferrer.inferCompilationUnit(unit); | 882 inferrer.inferCompilationUnit(unit); |
876 | 883 |
877 expect(methodB.returnType, classB.typeParameters[0].type); | 884 expect(methodB.returnType, classB.typeParameters[0].type); |
878 } | 885 } |
879 } | 886 } |
| 887 |
| 888 @reflectiveTest |
| 889 class VariableGathererTest extends AbstractContextTest { |
| 890 void test_creation_withFilter() { |
| 891 VariableFilter filter = (variable) => true; |
| 892 VariableGatherer gatherer = new VariableGatherer(filter); |
| 893 expect(gatherer, isNotNull); |
| 894 expect(gatherer.filter, filter); |
| 895 } |
| 896 |
| 897 void test_creation_withoutFilter() { |
| 898 VariableGatherer gatherer = new VariableGatherer(); |
| 899 expect(gatherer, isNotNull); |
| 900 expect(gatherer.filter, isNull); |
| 901 } |
| 902 |
| 903 void test_visit_noReferences() { |
| 904 Source source = addSource( |
| 905 '/test.dart', |
| 906 ''' |
| 907 library lib; |
| 908 import 'dart:math'; |
| 909 int zero = 0; |
| 910 class C { |
| 911 void m() => null; |
| 912 } |
| 913 typedef void F(); |
| 914 '''); |
| 915 CompilationUnit unit = context.resolveCompilationUnit2(source, source); |
| 916 VariableGatherer gatherer = new VariableGatherer(); |
| 917 unit.accept(gatherer); |
| 918 expect(gatherer.results, hasLength(0)); |
| 919 } |
| 920 |
| 921 void test_visit_withFilter() { |
| 922 VariableFilter filter = (VariableElement variable) => variable.isStatic; |
| 923 expect(_gather(filter), hasLength(1)); |
| 924 } |
| 925 |
| 926 void test_visit_withoutFilter() { |
| 927 expect(_gather(), hasLength(4)); |
| 928 } |
| 929 |
| 930 Set<VariableElement> _gather([VariableFilter filter = null]) { |
| 931 Source source = addSource( |
| 932 '/test.dart', |
| 933 ''' |
| 934 const int zero = 0; |
| 935 class Counter { |
| 936 int value = zero; |
| 937 void inc() { |
| 938 value++; |
| 939 } |
| 940 void dec() { |
| 941 value = value - 1; |
| 942 } |
| 943 void fromZero(f(int index)) { |
| 944 for (int i = zero; i < value; i++) { |
| 945 f(i); |
| 946 } |
| 947 } |
| 948 } |
| 949 '''); |
| 950 CompilationUnit unit = context.resolveCompilationUnit2(source, source); |
| 951 VariableGatherer gatherer = new VariableGatherer(filter); |
| 952 unit.accept(gatherer); |
| 953 return gatherer.results; |
| 954 } |
| 955 } |
OLD | NEW |