From: David ‘Bombe’ Roden Date: Wed, 5 Oct 2016 17:50:27 +0000 (+0200) Subject: Add test for PartContainer X-Git-Tag: 0.9.6^2~44 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2bff2bdea02c092b1f0aa7978a961a2ebf3b2130;p=Sone.git Add test for PartContainer --- 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)); + } + +}