Skip to content

Commit

Permalink
Add removeLastOrNull()
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Oct 31, 2021
1 parent 1eb8c03 commit f173b94
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 77 deletions.
9 changes: 9 additions & 0 deletions lib/src/collection/kt_list_mutable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ extension KtMutableListExtensions<T> on KtMutableList<T> {
}
return removeAt(lastIndex);
}

/// Removes the last element from this mutable list.
///
/// Returns that removed element, or throws [NoSuchElementException] if this list is empty.
T? removeLastOrNull() {
if (isEmpty()) return null;
return removeAt(lastIndex);
}

/// Reverses elements in the list in-place.
void reverse() {
final mid = size >> 1;
Expand Down
102 changes: 102 additions & 0 deletions test/collection/list_mutable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,108 @@ void testList(
});
});

group("removeFirst", () {
test("remove first item when found", () {
final list = mutableListOf("1", "2", "3");
final result = list.removeFirst();
expect(list, mutableListOf("2", "3"));
expect(result, "1");
});
test("remove first item when length is 1", () {
final list = mutableListOf("1");
final result = list.removeFirst();
expect(list, mutableListOf());
expect(result, "1");
});
test("remove first item when null", () {
final list = mutableListOf(null, null);
final result = list.removeFirst();
expect(list, mutableListOf(null));
expect(result, null);
});
test("throw when list is empty", () {
final list = mutableListOf();
expect(() => list.removeFirst(), throwsA(isA<NoSuchElementException>()));
});
});

group("removeFirstOrNull", () {
test("remove first item when found", () {
final list = mutableListOf("1", "2", "3");
final result = list.removeFirstOrNull();
expect(list, mutableListOf("2", "3"));
expect(result, "1");
});
test("remove first item when length is 1", () {
final list = mutableListOf("1");
final result = list.removeFirstOrNull();
expect(list, mutableListOf());
expect(result, "1");
});
test("null when item in list is null", () {
final list = mutableListOf(null, null);
final result = list.removeFirstOrNull();
expect(list, mutableListOf(null));
expect(result, null);
});
test("null when list is empty", () {
final list = mutableListOf();
final result = list.removeFirstOrNull();
expect(list, mutableListOf());
expect(result, null);
});
});

group("removeLast", () {
test("remove last item when found", () {
final list = mutableListOf("1", "2", "3");
final result = list.removeLast();
expect(list, mutableListOf("1", "2"));
expect(result, "3");
});
test("remove last item when length is 1", () {
final list = mutableListOf("1");
final result = list.removeLast();
expect(list, mutableListOf());
expect(result, "1");
});
test("remove last item when null", () {
final list = mutableListOf(null, "2", null);
final result = list.removeLast();
expect(list, mutableListOf(null, "2"));
expect(result, null);
});
test("throw when list is empty", () {
final list = mutableListOf();
expect(() => list.removeLast(), throwsA(isA<NoSuchElementException>()));
});
});

group("removeLastOrNull", () {
test("remove last item when found", () {
final list = mutableListOf("1", "2", "3");
final result = list.removeLastOrNull();
expect(list, mutableListOf("1", "2"));
expect(result, "3");
});
test("remove last item when length is 1", () {
final list = mutableListOf("1");
final result = list.removeLastOrNull();
expect(list, mutableListOf());
expect(result, "1");
});
test("remove last item when null", () {
final list = mutableListOf(null, "2", null);
final result = list.removeLastOrNull();
expect(list, mutableListOf(null, "2"));
expect(result, null);
});
test("null when list is empty", () {
final list = mutableListOf();
expect(list.removeLastOrNull(), null);
});
});

group("sorted", () {
String lastChar(String it) {
final last = it.runes.last;
Expand Down
77 changes: 0 additions & 77 deletions test/collection/list_mutable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,81 +210,4 @@ void testList(
expect(linkedSetOf<num>(1, 2, 3), linkedSetOf<int>(1, 2, 3));
});
});

group("removeFirst", () {
test("remove first item when found", () {
final list = mutableListOf("1", "2", "3");
final result = list.removeFirst();
expect(list, mutableListOf("2", "3"));
expect(result, "1");
});
test("remove first item when length is 1", () {
final list = mutableListOf("1");
final result = list.removeFirst();
expect(list, mutableListOf());
expect(result, "1");
});
test("remove first item when null", () {
final list = mutableListOf(null, null);
final result = list.removeFirst();
expect(list, mutableListOf(null));
expect(result, null);
});
test("throw when list is empty", () {
final list = mutableListOf();
expect(() => list.removeFirst(), throwsA(isA<NoSuchElementException>()));
});
});

group("removeLast", () {
test("remove last item when found", () {
final list = mutableListOf("1", "2", "3");
final result = list.removeLast();
expect(list, mutableListOf("1", "2"));
expect(result, "3");
});
test("remove last item when length is 1", () {
final list = mutableListOf("1");
final result = list.removeLast();
expect(list, mutableListOf());
expect(result, "1");
});
test("remove last item when null", () {
final list = mutableListOf(null, "2", null);
final result = list.removeLast();
expect(list, mutableListOf(null, "2"));
expect(result, null);
});
test("throw when list is empty", () {
final list = mutableListOf();
expect(() => list.removeLast(), throwsA(isA<NoSuchElementException>()));
});
});

group("removeFirstOrNull", () {
test("remove first item when found", () {
final list = mutableListOf("1", "2", "3");
final result = list.removeFirstOrNull();
expect(list, mutableListOf("2", "3"));
expect(result, "1");
});
test("remove first item when length is 1", () {
final list = mutableListOf("1");
final result = list.removeFirstOrNull();
expect(list, mutableListOf());
expect(result, "1");
});
test("null when item in list is null", () {
final list = mutableListOf(null, null);
final result = list.removeFirstOrNull();
expect(list, mutableListOf(null));
expect(result, null);
});
test("null when list is empty", () {
final list = mutableListOf();
final result = list.removeFirstOrNull();
expect(list, mutableListOf());
expect(result, null);
});
});
}

0 comments on commit f173b94

Please sign in to comment.