Skip to content

Commit

Permalink
linter: Migrate prefer_collection_literals tests.
Browse files Browse the repository at this point in the history
Change-Id: I6bdb49e67024ea450f2f347f9408b2a9555ed31e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/385300
Auto-Submit: Samuel Rawlins <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
  • Loading branch information
srawlins authored and Commit Queue committed Sep 17, 2024
1 parent 00a1608 commit bdddf5e
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 89 deletions.
263 changes: 263 additions & 0 deletions pkg/linter/test/rules/prefer_collection_literals_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,140 @@ f() => LinkedHashMap();
]);
}

test_iterable_emptyConstructor_iterableDeclaration() async {
await assertNoDiagnostics(r'''
void f() {
Iterable x = Iterable.empty();
}
''');
}

test_linkedHashMap_unnamedConstructor() async {
await assertDiagnostics(r'''
import 'dart:collection';
void f() {
LinkedHashMap();
}
''', [
lint(39, 15),
]);
}

test_linkedHashMap_unnamedConstructor_linkedHashMapParameterType() async {
await assertNoDiagnostics(r'''
import 'dart:collection';
void f() {
g(LinkedHashMap<int, int>());
}
void g(LinkedHashMap<int, int> p) {}
''');
}

test_linkedHashMap_unnamedConstructor_mapParameterType() async {
await assertDiagnostics(r'''
import 'dart:collection';
void f() {
g(LinkedHashMap<int, int>());
}
void g(Map<int, int> p) {}
''', [
lint(41, 25),
]);
}

test_linkedHashSet_fromConstructor() async {
await assertDiagnostics(r'''
import 'dart:collection';
void f() {
LinkedHashSet.from(['foo', 'bar', 'baz']);
}
''', [
lint(39, 41),
]);
}

test_linkedHashSet_fromConstructor_linkedHashSetDeclaration() async {
await assertNoDiagnostics(r'''
import 'dart:collection';
void f() {
LinkedHashSet<int> x = LinkedHashSet.from([1, 2, 3]);
}
''');
}

test_linkedHashSet_fromConstructor_setDeclaration() async {
await assertDiagnostics(r'''
import 'dart:collection';
void f() {
Set<int> x = LinkedHashSet.from([1, 2, 3]);
}
''', [
lint(52, 29),
]);
}

test_linkedHashSet_ofConstructor() async {
await assertDiagnostics(r'''
import 'dart:collection';
void f() {
LinkedHashSet.of(['foo', 'bar', 'baz']);
}
''', [
lint(39, 39),
]);
}

test_linkedHashSet_unnamedConstructor() async {
await assertDiagnostics(r'''
import 'dart:collection';
void f() {
LinkedHashSet();
}
''', [
lint(39, 15),
]);
}

test_linkedHashSet_unnamedConstructor_hashSetParameterType() async {
await assertNoDiagnostics(r'''
import 'dart:collection';
void f() {
g(LinkedHashSet<int>());
}
void g(LinkedHashSet<int> p) {}
''');
}

test_linkedHashSet_unnamedConstructor_linkedHashSetDeclaration() async {
await assertNoDiagnostics(r'''
import 'dart:collection';
void f() {
LinkedHashSet<int> x = LinkedHashSet<int>();
}
''');
}

test_linkedHashSet_unnamedConstructor_moreArgs() async {
await assertNoDiagnostics(r'''
import 'dart:collection';
void f() {
LinkedHashSet(equals: (a, b) => false, hashCode: (o) => 13)
..addAll({});
}
''');
}

test_linkedHashSet_unnamedConstructor_setDeclaration() async {
await assertDiagnostics(r'''
import 'dart:collection';
void f() {
Set<int> x = LinkedHashSet<int>();
}
''', [
lint(52, 20),
]);
}

/// https://github.com/dart-lang/linter/issues/2985
test_linkedHashSetParameter_named_type_required() async {
await assertNoDiagnostics(r'''
Expand Down Expand Up @@ -181,6 +315,66 @@ void c() {
]);
}

test_list_filledConstructor() async {
await assertNoDiagnostics(r'''
void f() {
List.filled(5, true);
}
''');
}

test_listLiteral() async {
await assertNoDiagnostics(r'''
void f() {
[];
}
''');
}

test_listLiteral_toSet() async {
await assertDiagnostics(r'''
void f() {
['foo', 'bar', 'baz'].toSet();
}
''', [
lint(13, 29),
]);
}

test_map_identityConstructor() async {
await assertNoDiagnostics(r'''
void f() {
Map.identity();
}
''');
}

test_map_unmodifiableConstructor() async {
await assertNoDiagnostics(r'''
void f() {
Map.unmodifiable({});
}
''');
}

test_map_unnamedConstructor() async {
await assertDiagnostics(r'''
void f() {
Map();
}
''', [
lint(13, 5),
]);
}

test_mapLiteral() async {
await assertNoDiagnostics(r'''
void f() {
var x = {};
}
''');
}

test_returnStatement_async() async {
await assertNoDiagnostics(r'''
import 'dart:collection';
Expand Down Expand Up @@ -247,6 +441,75 @@ Iterable<LinkedHashSet<int>> f() sync* {
''');
}

test_set_fromConstructor() async {
await assertDiagnostics(r'''
void f() {
Set.from(['foo', 'bar', 'baz']);
}
''', [
lint(13, 31),
]);
}

test_set_fromConstructor_withTypeArgs() async {
await assertDiagnostics(r'''
void f() {
Set<int>.from([]);
}
''', [
lint(13, 17),
]);
}

test_set_identityConstructor() async {
await assertNoDiagnostics(r'''
void f() {
Set.identity();
}
''');
}

test_set_ofConstructor() async {
await assertDiagnostics(r'''
void f() {
Set.of(['foo', 'bar', 'baz']);
}
''', [
lint(13, 29),
]);
}

test_set_unnamedConstructor() async {
await assertDiagnostics(r'''
void f() {
Set();
}
''', [
lint(13, 5),
]);
}

test_set_unnamedConstructor_objectParameterType() async {
await assertDiagnostics(r'''
void f() {
g(Set());
}
void g(Object p) {}
''', [
lint(15, 5),
]);
}

test_set_unnamedContsructor_explicitTypeArgs() async {
await assertDiagnostics(r'''
void f() {
Set<int>();
}
''', [
lint(13, 10),
]);
}

test_typedefConstruction() async {
await assertNoDiagnostics(r'''
typedef MyMap = Map<int, int>;
Expand Down
89 changes: 0 additions & 89 deletions pkg/linter/test_data/rules/prefer_collection_literals.dart

This file was deleted.

0 comments on commit bdddf5e

Please sign in to comment.