Skip to content

Commit a004960

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[analyzer] Add occurrences tests for null-aware elements
Part of #56989 Change-Id: Ic6e48f1579b60a59ddb17c08202232081cbd3bf2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411243 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent e8e56ff commit a004960

File tree

2 files changed

+161
-1
lines changed

2 files changed

+161
-1
lines changed

pkg/analysis_server/test/integration/analysis/occurrences_test.dart

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,164 @@ void f() {
6464
check('j', ['j = 0', 'j < i', 'j++', 'j;']);
6565
check('sum', ['sum = 0', 'sum +=', 'sum)']);
6666
}
67+
68+
Future<void> test_occurrences_inList_nullAware() async {
69+
var pathname = sourcePath('test.dart');
70+
var text = r'''
71+
int? g(int x) => x % 2 == 0 ? null : x;
72+
void f() {
73+
List<int> sum = [];
74+
for (int i = 0; i < 10; i++) {
75+
for (int j = 0; j < i; j++) {
76+
sum += [?g(i), ?g(j)];
77+
}
78+
}
79+
print(sum);
80+
}
81+
''';
82+
writeFile(pathname, text);
83+
await standardAnalysisSetup();
84+
await sendAnalysisSetSubscriptions({
85+
AnalysisService.OCCURRENCES: [pathname],
86+
});
87+
88+
await analysisFinished;
89+
expect(currentAnalysisErrors[pathname], isEmpty);
90+
91+
var params = await onAnalysisOccurrences.first;
92+
expect(params.file, equals(pathname));
93+
var occurrences = params.occurrences;
94+
95+
Set<int> findOffsets(String elementName) {
96+
for (var occurrence in occurrences) {
97+
if (occurrence.element.name == elementName) {
98+
return occurrence.offsets.toSet();
99+
}
100+
}
101+
fail('No element found matching $elementName');
102+
}
103+
104+
void check(String elementName, Iterable<String> expectedOccurrences) {
105+
var expectedOffsets =
106+
expectedOccurrences
107+
.map((String substring) => text.indexOf(substring))
108+
.toSet();
109+
var foundOffsets = findOffsets(elementName);
110+
expect(foundOffsets, equals(expectedOffsets));
111+
}
112+
113+
check('i', ['i = 0', 'i < 10', 'i++', 'i;', 'i),']);
114+
check('j', ['j = 0', 'j < i', 'j++', 'j)];']);
115+
check('sum', ['sum = []', 'sum +=', 'sum)']);
116+
}
117+
118+
Future<void> test_occurrences_inMap_nullAware() async {
119+
var pathname = sourcePath('test.dart');
120+
var text = r'''
121+
int? g1(int x) => x % 2 == 0 ? null : x;
122+
int? g2(int x) => x % 3 == 0 ? null : x;
123+
int? g3(int x) => x % 5 == 0 ? null : x;
124+
void f() {
125+
Map<int, int> sum = {};
126+
for (int i = 0; i < 10; i++) {
127+
for (int j = 0; j < i; j++) {
128+
sum.addAll({?g1(i): ?g1(j)});
129+
sum.addAll({?g2(i): j});
130+
sum.addAll({i: ?g3(j,)});
131+
}
132+
}
133+
print(sum);
134+
}
135+
''';
136+
writeFile(pathname, text);
137+
await standardAnalysisSetup();
138+
await sendAnalysisSetSubscriptions({
139+
AnalysisService.OCCURRENCES: [pathname],
140+
});
141+
142+
await analysisFinished;
143+
expect(currentAnalysisErrors[pathname], isEmpty);
144+
145+
var params = await onAnalysisOccurrences.first;
146+
expect(params.file, equals(pathname));
147+
var occurrences = params.occurrences;
148+
149+
Set<int> findOffsets(String elementName) {
150+
for (var occurrence in occurrences) {
151+
if (occurrence.element.name == elementName) {
152+
return occurrence.offsets.toSet();
153+
}
154+
}
155+
fail('No element found matching $elementName');
156+
}
157+
158+
void check(String elementName, Iterable<String> expectedOccurrences) {
159+
var expectedOffsets =
160+
expectedOccurrences
161+
.map((String substring) => text.indexOf(substring))
162+
.toSet();
163+
var foundOffsets = findOffsets(elementName);
164+
expect(foundOffsets, equals(expectedOffsets));
165+
}
166+
167+
check('i', ['i = 0', 'i < 10', 'i++', 'i;', 'i): ?', 'i): j', 'i:']);
168+
check('j', ['j = 0', 'j < i', 'j++', 'j)});', 'j});', 'j,)});']);
169+
check('sum', [
170+
'sum = {}',
171+
'sum.addAll({?g1',
172+
'sum.addAll({?g2',
173+
'sum.addAll({i',
174+
'sum)',
175+
]);
176+
}
177+
178+
Future<void> test_occurrences_inSet_nullAware() async {
179+
var pathname = sourcePath('test.dart');
180+
var text = r'''
181+
int? g(int x) => x % 2 == 0 ? null : x;
182+
void f() {
183+
Set<int> sum = {};
184+
for (int i = 0; i < 10; i++) {
185+
for (int j = 0; j < i; j++) {
186+
sum.addAll({?g(i), ?g(j)});
187+
}
188+
}
189+
print(sum);
190+
}
191+
''';
192+
writeFile(pathname, text);
193+
await standardAnalysisSetup();
194+
await sendAnalysisSetSubscriptions({
195+
AnalysisService.OCCURRENCES: [pathname],
196+
});
197+
198+
await analysisFinished;
199+
expect(currentAnalysisErrors[pathname], isEmpty);
200+
201+
var params = await onAnalysisOccurrences.first;
202+
expect(params.file, equals(pathname));
203+
var occurrences = params.occurrences;
204+
205+
Set<int> findOffsets(String elementName) {
206+
for (var occurrence in occurrences) {
207+
if (occurrence.element.name == elementName) {
208+
return occurrence.offsets.toSet();
209+
}
210+
}
211+
fail('No element found matching $elementName');
212+
}
213+
214+
void check(String elementName, Iterable<String> expectedOccurrences) {
215+
var expectedOffsets =
216+
expectedOccurrences
217+
.map((String substring) => text.indexOf(substring))
218+
.toSet();
219+
var foundOffsets = findOffsets(elementName);
220+
expect(foundOffsets, equals(expectedOffsets));
221+
}
222+
223+
check('i', ['i = 0', 'i < 10', 'i++', 'i;', 'i),']);
224+
check('j', ['j = 0', 'j < i', 'j++', 'j)});']);
225+
check('sum', ['sum = {}', 'sum.addAll', 'sum)']);
226+
}
67227
}

pkg/analysis_server/test/integration/support/integration_tests.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ abstract class AbstractAnalysisServerIntegrationTest extends IntegrationTest
212212
writeTestPackageConfig();
213213

214214
writeTestPackageAnalysisOptionsFile(
215-
analysisOptionsContent(experiments: ['macros']),
215+
analysisOptionsContent(experiments: ['macros', 'null-aware-elements']),
216216
);
217217

218218
onAnalysisErrors.listen((AnalysisErrorsParams params) {

0 commit comments

Comments
 (0)