Skip to content

Commit

Permalink
Add missing size validation test for last(int) (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Oct 10, 2024
1 parent 8b87a87 commit 6a27ce6
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/test/java/com/pivovarit/gatherers/LastTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@

import java.util.stream.Stream;

import static com.pivovarit.gatherers.MoreGatherers.last;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class LastTest {

@Test
void shouldRejectInvalidSize() {
assertThatThrownBy(() -> last(0))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("number of elements can't be lower than one");
}

@Test
void shouldLastEmpty() {
assertThat(Stream.of().gather(MoreGatherers.last(42))).isEmpty();
assertThat(Stream.of().gather(last(42))).isEmpty();
}

@Test
void shouldTakeLastElement() {
assertThat(Stream.of(1, 2, 3).gather(MoreGatherers.last(1))).containsExactly(3);
assertThat(Stream.of(1, 2, 3).gather(last(1))).containsExactly(3);
}

@Test
void shouldTakeLastNElements() {
assertThat(Stream.of(1, 2, 3).gather(MoreGatherers.last(2))).containsExactly(2, 3);
assertThat(Stream.of(1, 2, 3).gather(last(2))).containsExactly(2, 3);
}

@Test
void shouldTakeLastAllElements() {
assertThat(Stream.of(1, 2, 3).gather(MoreGatherers.last(42))).containsExactly(1, 2, 3);
assertThat(Stream.of(1, 2, 3).gather(last(42))).containsExactly(1, 2, 3);
}
}

0 comments on commit 6a27ce6

Please sign in to comment.