Skip to content

Commit

Permalink
Fixed coverage
Browse files Browse the repository at this point in the history
Fixed issues reported by tests
  • Loading branch information
NicolaVerbeeck committed Apr 1, 2021
1 parent 2565ccd commit 9a90a9c
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,5 @@ gradle-app.setting
### Gradle Patch ###
**/build/

# End of https://www.toptal.com/developers/gitignore/api/dart,flutter,intellij+all,git,macos,windows,linux,gradle
# End of https://www.toptal.com/developers/gitignore/api/dart,flutter,intellij+all,git,macos,windows,linux,gradle
/test/coverage_helper_test.dart
43 changes: 27 additions & 16 deletions lib/src/extension/list_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,49 @@ extension ListExtensions<T> on List<T> {
addAll(newData);
}

/// Sorts the list based on the comparable returned by [by]
void sortBy<R>(Comparable<R>? by(T item)) {
/// Sorts the list based on the comparable returned by [by]. By default
/// the sorting is [ascending]
void sortBy<R>(Comparable<R>? by(T item), {bool ascending = true}) {
sort((a, b) {
final byA = by(a);
final byB = by(b);
if (byA == null) return -1;
if (byB == null) return 1;
return _compareValues(byA, byB);
if (byA == null) return ascending ? -1 : 1;
if (byB == null) return ascending ? 1 : -1;
return _compareValues(byA, byB, ascending);
});
}

/// Sorts the list by comparing first comparing using [by] and if the items
/// are equal, by comparing them using [by2]
void sortBy2<R>(Comparable<R>? by(T item), Comparable<R>? by2(T item)) {
/// are equal, by comparing them using [by2]. By default
/// the sorting is [ascending]
void sortBy2<R>(
Comparable<R>? by(T item),
Comparable<R>? by2(T item), {
bool ascending = true,
}) {
sort((a, b) {
final byA = by(a);
final byB = by(b);
if (byA == null) return -1;
if (byB == null) return 1;
final result = _compareValues(byA, byB);
if (result != 0) return result;
if (byA == null && byB != null) return ascending ? -1 : 1;
if (byB == null && byA != null) return ascending ? 1 : -1;
if (byA != null && byB != null) {
final result = _compareValues(byA, byB, ascending);
if (result != 0) return result;
}

final byA2 = by2(a);
final byB2 = by2(b);
if (byA2 == null) return -1;
if (byB2 == null) return 1;
return _compareValues(byA2, byB2);
if (byA2 == null && byB2 == null) return 0;
if (byA2 == null) return ascending ? -1 : 1;
if (byB2 == null) return ascending ? 1 : -1;
return _compareValues(byA2, byB2, ascending);
});
}
}

int _compareValues<T extends Comparable<dynamic>>(T a, T b) {
int _compareValues<T extends Comparable<dynamic>>(T a, T b, bool ascending) {
if (identical(a, b)) return 0;
return a.compareTo(b);
if (ascending)
return a.compareTo(b);
return -a.compareTo(b);
}
10 changes: 10 additions & 0 deletions test/extension/iterable_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,15 @@ void main() {
expect(result.item1, [1, 2, 3, 4, 5, 6, 7]);
expect(result.item2, [8, 9, 10]);
});
test('Test map indexed', () {
final seenIndexes = <int>[];
final seenValues = <int>[];
sut.mapIndexed((index, e) {
seenIndexes.add(index);
seenValues.add(e);
}).toList(growable: false);
expect(seenIndexes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(seenValues, sut);
});
});
}
90 changes: 85 additions & 5 deletions test/extension/list_extension_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:icapps_architecture/icapps_architecture.dart';
import 'package:tuple/tuple.dart';

void main() {
late List<int> sut;
Expand All @@ -20,14 +21,93 @@ void main() {
sut.sortBy((e) => e % 2 == 0 ? null : e);
expect(sut, [2, 4, 6, 8, 10, 1, 3, 5, 7, 9]);
});
test('Test sort by descending', () {
sut.sortBy((e) => e, ascending: false);
expect(sut, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
});
test('Test sort by null descending', () {
sut.sortBy((e) => e % 2 == 0 ? null : e, ascending: false);
expect(sut, [9, 7, 5, 3, 1, 10, 8, 6, 4, 2]);
});
test('Test sort by2', () {
sut.sortBy2((e) => e.toString(), (e) => 10 - e);
expect(sut, [1, 10, 2, 3, 4, 5, 6, 7, 8, 9]);
final complexSut = <Tuple2<String, int>>[
Tuple2("ABC", 2),
Tuple2("ABC", 1),
Tuple2("BCD", 25),
Tuple2("ABCDE", 25),
];

complexSut.sortBy2((e) => e.item1, (e) => e.item2);
expect(complexSut, <Tuple2<String, int>>[
Tuple2("ABC", 1),
Tuple2("ABC", 2),
Tuple2("ABCDE", 25),
Tuple2("BCD", 25),
]);
});
test('Test sort by2, descending', () {
final complexSut = <Tuple2<String, int>>[
Tuple2("ABC", 2),
Tuple2("ABC", 1),
Tuple2("BCD", 25),
Tuple2("ABCDE", 25),
];

complexSut.sortBy2((e) => e.item1, (e) => e.item2, ascending: false);
expect(complexSut, <Tuple2<String, int>>[
Tuple2("BCD", 25),
Tuple2("ABCDE", 25),
Tuple2("ABC", 2),
Tuple2("ABC", 1),
]);
});
test('Test sort by2 nullable', () {
sut.sortBy2((e) => e % 2 == 0 ? null : e.toString(),
(e) => e % 2 == 0 ? null : 10 - e);
expect(sut, [2, 4, 6, 8, 10, 1, 3, 5, 7, 9]);
final complexSut = <Tuple2<String?, int?>>[
Tuple2("ABC", 2),
Tuple2("ABC", 1),
Tuple2("BCD", 25),
Tuple2(null, null),
Tuple2("ABCDE", 25),
Tuple2(null, 25),
Tuple2(null, 24),
Tuple2("ABC", null),
];

complexSut.sortBy2((e) => e.item1, (e) => e.item2);
expect(complexSut, <Tuple2<String?, int?>>[
Tuple2(null, null),
Tuple2(null, 24),
Tuple2(null, 25),
Tuple2("ABC", null),
Tuple2("ABC", 1),
Tuple2("ABC", 2),
Tuple2("ABCDE", 25),
Tuple2("BCD", 25),
]);
});
test('Test sort by2 nullable descending', () {
final complexSut = <Tuple2<String?, int?>>[
Tuple2("ABC", 2),
Tuple2("ABC", 1),
Tuple2(null, null),
Tuple2("BCD", 25),
Tuple2("ABCDE", 25),
Tuple2(null, 25),
Tuple2(null, 24),
Tuple2("ABC", null),
];

complexSut.sortBy2((e) => e.item1, (e) => e.item2, ascending: false);
expect(complexSut, <Tuple2<String?, int?>>[
Tuple2("BCD", 25),
Tuple2("ABCDE", 25),
Tuple2("ABC", 2),
Tuple2("ABC", 1),
Tuple2("ABC", null),
Tuple2(null, 25),
Tuple2(null, 24),
Tuple2(null, null),
]);
});
});
}

0 comments on commit 9a90a9c

Please sign in to comment.