5
5
import 'package:analyzer/dart/analysis/features.dart' ;
6
6
import 'package:analyzer/error/error.dart' ;
7
7
import 'package:analyzer/error/listener.dart' ;
8
- import 'package:analyzer/src/dart/analysis/experiments.dart' ;
9
8
import 'package:analyzer/src/dart/element/element.dart' ;
10
9
import 'package:analyzer/src/error/codes.dart' ;
11
10
import 'package:analyzer/src/generated/constant.dart' ;
@@ -18,14 +17,126 @@ import '../resolution/context_collection_resolution.dart';
18
17
main () {
19
18
defineReflectiveSuite (() {
20
19
defineReflectiveTests (ConstantVisitorTest );
21
- defineReflectiveTests (ConstantVisitorWithNullSafetyWithTripleShiftTest );
22
- defineReflectiveTests (ConstantVisitorWithNullSafetyTest );
20
+ defineReflectiveTests (ConstantVisitorWithoutNullSafetyTest );
23
21
});
24
22
}
25
23
26
24
@reflectiveTest
27
25
class ConstantVisitorTest extends ConstantVisitorTestSupport
28
- with ConstantVisitorTestCases {}
26
+ with ConstantVisitorTestCases {
27
+ test_visitAsExpression_potentialConstType () async {
28
+ await assertNoErrorsInCode ('''
29
+ const num three = 3;
30
+
31
+ class C<T extends num> {
32
+ final T w;
33
+ const C() : w = three as T;
34
+ }
35
+
36
+ void main() {
37
+ const C<int>().w;
38
+ }
39
+ ''' );
40
+ }
41
+
42
+ test_visitBinaryExpression_gtGtGt_negative_fewerBits () async {
43
+ await resolveTestCode ('''
44
+ const c = 0xFFFFFFFF >>> 8;
45
+ ''' );
46
+ DartObjectImpl result = _evaluateConstant ('c' );
47
+ expect (result.type, typeProvider.intType);
48
+ expect (result.toIntValue (), 0xFFFFFF );
49
+ }
50
+
51
+ test_visitBinaryExpression_gtGtGt_negative_moreBits () async {
52
+ await resolveTestCode ('''
53
+ const c = 0xFFFFFFFF >>> 33;
54
+ ''' );
55
+ DartObjectImpl result = _evaluateConstant ('c' );
56
+ expect (result.type, typeProvider.intType);
57
+ expect (result.toIntValue (), 0 );
58
+ }
59
+
60
+ test_visitBinaryExpression_gtGtGt_negative_moreThan64Bits () async {
61
+ await resolveTestCode ('''
62
+ const c = 0xFFFFFFFF >>> 65;
63
+ ''' );
64
+ DartObjectImpl result = _evaluateConstant ('c' );
65
+ expect (result.type, typeProvider.intType);
66
+ expect (result.toIntValue (), 0 );
67
+ }
68
+
69
+ test_visitBinaryExpression_gtGtGt_negative_negativeBits () async {
70
+ await resolveTestCode ('''
71
+ const c = 0xFFFFFFFF >>> -2;
72
+ ''' );
73
+ _evaluateConstantOrNull ('c' ,
74
+ errorCodes: [CompileTimeErrorCode .CONST_EVAL_THROWS_EXCEPTION ]);
75
+ }
76
+
77
+ test_visitBinaryExpression_gtGtGt_negative_zeroBits () async {
78
+ await resolveTestCode ('''
79
+ const c = 0xFFFFFFFF >>> 0;
80
+ ''' );
81
+ DartObjectImpl result = _evaluateConstant ('c' );
82
+ expect (result.type, typeProvider.intType);
83
+ expect (result.toIntValue (), 0xFFFFFFFF );
84
+ }
85
+
86
+ test_visitBinaryExpression_gtGtGt_positive_fewerBits () async {
87
+ await resolveTestCode ('''
88
+ const c = 0xFF >>> 3;
89
+ ''' );
90
+ DartObjectImpl result = _evaluateConstant ('c' );
91
+ expect (result.type, typeProvider.intType);
92
+ expect (result.toIntValue (), 0x1F );
93
+ }
94
+
95
+ test_visitBinaryExpression_gtGtGt_positive_moreBits () async {
96
+ await resolveTestCode ('''
97
+ const c = 0xFF >>> 9;
98
+ ''' );
99
+ DartObjectImpl result = _evaluateConstant ('c' );
100
+ expect (result.type, typeProvider.intType);
101
+ expect (result.toIntValue (), 0 );
102
+ }
103
+
104
+ test_visitBinaryExpression_gtGtGt_positive_moreThan64Bits () async {
105
+ await resolveTestCode ('''
106
+ const c = 0xFF >>> 65;
107
+ ''' );
108
+ DartObjectImpl result = _evaluateConstant ('c' );
109
+ expect (result.type, typeProvider.intType);
110
+ expect (result.toIntValue (), 0 );
111
+ }
112
+
113
+ test_visitBinaryExpression_gtGtGt_positive_negativeBits () async {
114
+ await resolveTestCode ('''
115
+ const c = 0xFF >>> -2;
116
+ ''' );
117
+ _evaluateConstantOrNull ('c' ,
118
+ errorCodes: [CompileTimeErrorCode .CONST_EVAL_THROWS_EXCEPTION ]);
119
+ }
120
+
121
+ test_visitBinaryExpression_gtGtGt_positive_zeroBits () async {
122
+ await resolveTestCode ('''
123
+ const c = 0xFF >>> 0;
124
+ ''' );
125
+ DartObjectImpl result = _evaluateConstant ('c' );
126
+ expect (result.type, typeProvider.intType);
127
+ expect (result.toIntValue (), 0xFF );
128
+ }
129
+
130
+ test_visitSimpleIdentifier_className () async {
131
+ await resolveTestCode ('''
132
+ const a = C;
133
+ class C {}
134
+ ''' );
135
+ DartObjectImpl result = _evaluateConstant ('a' );
136
+ expect (result.type, typeProvider.typeType);
137
+ assertType (result.toTypeValue (), 'C*' );
138
+ }
139
+ }
29
140
30
141
@reflectiveTest
31
142
mixin ConstantVisitorTestCases on ConstantVisitorTestSupport {
@@ -281,16 +392,6 @@ class B {
281
392
expect (result, isNull);
282
393
}
283
394
284
- test_visitAsExpression_null () async {
285
- await resolveTestCode ('''
286
- const a = null;
287
- const b = a as A;
288
- class A {}
289
- ''' );
290
- DartObjectImpl result = _evaluateConstant ('b' );
291
- expect (result.type, typeProvider.nullType);
292
- }
293
-
294
395
test_visitAsExpression_potentialConst () async {
295
396
await assertNoErrorsInCode ('''
296
397
class A {
@@ -939,16 +1040,6 @@ const b = B('');
939
1040
]);
940
1041
}
941
1042
942
- test_visitSimpleIdentifier_className () async {
943
- await resolveTestCode ('''
944
- const a = C;
945
- class C {}
946
- ''' );
947
- DartObjectImpl result = _evaluateConstant ('a' );
948
- expect (result.type, typeProvider.typeType);
949
- assertType (result.toTypeValue (), 'C' );
950
- }
951
-
952
1043
test_visitSimpleIdentifier_dynamic () async {
953
1044
await resolveTestCode ('''
954
1045
const a = dynamic;
@@ -1017,10 +1108,7 @@ const b = 3;''');
1017
1108
}
1018
1109
}
1019
1110
1020
- class ConstantVisitorTestSupport extends PubPackageResolutionTest
1021
- with WithoutNullSafetyMixin {
1022
- // TODO(https://github.com/dart-lang/sdk/issues/44666): Use null safety in
1023
- // test cases.
1111
+ class ConstantVisitorTestSupport extends PubPackageResolutionTest {
1024
1112
DartObjectImpl _evaluateConstant (
1025
1113
String name, {
1026
1114
List <ErrorCode >? errorCodes,
@@ -1076,130 +1164,15 @@ class ConstantVisitorTestSupport extends PubPackageResolutionTest
1076
1164
}
1077
1165
1078
1166
@reflectiveTest
1079
- class ConstantVisitorWithNullSafetyTest extends ConstantVisitorTestSupport
1080
- with WithNullSafetyMixin {
1081
- test_visitAsExpression_potentialConstType () async {
1082
- await assertNoErrorsInCode ('''
1083
- const num three = 3;
1084
-
1085
- class C<T extends num> {
1086
- final T w;
1087
- const C() : w = three as T;
1088
- }
1089
-
1090
- void main() {
1091
- const C<int>().w;
1092
- }
1093
- ''' );
1094
- }
1095
- }
1096
-
1097
- @reflectiveTest
1098
- class ConstantVisitorWithNullSafetyWithTripleShiftTest
1099
- extends ConstantVisitorTestSupport {
1100
- @override
1101
- String get testPackageLanguageVersion =>
1102
- '${ExperimentStatus .currentVersion .major }.'
1103
- '${ExperimentStatus .currentVersion .minor }' ;
1104
-
1105
- @override
1106
- void setUp () {
1107
- super .setUp ();
1108
-
1109
- writeTestPackageAnalysisOptionsFile (
1110
- AnalysisOptionsFileConfig (
1111
- experiments: [
1112
- EnableString .triple_shift,
1113
- ],
1114
- ),
1115
- );
1116
- }
1117
-
1118
- test_visitBinaryExpression_gtGtGt_negative_fewerBits () async {
1119
- await resolveTestCode ('''
1120
- const c = 0xFFFFFFFF >>> 8;
1121
- ''' );
1122
- DartObjectImpl result = _evaluateConstant ('c' );
1123
- expect (result.type, typeProvider.intType);
1124
- expect (result.toIntValue (), 0xFFFFFF );
1125
- }
1126
-
1127
- test_visitBinaryExpression_gtGtGt_negative_moreBits () async {
1128
- await resolveTestCode ('''
1129
- const c = 0xFFFFFFFF >>> 33;
1130
- ''' );
1131
- DartObjectImpl result = _evaluateConstant ('c' );
1132
- expect (result.type, typeProvider.intType);
1133
- expect (result.toIntValue (), 0 );
1134
- }
1135
-
1136
- test_visitBinaryExpression_gtGtGt_negative_moreThan64Bits () async {
1137
- await resolveTestCode ('''
1138
- const c = 0xFFFFFFFF >>> 65;
1139
- ''' );
1140
- DartObjectImpl result = _evaluateConstant ('c' );
1141
- expect (result.type, typeProvider.intType);
1142
- expect (result.toIntValue (), 0 );
1143
- }
1144
-
1145
- test_visitBinaryExpression_gtGtGt_negative_negativeBits () async {
1146
- await resolveTestCode ('''
1147
- const c = 0xFFFFFFFF >>> -2;
1148
- ''' );
1149
- _evaluateConstantOrNull ('c' ,
1150
- errorCodes: [CompileTimeErrorCode .CONST_EVAL_THROWS_EXCEPTION ]);
1151
- }
1152
-
1153
- test_visitBinaryExpression_gtGtGt_negative_zeroBits () async {
1154
- await resolveTestCode ('''
1155
- const c = 0xFFFFFFFF >>> 0;
1156
- ''' );
1157
- DartObjectImpl result = _evaluateConstant ('c' );
1158
- expect (result.type, typeProvider.intType);
1159
- expect (result.toIntValue (), 0xFFFFFFFF );
1160
- }
1161
-
1162
- test_visitBinaryExpression_gtGtGt_positive_fewerBits () async {
1163
- await resolveTestCode ('''
1164
- const c = 0xFF >>> 3;
1165
- ''' );
1166
- DartObjectImpl result = _evaluateConstant ('c' );
1167
- expect (result.type, typeProvider.intType);
1168
- expect (result.toIntValue (), 0x1F );
1169
- }
1170
-
1171
- test_visitBinaryExpression_gtGtGt_positive_moreBits () async {
1172
- await resolveTestCode ('''
1173
- const c = 0xFF >>> 9;
1174
- ''' );
1175
- DartObjectImpl result = _evaluateConstant ('c' );
1176
- expect (result.type, typeProvider.intType);
1177
- expect (result.toIntValue (), 0 );
1178
- }
1179
-
1180
- test_visitBinaryExpression_gtGtGt_positive_moreThan64Bits () async {
1181
- await resolveTestCode ('''
1182
- const c = 0xFF >>> 65;
1183
- ''' );
1184
- DartObjectImpl result = _evaluateConstant ('c' );
1185
- expect (result.type, typeProvider.intType);
1186
- expect (result.toIntValue (), 0 );
1187
- }
1188
-
1189
- test_visitBinaryExpression_gtGtGt_positive_negativeBits () async {
1190
- await resolveTestCode ('''
1191
- const c = 0xFF >>> -2;
1192
- ''' );
1193
- _evaluateConstantOrNull ('c' ,
1194
- errorCodes: [CompileTimeErrorCode .CONST_EVAL_THROWS_EXCEPTION ]);
1195
- }
1196
-
1197
- test_visitBinaryExpression_gtGtGt_positive_zeroBits () async {
1167
+ class ConstantVisitorWithoutNullSafetyTest extends ConstantVisitorTestSupport
1168
+ with ConstantVisitorTestCases , WithoutNullSafetyMixin {
1169
+ test_visitAsExpression_null () async {
1198
1170
await resolveTestCode ('''
1199
- const c = 0xFF >>> 0;
1171
+ const a = null;
1172
+ const b = a as A;
1173
+ class A {}
1200
1174
''' );
1201
- DartObjectImpl result = _evaluateConstant ('c' );
1202
- expect (result.type, typeProvider.intType);
1203
- expect (result.toIntValue (), 0xFF );
1175
+ DartObjectImpl result = _evaluateConstant ('b' );
1176
+ expect (result.type, typeProvider.nullType);
1204
1177
}
1205
1178
}
0 commit comments