Skip to content

Commit

Permalink
Merge pull request #15 from passsy/feature/comparison_tests
Browse files Browse the repository at this point in the history
Add comparisons tests
  • Loading branch information
passsy authored Dec 2, 2018
2 parents 687ff86 + 25c03bc commit ebf5f91
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
24 changes: 6 additions & 18 deletions lib/src/comparisons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,18 @@ Comparator<T> reverse<T>(Comparator<T> comparator) {
return compareTo;
}

//class _ReversedComparator<T> {
// _ReversedComparator(this.comparator);
//
// Comparator<T> comparator;
//
// int compareTo(T a, T b) => comparator(b, a);
//}

/**
* Creates a comparator using the function to transform value to a [Comparable] instance for comparison.
*/
Comparator<T> compareBy<T>(Comparable Function(T) selector) {
int compareTo(T a, T b) => selector(a).compareTo(selector(b));

return compareTo;
}

/**
* Creates a descending comparator using the function to transform value to a [Comparable] instance for comparison.
*/
Comparator<T> compareByDescending<T>(Comparable Function(T) selector) {
int compareTo(T a, T b) => selector(b).compareTo(selector(a));

return compareTo;
}

//class _CompareBy<T> {
// Comparable Function(T) selector;
//
// _CompareBy(this.selector);
//
// int compareTo(T a, T b) => selector(a).compareTo(selector(b));
//}
28 changes: 28 additions & 0 deletions test/comparisons_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:dart_kollection/dart_kollection.dart';
import 'package:test/test.dart';

void main() {
group('compare in natural order', () {
test("naturalOrder", () {
final list = listOf([3, 4, 5, 2, 1]);
final natural = list.sortedWith(naturalOrder());
expect(natural, listOf([1, 2, 3, 4, 5]));
});
});

group('compare reversed order', () {
test("reverseOrder", () {
final list = listOf([3, 4, 5, 2, 1]);
final natural = list.sortedWith(reverseOrder());
expect(natural, listOf([5, 4, 3, 2, 1]));
});
});

group("reverse custom comparator", () {
test("reverse natural", () {
final list = listOf([3, 4, 5, 2, 1]);
final reversedNatural = list.sortedWith(reverse(naturalOrder()));
expect(reversedNatural, listOf([5, 4, 3, 2, 1]));
});
});
}
2 changes: 2 additions & 0 deletions test/dart_kollection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import 'collection/map_mutable_extensions_test.dart' as map_mutable_extensions_t
import 'collection/map_test.dart' as map_test;
import 'collection/set_test.dart' as set_test;
import 'collections_test.dart' as collections_test;
import 'comparisons_test.dart' as comparisons_test;
import 'exceptions_test.dart' as exceptions_test;
import 'tuples_test.dart' as tuples_test;

main() {
collections_test.main();
comparisons_test.main();
exceptions_test.main();
tuples_test.main();
iterable_extensions_test.main();
Expand Down

0 comments on commit ebf5f91

Please sign in to comment.