nc-photos/app/test/iterable_extension_test.dart

130 lines
3.2 KiB
Dart
Raw Normal View History

2022-08-06 19:00:38 +02:00
import 'package:nc_photos/int_extension.dart';
2021-04-18 13:35:20 +02:00
import 'package:nc_photos/iterable_extension.dart';
2021-11-19 14:47:22 +01:00
import 'package:quiver/core.dart';
2021-04-18 13:35:20 +02:00
import 'package:test/test.dart';
import 'package:tuple/tuple.dart';
void main() {
group("IterableExtension", () {
2021-04-26 12:54:57 +02:00
test("withIndex", () {
final src = [1, 4, 5, 2, 3];
final result = src.withIndex().toList();
2021-09-15 08:58:06 +02:00
expect(result[0], const Tuple2(0, 1));
expect(result[1], const Tuple2(1, 4));
expect(result[2], const Tuple2(2, 5));
expect(result[3], const Tuple2(3, 2));
expect(result[4], const Tuple2(4, 3));
2021-04-26 12:54:57 +02:00
});
2021-04-18 13:35:20 +02:00
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);
});
2021-11-19 14:47:22 +01:00
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),
]);
});
2022-07-25 07:55:47 +02:00
group("indexOf", () {
test("start = 0", () {
expect([1, 2, 3, 4, 5].indexOf(3), 2);
});
test("start > 0", () {
expect([1, 2, 3, 4, 5].indexOf(3, 2), 2);
expect([1, 2, 3, 4, 5].indexOf(3, 3), -1);
});
});
2022-08-06 19:00:38 +02:00
test("withPartition", () async {
expect(
await 0.until(10).withPartition((sublist) => [sublist], 4),
[
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9],
],
);
});
2021-04-18 13:35:20 +02:00
});
}
class _ContainsIfTest {
_ContainsIfTest(this.x);
final int x;
}
2021-11-19 14:47:22 +01:00
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;
}