Skip to content

Commit

Permalink
Merge pull request #20 from passsy/feature/test_firstOrNull
Browse files Browse the repository at this point in the history
Bugfix: Make firstOrNull return null for empty lists
  • Loading branch information
passsy committed Dec 6, 2018
2 parents f8d776d + 8368ef0 commit 0887669
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/src/extension/iterable_extension_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ abstract class KIterableExtensionsMixin<T>
}
final i = iterator();
if (!i.hasNext()) {
throw NoSuchElementException("Collection is empty");
return null;
}
return i.next();
} else {
Expand Down
21 changes: 21 additions & 0 deletions test/collection/iterable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,27 @@ void testIterable(KIterable<T> Function<T>() emptyIterable,
});
});

group("firstOrNull", () {
if (ordered) {
test("get first element", () {
expect(iterableOf(["a", "b"]).firstOrNull(), "a");
});
} else {
test("get random first element", () {
var result = iterableOf(["a", "b"]).firstOrNull();
expect(result == "a" || result == "b", true);
});
}

test("firstOrNull returns null for empty", () {
expect(emptyIterable().firstOrNull(), isNull);
});

test("finds nothing throws", () {
expect(iterableOf<String>(["a"]).firstOrNull((it) => it == "b"), isNull);
});
});

group("flatMap", () {
test("flatMap int to string", () {
final iterable = iterableOf([1, 2, 3]);
Expand Down

0 comments on commit 0887669

Please sign in to comment.