Skip to content

Commit a8ecc7a

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[analyzer] Add tests for null-aware elements and color codes
Part of #56989 Change-Id: I90895dde478eb85ce8cc653c32fe32fa45b49dc8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/397904 Commit-Queue: Chloe Stefantsova <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]>
1 parent 4da6cde commit a8ecc7a

File tree

1 file changed

+122
-24
lines changed

1 file changed

+122
-24
lines changed

pkg/analysis_server/test/src/computer/color_computer_test.dart

Lines changed: 122 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ class ColorComputerTest extends AbstractContextTest {
9090
// in non-const contexts.
9191
'const CupertinoDynamicColor.withBrightness(color: CupertinoColors.white, darkColor: CupertinoColors.black)':
9292
{
93-
null: 0xFFFFFFFF,
94-
'CupertinoColors.white': 0xFFFFFFFF,
95-
'CupertinoColors.black': 0xFF000000,
96-
},
93+
null: 0xFFFFFFFF,
94+
'CupertinoColors.white': 0xFFFFFFFF,
95+
'CupertinoColors.black': 0xFF000000,
96+
},
9797
};
9898

9999
late String testPath;
@@ -156,8 +156,7 @@ class ColorComputerTest extends AbstractContextTest {
156156
expect(
157157
colors,
158158
hasLength(expectedColorValues.length),
159-
reason:
160-
'${expectedColorValues.length} colors should be detected in:\n'
159+
reason: '${expectedColorValues.length} colors should be detected in:\n'
161160
'$dartCode',
162161
);
163162

@@ -181,10 +180,10 @@ class ColorComputerTest extends AbstractContextTest {
181180
);
182181

183182
void expectComponent(int actual, int expected, String name) => expect(
184-
actual,
185-
expected,
186-
reason: '$name value for $expectedColorCode is not correct',
187-
);
183+
actual,
184+
expected,
185+
reason: '$name value for $expectedColorCode is not correct',
186+
);
188187

189188
expectComponent(color.color.alpha, expectedAlpha, 'Alpha');
190189
expectComponent(color.color.red, expectedRed, 'Red');
@@ -195,10 +194,9 @@ class ColorComputerTest extends AbstractContextTest {
195194

196195
void expectNoErrors(ResolvedUnitResult result) {
197196
// If the test code has errors, generate a suitable failure to help debug.
198-
var errors =
199-
result.errors
200-
.where((error) => error.severity == Severity.error)
201-
.toList();
197+
var errors = result.errors
198+
.where((error) => error.severity == Severity.error)
199+
.toList();
202200
if (errors.isNotEmpty) {
203201
throw 'Code has errors: $errors\n\n${result.content}';
204202
}
@@ -260,12 +258,15 @@ class MyTheme {
260258
instanceMaterialRedAccent = Colors.redAccent;
261259
}
262260
''';
263-
await expectColors(testCode, {
264-
'MyTheme.staticWhite': 0xFFFFFFFF,
265-
'MyTheme.staticMaterialRedAccent': 0xFFFFAA00,
266-
'theme.instanceWhite': 0xFFFFFFFF,
267-
'theme.instanceMaterialRedAccent': 0xFFFFAA00,
268-
}, otherCode: otherCode);
261+
await expectColors(
262+
testCode,
263+
{
264+
'MyTheme.staticWhite': 0xFFFFFFFF,
265+
'MyTheme.staticMaterialRedAccent': 0xFFFFAA00,
266+
'theme.instanceWhite': 0xFFFFFFFF,
267+
'theme.instanceMaterialRedAccent': 0xFFFFAA00,
268+
},
269+
otherCode: otherCode);
269270
}
270271

271272
Future<void> test_customConst_initializer() async {
@@ -282,10 +283,13 @@ void f() {
282283
const myThemeWhite = Colors.white;
283284
const myThemeMaterialRedAccent = Colors.redAccent;
284285
''';
285-
await expectColors(testCode, {
286-
'myThemeWhite': 0xFFFFFFFF,
287-
'myThemeMaterialRedAccent': 0xFFFFAA00,
288-
}, otherCode: otherCode);
286+
await expectColors(
287+
testCode,
288+
{
289+
'myThemeWhite': 0xFFFFFFFF,
290+
'myThemeMaterialRedAccent': 0xFFFFAA00,
291+
},
292+
otherCode: otherCode);
289293
}
290294

291295
Future<void> test_local_const() async {
@@ -352,6 +356,100 @@ void f() {
352356
await checkAllColors(testCode);
353357
}
354358

359+
Future<void> test_nullAwareElement_inList_const() async {
360+
const testCode = '''
361+
void f() {
362+
const colors = [
363+
?[[COLOR]],
364+
];
365+
}
366+
''';
367+
await checkAllColors(testCode, onlyConst: true);
368+
}
369+
370+
Future<void> test_nullAwareElement_inList_nonConst() async {
371+
const testCode = '''
372+
void f() {
373+
final colors = [
374+
?[[COLOR]],
375+
];
376+
}
377+
''';
378+
await checkAllColors(testCode);
379+
}
380+
381+
Future<void> test_nullAwareElement_inSet_const() async {
382+
// In the following test case the 'Color' object is placed inside of a list
383+
// literal, since the 'Color' class defines 'operator==', and its objects
384+
// can't be elements of constant sets.
385+
const testCode = '''
386+
void f() {
387+
const colors = {
388+
?[ [[COLOR]] ],
389+
};
390+
}
391+
''';
392+
await checkAllColors(testCode, onlyConst: true);
393+
}
394+
395+
Future<void> test_nullAwareElement_inSet_nonConst() async {
396+
const testCode = '''
397+
void f() {
398+
final colors = {
399+
?[[COLOR]],
400+
};
401+
}
402+
''';
403+
await checkAllColors(testCode);
404+
}
405+
406+
Future<void> test_nullAwareKey_inMap_const() async {
407+
// In the following test case the 'Color' object is placed inside of a list
408+
// literal, since the 'Color' class defines 'operator==', and its objects
409+
// can't be keys of constant maps.
410+
const testCode = '''
411+
void f() {
412+
const colors = {
413+
?[ [[COLOR]] ]: "value",
414+
};
415+
}
416+
''';
417+
await checkAllColors(testCode, onlyConst: true);
418+
}
419+
420+
Future<void> test_nullAwareKey_inMap_nonConst() async {
421+
const testCode = '''
422+
void f() {
423+
final colors = {
424+
?[[COLOR]]: "value",
425+
};
426+
}
427+
''';
428+
await checkAllColors(testCode);
429+
}
430+
431+
Future<void> test_nullAwareValue_inMap_const() async {
432+
const testCode = '''
433+
void f() {
434+
const colors = {
435+
"key": ?[[COLOR]],
436+
};
437+
}
438+
''';
439+
await checkAllColors(testCode, onlyConst: true);
440+
}
441+
442+
Future<void> test_nullAwareValue_inMap_nonConst() async {
443+
const testCode = '''
444+
void f() {
445+
final colors = {
446+
"key": ?[[COLOR]],
447+
};
448+
}
449+
''';
450+
await checkAllColors(testCode);
451+
}
452+
355453
Future<void> test_topLevel_const() async {
356454
const testCode = '''
357455
const a = [[COLOR]];

0 commit comments

Comments
 (0)