Skip to content

Commit

Permalink
Move prefer_const_constructors tests (#4683)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored Aug 14, 2023
1 parent 3675167 commit c671de2
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 100 deletions.
234 changes: 234 additions & 0 deletions test/rules/prefer_const_constructors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,214 @@ class PreferConstConstructorsTest extends LintRuleTest {
@override
String get lintRule => 'prefer_const_constructors';

test_canBeConst_argumentIsAdjacentStrings() async {
await assertDiagnostics(r'''
class A {
const A(String s);
}
var a = A('adjacent' 'string');
''', [
lint(41, 22),
]);
}

test_canBeConst_argumentIsListLiteral() async {
await assertDiagnostics(r'''
class A {
const A(List<int> l);
}
var a = A([]);
''', [
lint(44, 5),
]);
}

test_canBeConst_argumentIsMap_nonLiteral() async {
await assertNoDiagnostics(r'''
class A {
const A(Map<int, int> m);
}
A f(Map<int, int> m) => A(m);
''');
}

test_canBeConst_argumentIsMapLiteral_containsNonLiteral() async {
await assertNoDiagnostics(r'''
class A {
const A(Map<int, int> m);
}
A f(int x) => A({x: x});
''');
}

test_canBeConst_argumentIsMapLiteral_instantiated() async {
await assertDiagnostics(r'''
class A {
const A(Map<int, int> m);
}
var a = A({});
''', [
lint(48, 5),
]);
}

test_canBeConst_explicitTypeArgument_dynamic() async {
await assertDiagnostics(r'''
class A<T> {
const A();
}
var a = A<dynamic>();
''', [
lint(36, 12),
]);
}

test_canBeConst_explicitTypeArgument_string() async {
await assertDiagnostics(r'''
class A<T> {
const A();
}
var a = A<String>();
''', [
lint(36, 11),
]);
}

test_canBeConst_intLiteralArgument() async {
await assertDiagnostics(r'''
class A {
const A(int x);
}
var a = A(5);
''', [
lint(38, 4),
]);
}

test_canBeConst_optionalNamedParameter() async {
await assertDiagnostics(r'''
class A {
const A({A? parent});
}
var a = A();
''', [
lint(44, 3),
]);
}

test_canBeConst_optionalNamedParameter_nested() async {
await assertDiagnostics(r'''
class A {
const A({A? parent});
const A.a();
}
var a = A(
parent: A.a(),
);
''', [
lint(59, 21),
lint(72, 5),
]);
}

test_canBeConst_optionalNamedParameter_newKeyword() async {
await assertDiagnostics(r'''
class A {
const A({A? parent});
}
var a = new A();
''', [
lint(44, 7),
]);
}

test_cannotBeConst_argumentIsAdjacentStrings_withInterpolation() async {
await assertNoDiagnostics(r'''
class A {
const A(String s);
}
A f(int i) => A('adjacent' '$i');
''');
}

test_cannotBeConst_argumentIsListLiteral_nonLiteralElement() async {
await assertNoDiagnostics(r'''
class A {
const A(List<int> l);
}
A f(int i) => A([i]);
''');
}

test_cannotBeConst_argumentIsLocalVariable() async {
await assertNoDiagnostics(r'''
class A {
const A(String s);
}
void f() {
final s = '';
var a = A(s);
}
''');
}

test_cannotBeConst_argumentIsNonLiteral() async {
await assertNoDiagnostics(r'''
class A {
const A(String s);
}
A f(String s) => A(s);
''');
}

test_cannotBeConst_argumentIsNonLiteralList() async {
await assertNoDiagnostics(r'''
class A {
const A(List<int> l);
}
A f(List<int> l) => A(l);
''');
}

test_cannotBeConst_explicitTypeArgument_typeVariable() async {
await assertNoDiagnostics(r'''
class A<T> {
const A();
}
void f<U>() => A<U>();
''');
}

test_cannotBeConst_nonConstArgument() async {
await assertNoDiagnostics(r'''
class A {
final int x;
const A(this.x);
}
A f(int x) => A(x);
''');
}

test_cannotBeConst_notConstConstructor() async {
await assertNoDiagnostics(r'''
class A {
A();
}
var a = A();
''');
}

test_cannotBeConst_stringLiteralArgument_withInterpolation() async {
await assertNoDiagnostics(r'''
class A {
const A(String s);
static A m1(int i) => A('$i');
}
''');
}

test_deferred_arg() async {
newFile2('$testPackageLibPath/a.dart', '''
class A {
Expand Down Expand Up @@ -76,4 +284,30 @@ K k() {
error(WarningCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR, 90, 3),
]);
}

test_isConst_intLiteralArgument() async {
await assertNoDiagnostics(r'''
class A {
final int x;
const A(this.x);
}
A f() => const A(5);
''');
}

test_isConstCall_optionalNamedParameter() async {
await assertNoDiagnostics(r'''
class A {
const A({A? parent});
}
var a = const A();
''');
}

test_objectConstructorCall() async {
await assertNoDiagnostics(r'''
var x = Object();
''');
}
}
100 changes: 0 additions & 100 deletions test_data/rules/prefer_const_constructors.dart

This file was deleted.

0 comments on commit c671de2

Please sign in to comment.