More test cases

This commit is contained in:
Ming Ming 2021-04-18 19:35:20 +08:00
parent 6bc0e8d0d3
commit be1e642a95
2 changed files with 122 additions and 0 deletions

View file

@ -0,0 +1,40 @@
import 'package:nc_photos/iterable_extension.dart';
import 'package:test/test.dart';
import 'package:tuple/tuple.dart';
void main() {
group("IterableExtension", () {
test("sorted", () {
final src = [1, 4, 5, 2, 3, 8, 6, 7];
expect(src.sorted(), [1, 2, 3, 4, 5, 6, 7, 8]);
});
test("mapWithIndex", () {
final src = [1, 4, 5, 2, 3];
final result =
src.mapWithIndex((index, element) => Tuple2(index, element)).toList();
expect(result[0], Tuple2(0, 1));
expect(result[1], Tuple2(1, 4));
expect(result[2], Tuple2(2, 5));
expect(result[3], Tuple2(3, 2));
expect(result[4], Tuple2(4, 3));
});
test("containsIf", () {
final src = [
_ContainsIfTest(1),
_ContainsIfTest(4),
_ContainsIfTest(5),
_ContainsIfTest(2),
_ContainsIfTest(3),
];
expect(src.containsIf(_ContainsIfTest(5), (a, b) => a.x == b.x), true);
});
});
}
class _ContainsIfTest {
_ContainsIfTest(this.x);
final int x;
}

View file

@ -0,0 +1,82 @@
import 'package:nc_photos/list_extension.dart';
import 'package:quiver/core.dart';
import 'package:test/test.dart';
void main() {
group("ListExtension", () {
group("distinct", () {
test("primitive", () {
expect([1, 2, 3, 4, 5, 3, 2, 4, 6].distinct(), [1, 2, 3, 4, 5, 6]);
});
test("class", () {
expect(
[
_DistinctTest(1, 1),
_DistinctTest(2, 2),
_DistinctTest(3, 3),
_DistinctTest(4, 4),
_DistinctTest(5, 4),
_DistinctTest(3, 6),
_DistinctTest(2, 2),
_DistinctTest(4, 8),
_DistinctTest(6, 9),
].distinct(),
[
_DistinctTest(1, 1),
_DistinctTest(2, 2),
_DistinctTest(3, 3),
_DistinctTest(4, 4),
_DistinctTest(5, 4),
_DistinctTest(3, 6),
_DistinctTest(4, 8),
_DistinctTest(6, 9),
]);
});
});
test("distinctIf", () {
expect(
[
_DistinctTest(1, 1),
_DistinctTest(2, 2),
_DistinctTest(3, 3),
_DistinctTest(4, 4),
_DistinctTest(5, 5),
_DistinctTest(3, 6),
_DistinctTest(2, 7),
_DistinctTest(4, 8),
_DistinctTest(6, 9),
].distinctIf((a, b) => a.x == b.x, (a) => a.x),
[
_DistinctTest(1, 1),
_DistinctTest(2, 2),
_DistinctTest(3, 3),
_DistinctTest(4, 4),
_DistinctTest(5, 5),
_DistinctTest(6, 9),
]);
});
test("takeIndex", () {
expect([1, 2, 3, 4, 5, 6].takeIndex([5, 4, 3, 1, 0]), [6, 5, 4, 2, 1]);
});
});
}
class _DistinctTest {
_DistinctTest(this.x, this.y);
@override
operator ==(Object other) =>
other is _DistinctTest && x == other.x && y == other.y;
@override
get hashCode => hash2(x, y);
@override
toString() => "{x: $x, y: $y}";
final int x;
final int y;
}