Skip to content

Commit

Permalink
Merge pull request #24 from passsy/feature/indexed_setter_operator
Browse files Browse the repository at this point in the history
Bugfix KList.last() which returned first()
  • Loading branch information
passsy authored Dec 14, 2018
2 parents 9329221 + 6f533b9 commit c646537
Show file tree
Hide file tree
Showing 3 changed files with 46 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 @@ -591,7 +591,7 @@ abstract class KIterableExtensionsMixin<T>
if (predicate == null) {
if (this is KList) {
var list = (this as KList);
return list.isEmpty() ? null : list.get(0);
return list.isEmpty() ? null : list.get(list.lastIndex);
} else {
final i = iterator();
if (!i.hasNext()) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/extension/list_extension_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ abstract class KListExtensionsMixin<T> implements KListExtension<T>, KList<T> {
@override
T last([bool Function(T) predicate]) {
if (predicate == null) {
if (isEmpty()) throw NoSuchElementException("List is empty.");
return get(lastIndex);
} else {
final i = listIterator(size);
Expand Down
44 changes: 44 additions & 0 deletions test/collection/iterable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,50 @@ void testIterable(KIterable<T> Function<T>() emptyIterable,
});
});

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

test("last throws for no elements", () {
expect(() => emptyIterable().last(),
throwsA(TypeMatcher<NoSuchElementException>()));
});

test("finds nothing throws", () {
expect(() => iterableOf<String>(["a"]).last((it) => it == "b"),
throwsA(TypeMatcher<NoSuchElementException>()));
});
});

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

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

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

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

0 comments on commit c646537

Please sign in to comment.