From 2bff2bdea02c092b1f0aa7978a961a2ebf3b2130 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 5 Oct 2016 19:50:27 +0200 Subject: [PATCH] Add test for PartContainer --- .../pterodactylus/sone/text/PartContainerTest.java | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/text/PartContainerTest.java diff --git a/src/test/java/net/pterodactylus/sone/text/PartContainerTest.java b/src/test/java/net/pterodactylus/sone/text/PartContainerTest.java new file mode 100644 index 0000000..dfbaa21 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/text/PartContainerTest.java @@ -0,0 +1,94 @@ +package net.pterodactylus.sone.text; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import org.junit.Test; + +/** + * Unit test for {@link PartContainer}. + * + * @author David ‘Bombe’ Roden + */ +public class PartContainerTest { + + private final PartContainer container = new PartContainer(); + + @Test + public void emptyContainerHasSizeZero() { + assertThat(container.size(), is(0)); + } + + @Test + public void containerWithSinglePartHasSizeOne() { + container.add(mock(Part.class)); + assertThat(container.size(), is(1)); + } + + @Test + public void containerWithSinglePartCanReturnPart() { + Part part = mock(Part.class); + container.add(part); + assertThat(container.getPart(0), is(part)); + } + + @Test + public void containerIsEmptyAfterPartIsAddedAndRemoved() { + container.add(mock(Part.class)); + container.removePart(0); + assertThat(container.size(), is(0)); + } + + @Test + public void containerContainsSecondPartIfFirstPartIsRemoved() { + container.add(mock(Part.class)); + Part part = mock(Part.class); + container.add(part); + container.removePart(0); + assertThat(container.getPart(0), is(part)); + } + + @Test + public void textOfContainerPartIsTextOfPartsConcatenated() { + container.add(createPartWithText("first")); + container.add(createPartWithText("second")); + assertThat(container.getText(), is("firstsecond")); + } + + private Part createPartWithText(String text) { + Part part = mock(Part.class); + when(part.getText()).thenReturn(text); + return part; + } + + @Test(expected = NoSuchElementException.class) + public void emptyContainerIteratorThrowsOnNext() { + container.iterator().next(); + } + + @Test + public void iteratorIteratesPartsRecursivelyInCorrectOrder() { + Part firstPart = mock(Part.class); + PartContainer secondPart = new PartContainer(); + Part thirdPart = mock(Part.class); + Part nestedFirstPart = mock(Part.class); + Part nestedSecondPart = mock(Part.class); + secondPart.add(nestedFirstPart); + secondPart.add(nestedSecondPart); + container.add(firstPart); + container.add(secondPart); + container.add(thirdPart); + Iterator parts = container.iterator(); + assertThat(parts.next(), is(firstPart)); + assertThat(parts.next(), is(nestedFirstPart)); + assertThat(parts.next(), is(nestedSecondPart)); + assertThat(parts.next(), is(thirdPart)); + assertThat(parts.hasNext(), is(false)); + } + +} -- 2.7.4