Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 2a8f37b

Browse files
authored
use_named_constants constant pattern tests (#4211)
1 parent 40dd80c commit 2a8f37b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

test/rules/all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ import 'use_is_even_rather_than_modulo_test.dart'
127127
as use_is_even_rather_than_modulo;
128128
import 'use_late_for_private_fields_and_variables_test.dart'
129129
as use_late_for_private_fields_and_variables;
130+
import 'use_named_constants_test.dart' as use_named_constants;
130131
import 'use_super_parameters_test.dart' as use_super_parameters;
131132
import 'void_checks_test.dart' as void_checks;
132133

@@ -222,6 +223,7 @@ void main() {
222223
use_enums.main();
223224
use_is_even_rather_than_modulo.main();
224225
use_late_for_private_fields_and_variables.main();
226+
use_named_constants.main();
225227
use_super_parameters.main();
226228
void_checks.main();
227229
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
import 'package:test_reflective_loader/test_reflective_loader.dart';
6+
7+
import '../rule_test_support.dart';
8+
9+
main() {
10+
defineReflectiveSuite(() {
11+
defineReflectiveTests(UseNamedConstantsTestLanguage300);
12+
});
13+
}
14+
15+
@reflectiveTest
16+
class UseNamedConstantsTestLanguage300 extends LintRuleTest
17+
with LanguageVersion300Mixin {
18+
@override
19+
String get lintRule => 'use_named_constants';
20+
21+
/// https://github.com/dart-lang/linter/issues/4201
22+
test_constantPattern_ifCase() async {
23+
await assertDiagnostics(r'''
24+
class A {
25+
const A(this.value);
26+
final int value;
27+
28+
static const zero = A(0);
29+
}
30+
31+
void f(A a) {
32+
if (a case const A(0)) print(a);
33+
}
34+
''', [
35+
lint(117, 4),
36+
]);
37+
}
38+
39+
/// https://github.com/dart-lang/linter/issues/4201
40+
test_constantPattern_switch() async {
41+
await assertDiagnostics(r'''
42+
class A {
43+
const A(this.value);
44+
final int value;
45+
46+
static const zero = A(0);
47+
static const one = A(1);
48+
}
49+
50+
void f(A a) {
51+
switch (a) {
52+
case const A(1):
53+
}
54+
}
55+
''', [
56+
lint(155, 4),
57+
]);
58+
}
59+
}

0 commit comments

Comments
 (0)