@@ -43,42 +43,50 @@ main() {
43
43
*/
44
44
class ConstantEvaluationValidator_ForTest
45
45
implements ConstantEvaluationValidator {
46
+ final InternalAnalysisContext context;
46
47
ConstantValueComputer computer;
47
-
48
48
ConstantEvaluationTarget _nodeBeingEvaluated;
49
49
50
+ ConstantEvaluationValidator_ForTest (this .context);
51
+
50
52
@override
51
53
void beforeComputeValue (ConstantEvaluationTarget constant) {
52
54
_nodeBeingEvaluated = constant;
53
55
}
54
56
55
57
@override
56
- void beforeGetConstantInitializers (ConstructorElement constructor) {
57
- // Make sure we properly recorded the dependency.
58
- expect (
59
- computer.referenceGraph.containsPath (_nodeBeingEvaluated, constructor),
60
- isTrue);
61
- }
58
+ void beforeGetConstantInitializers (ConstructorElement constructor) =>
59
+ _checkPathTo (constructor);
62
60
63
61
@override
64
- void beforeGetEvaluationResult (ConstantEvaluationTarget constant) {
65
- // Make sure we properly recorded the dependency.
66
- expect (computer.referenceGraph.containsPath (_nodeBeingEvaluated, constant),
67
- isTrue);
68
- }
62
+ void beforeGetEvaluationResult (ConstantEvaluationTarget constant) =>
63
+ _checkPathTo (constant);
69
64
70
65
@override
71
- void beforeGetFieldEvaluationResult (FieldElementImpl field) {
72
- // Make sure we properly recorded the dependency.
73
- expect (computer.referenceGraph.containsPath (_nodeBeingEvaluated, field),
74
- isTrue);
75
- }
66
+ void beforeGetFieldEvaluationResult (FieldElementImpl field) =>
67
+ _checkPathTo (field);
76
68
77
69
@override
78
- void beforeGetParameterDefault (ParameterElement parameter) {
79
- // Make sure we properly recorded the dependency.
80
- expect (computer.referenceGraph.containsPath (_nodeBeingEvaluated, parameter),
81
- isTrue);
70
+ void beforeGetParameterDefault (ParameterElement parameter) =>
71
+ _checkPathTo (parameter);
72
+
73
+ void _checkPathTo (ConstantEvaluationTarget target) {
74
+ if (computer.referenceGraph.containsPath (_nodeBeingEvaluated, target)) {
75
+ return ; // pass
76
+ }
77
+ // print a nice error message on failure
78
+ StringBuffer out = new StringBuffer ();
79
+ out.writeln ("missing path in constant dependency graph" );
80
+ out.writeln ("from $_nodeBeingEvaluated to $target " );
81
+ for (var s in context.analysisCache.sources) {
82
+ String text = context.getContents (s).data;
83
+ if (text != "" ) {
84
+ out.writeln ('''
85
+ === ${s .shortName }
86
+ $text ''' );
87
+ }
88
+ }
89
+ fail (out.toString ());
82
90
}
83
91
}
84
92
@@ -851,17 +859,17 @@ class C {
851
859
TestLogger logger = new TestLogger ();
852
860
AnalysisEngine .instance.logger = logger;
853
861
try {
854
- Source librarySource = addSource (r'''
862
+ Source source = addSource (r'''
855
863
const int a = c;
856
864
const int b = a;
857
865
const int c = b;''' );
858
- LibraryElement libraryElement = resolve2 (librarySource );
866
+ LibraryElement libraryElement = resolve2 (source );
859
867
CompilationUnit unit =
860
- analysisContext.resolveCompilationUnit (librarySource , libraryElement);
861
- analysisContext.computeErrors (librarySource );
868
+ analysisContext.resolveCompilationUnit (source , libraryElement);
869
+ analysisContext.computeErrors (source );
862
870
expect (unit, isNotNull);
863
871
ConstantValueComputer computer = _makeConstantValueComputer ();
864
- computer.add (unit, librarySource, librarySource );
872
+ computer.add (unit, source, source );
865
873
computer.computeValues ();
866
874
NodeList <CompilationUnitMember > members = unit.declarations;
867
875
expect (members, hasLength (3 ));
@@ -874,15 +882,15 @@ class C {
874
882
}
875
883
876
884
void test_computeValues_dependentVariables () {
877
- Source librarySource = addSource (r'''
885
+ Source source = addSource (r'''
878
886
const int b = a;
879
887
const int a = 0;''' );
880
- LibraryElement libraryElement = resolve2 (librarySource );
888
+ LibraryElement libraryElement = resolve2 (source );
881
889
CompilationUnit unit =
882
- analysisContext.resolveCompilationUnit (librarySource , libraryElement);
890
+ analysisContext.resolveCompilationUnit (source , libraryElement);
883
891
expect (unit, isNotNull);
884
892
ConstantValueComputer computer = _makeConstantValueComputer ();
885
- computer.add (unit, librarySource, librarySource );
893
+ computer.add (unit, source, source );
886
894
computer.computeValues ();
887
895
NodeList <CompilationUnitMember > members = unit.declarations;
888
896
expect (members, hasLength (2 ));
@@ -933,30 +941,30 @@ const int d = c;''');
933
941
}
934
942
935
943
void test_computeValues_singleVariable () {
936
- Source librarySource = addSource ("const int a = 0;" );
937
- LibraryElement libraryElement = resolve2 (librarySource );
944
+ Source source = addSource ("const int a = 0;" );
945
+ LibraryElement libraryElement = resolve2 (source );
938
946
CompilationUnit unit =
939
- analysisContext.resolveCompilationUnit (librarySource , libraryElement);
947
+ analysisContext.resolveCompilationUnit (source , libraryElement);
940
948
expect (unit, isNotNull);
941
949
ConstantValueComputer computer = _makeConstantValueComputer ();
942
- computer.add (unit, librarySource, librarySource );
950
+ computer.add (unit, source, source );
943
951
computer.computeValues ();
944
952
NodeList <CompilationUnitMember > members = unit.declarations;
945
953
expect (members, hasLength (1 ));
946
954
_validate (true , (members[0 ] as TopLevelVariableDeclaration ).variables);
947
955
}
948
956
949
957
void test_computeValues_value_depends_on_enum () {
950
- Source librarySource = addSource ('''
958
+ Source source = addSource ('''
951
959
enum E { id0, id1 }
952
960
const E e = E.id0;
953
961
''' );
954
- LibraryElement libraryElement = resolve2 (librarySource );
962
+ LibraryElement libraryElement = resolve2 (source );
955
963
CompilationUnit unit =
956
- analysisContext.resolveCompilationUnit (librarySource , libraryElement);
964
+ analysisContext.resolveCompilationUnit (source , libraryElement);
957
965
expect (unit, isNotNull);
958
966
ConstantValueComputer computer = _makeConstantValueComputer ();
959
- computer.add (unit, librarySource, librarySource );
967
+ computer.add (unit, source, source );
960
968
computer.computeValues ();
961
969
TopLevelVariableDeclaration declaration = unit.declarations
962
970
.firstWhere ((member) => member is TopLevelVariableDeclaration );
@@ -1963,7 +1971,7 @@ class A {
1963
1971
1964
1972
ConstantValueComputer _makeConstantValueComputer () {
1965
1973
ConstantEvaluationValidator_ForTest validator =
1966
- new ConstantEvaluationValidator_ForTest ();
1974
+ new ConstantEvaluationValidator_ForTest (analysisContext2 );
1967
1975
validator.computer = new ConstantValueComputer (
1968
1976
analysisContext2,
1969
1977
analysisContext2.typeProvider,
0 commit comments