Skip to content

Commit

Permalink
Merge pull request #41 from passsy/feature/fold_reduce
Browse files Browse the repository at this point in the history
Add fold(Right) and reduce(Right) tests
  • Loading branch information
passsy authored Dec 26, 2018
2 parents e2f4366 + 061c4cd commit fc3db4e
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
73 changes: 73 additions & 0 deletions test/collection/iterable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,53 @@ void testIterable(KIterable<T> Function<T>() emptyIterable,
});
});

group("fold", () {
if (ordered) {
test("fold division", () {
final iterable = iterableOf([
[1, 2],
[3, 4],
[5, 6]
]);
final result = iterable.fold(
listOf<int>(), (KList<int> acc, it) => acc + listOf(it));
expect(result, listOf([1, 2, 3, 4, 5, 6]));
});
}

test("operation must be non null", () {
final e = catchException<ArgumentError>(
() => emptyIterable().fold("foo", null));
expect(e.message, allOf(contains("null"), contains("operation")));
});
});

group("foldIndexed", () {
if (ordered) {
test("foldIndexed division", () {
final iterable = iterableOf([
[1, 2],
[3, 4],
[5, 6]
]);
var i = 0;
final result =
iterable.foldIndexed(listOf<int>(), (index, KList<int> acc, it) {
expect(index, i);
i++;
return acc + listOf(it);
});
expect(result, listOf([1, 2, 3, 4, 5, 6]));
});
}

test("operation must be non null", () {
final e = catchException<ArgumentError>(
() => emptyIterable().foldIndexed("foo", null));
expect(e.message, allOf(contains("null"), contains("operation")));
});
});

group("groupBy", () {
if (ordered) {
test("basic", () {
Expand Down Expand Up @@ -1052,6 +1099,32 @@ void testIterable(KIterable<T> Function<T>() emptyIterable,
});
});

group("reduceIndexed", () {
test("reduceIndexed", () {
var i = 1;
final result =
iterableOf([1, 2, 3, 4]).reduceIndexed((index, int acc, it) {
expect(index, i);
i++;
return it + acc;
});
expect(result, 10);
});

test("empty throws", () {
expect(
() => emptyIterable<int>()
.reduceIndexed((index, int acc, it) => it + acc),
throwsUnsupportedError);
});

test("reduceIndexed doesn't allow null as operation", () {
final iterable = emptyIterable<String>();
var e = catchException<ArgumentError>(() => iterable.reduceIndexed(null));
expect(e.message, allOf(contains("null"), contains("operation")));
});
});

group("reversed", () {
test("mutliple", () {
final result = iterableOf([1, 2, 3, 4]).reversed();
Expand Down
44 changes: 44 additions & 0 deletions test/collection/list_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,49 @@ void main() {
});
});

group("foldRight", () {
test("foldRight division", () {
final iterable = listOf([
[1, 2],
[3, 4],
[5, 6]
]);
final result = iterable.foldRight(
listOf<int>(), (it, KList<int> acc) => acc + listOf(it));
expect(result, listOf([5, 6, 3, 4, 1, 2]));
});

test("operation must be non null", () {
final e = catchException<ArgumentError>(
() => emptyList().foldRight("foo", null));
expect(e.message, allOf(contains("null"), contains("operation")));
});
});

group("foldRightIndexed", () {
test("foldRightIndexed division", () {
final iterable = listOf([
[1, 2],
[3, 4],
[5, 6]
]);
var i = 2;
final result =
iterable.foldRightIndexed(listOf<int>(), (index, it, KList<int> acc) {
expect(index, i);
i--;
return acc + listOf(it);
});
expect(result, listOf([5, 6, 3, 4, 1, 2]));
});

test("operation must be non null", () {
final e = catchException<ArgumentError>(
() => emptyList().foldRightIndexed("foo", null));
expect(e.message, allOf(contains("null"), contains("operation")));
});
});

group("first", () {
test("get first element", () {
expect(listOf(["a", "b"]).first(), "a");
Expand Down Expand Up @@ -99,6 +142,7 @@ void main() {
expect(e.message, allOf(contains("null"), contains("defaultValue")));
});
});

group("getOrNull", () {
test("get item", () {
final list = listOf(["a", "b", "c"]);
Expand Down
27 changes: 27 additions & 0 deletions test/collection/list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ void main() {
});
});

group("reduceRightIndexed", () {
test("reduceRightIndexed", () {
var i = 2;
final result =
listOf([1, 2, 3, 4]).reduceRightIndexed((index, it, int acc) {
expect(index, i);
i--;
return it + acc;
});
expect(result, 10);
});

test("empty throws", () {
expect(
() => emptyList<int>()
.reduceRightIndexed((index, it, int acc) => it + acc),
throwsUnsupportedError);
});

test("reduceRightIndexed doesn't allow null as operation", () {
final list = emptyList<String>();
var e =
catchException<ArgumentError>(() => list.reduceRightIndexed(null));
expect(e.message, allOf(contains("null"), contains("operation")));
});
});

test("sublist works ", () {
final list = listOf(["a", "b", "c"]);
final subList = list.subList(1, 3);
Expand Down

0 comments on commit fc3db4e

Please sign in to comment.