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

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

Issue 1320923003: Implement a task to compute the dependencies between static variables (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library test.src.task.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();
22 runReflectiveTests(DeclarationFinderTest);
20 runReflectiveTests(InferrenceFinderTest); 23 runReflectiveTests(InferrenceFinderTest);
21 runReflectiveTests(InstanceMemberInferrerTest); 24 runReflectiveTests(InstanceMemberInferrerTest);
25 runReflectiveTests(VariableGathererTest);
22 } 26 }
23 27
24 @reflectiveTest 28 @reflectiveTest
29 class DeclarationFinderTest extends AbstractContextTest {
30 void test_creation() {
31 VariableElement variable = ElementFactory.topLevelVariableElement2('X');
32 DeclarationFinder finder = new DeclarationFinder(variable);
33 expect(finder, isNotNull);
34 expect(finder.targetElement, variable);
35 }
36
37 void test_find() {
38 String content = '''
39 class C {
40 int x;
41 }
42 ''';
43 Source source = addSource('/test.dart', content);
44 CompilationUnit unit = context.resolveCompilationUnit2(source, source);
45 NodeLocator locator = new NodeLocator(content.indexOf('x'));
46 SimpleIdentifier identifier = locator.searchWithin(unit);
47 VariableDeclaration result =
48 DeclarationFinder.find(unit, identifier.staticElement);
49 expect(result, identifier.parent);
50 }
51 }
52
53 @reflectiveTest
25 class InferrenceFinderTest extends AbstractContextTest { 54 class InferrenceFinderTest extends AbstractContextTest {
26 void test_creation() { 55 void test_creation() {
27 InferrenceFinder finder = new InferrenceFinder(); 56 InferrenceFinder finder = new InferrenceFinder();
28 expect(finder, isNotNull); 57 expect(finder, isNotNull);
29 expect(finder.classes, isEmpty); 58 expect(finder.classes, isEmpty);
30 expect(finder.staticVariables, isEmpty); 59 expect(finder.staticVariables, isEmpty);
31 } 60 }
32 61
33 void test_visit() { 62 void test_visit() {
34 Source source = addSource( 63 Source source = addSource(
35 '/test.dart', 64 '/test.dart',
36 r''' 65 r'''
37 const c = 1; 66 const c = 1;
38 final f = ''; 67 final f = '';
39 var v = const A(); 68 var v = const A();
69 int i;
40 class A { 70 class A {
41 static final fa = 0; 71 static final fa = 0;
72 static int fi;
42 const A(); 73 const A();
43 } 74 }
44 class B extends A { 75 class B extends A {
45 static const cb = 1; 76 static const cb = 1;
46 static vb = 0; 77 static vb = 0;
47 const ci = 2; 78 const ci = 2;
48 final fi = ''; 79 final fi = '';
49 var vi; 80 var vi;
50 } 81 }
51 class C = Object with A; 82 class C = Object with A;
52 typedef int F(int x); 83 typedef int F(int x);
53 '''); 84 ''');
54 computeResult(source, PARSED_UNIT); 85 LibrarySpecificUnit librarySpecificUnit =
55 CompilationUnit unit = outputs[PARSED_UNIT]; 86 new LibrarySpecificUnit(source, source);
87 computeResult(librarySpecificUnit, RESOLVED_UNIT5);
88 CompilationUnit unit = outputs[RESOLVED_UNIT5];
56 InferrenceFinder finder = new InferrenceFinder(); 89 InferrenceFinder finder = new InferrenceFinder();
57 unit.accept(finder); 90 unit.accept(finder);
58 expect(finder.classes, hasLength(3)); 91 expect(finder.classes, hasLength(3));
59 expect(finder.staticVariables, hasLength(6)); 92 expect(finder.staticVariables, hasLength(6));
60 } 93 }
61 } 94 }
62 95
63 @reflectiveTest 96 @reflectiveTest
64 class InstanceMemberInferrerTest extends AbstractContextTest { 97 class InstanceMemberInferrerTest extends AbstractContextTest {
65 InstanceMemberInferrer get createInferrer => 98 InstanceMemberInferrer get createInferrer =>
(...skipping 804 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 '''); 903 ''');
871 ClassElement classB = unit.getType('B'); 904 ClassElement classB = unit.getType('B');
872 MethodElement methodB = classB.getMethod(methodName); 905 MethodElement methodB = classB.getMethod(methodName);
873 expect(methodB.returnType.isDynamic, isTrue); 906 expect(methodB.returnType.isDynamic, isTrue);
874 907
875 inferrer.inferCompilationUnit(unit); 908 inferrer.inferCompilationUnit(unit);
876 909
877 expect(methodB.returnType, classB.typeParameters[0].type); 910 expect(methodB.returnType, classB.typeParameters[0].type);
878 } 911 }
879 } 912 }
913
914 @reflectiveTest
915 class VariableGathererTest extends AbstractContextTest {
916 void test_creation_withFilter() {
917 VariableFilter filter = (variable) => true;
918 VariableGatherer gatherer = new VariableGatherer(filter);
919 expect(gatherer, isNotNull);
920 expect(gatherer.filter, filter);
921 }
922
923 void test_creation_withoutFilter() {
924 VariableGatherer gatherer = new VariableGatherer();
925 expect(gatherer, isNotNull);
926 expect(gatherer.filter, isNull);
927 }
928
929 void test_visit_noReferences() {
930 Source source = addSource(
931 '/test.dart',
932 '''
933 library lib;
934 import 'dart:math';
935 int zero = 0;
936 class C {
937 void m() => null;
938 }
939 typedef void F();
940 ''');
941 CompilationUnit unit = context.resolveCompilationUnit2(source, source);
942 VariableGatherer gatherer = new VariableGatherer();
943 unit.accept(gatherer);
944 expect(gatherer.results, hasLength(0));
945 }
946
947 void test_visit_withFilter() {
948 VariableFilter filter = (VariableElement variable) => variable.isStatic;
949 expect(_gather(filter), hasLength(1));
950 }
951
952 void test_visit_withoutFilter() {
953 expect(_gather(), hasLength(4));
954 }
955
956 Set<VariableElement> _gather([VariableFilter filter = null]) {
957 Source source = addSource(
958 '/test.dart',
959 '''
960 const int zero = 0;
961 class Counter {
962 int value = zero;
963 void inc() {
964 value++;
965 }
966 void dec() {
967 value = value - 1;
968 }
969 void fromZero(f(int index)) {
970 for (int i = zero; i < value; i++) {
971 f(i);
972 }
973 }
974 }
975 ''');
976 CompilationUnit unit = context.resolveCompilationUnit2(source, source);
977 VariableGatherer gatherer = new VariableGatherer(filter);
978 unit.accept(gatherer);
979 return gatherer.results;
980 }
981 }
OLDNEW
« pkg/analyzer/test/src/task/dart_test.dart ('K') | « pkg/analyzer/test/src/task/dart_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698