From 80894c39942cf2fa473490e13b4953edbfbbb8f9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 8 Nov 2013 18:44:57 +0100 Subject: [PATCH] Add unit test for PartContainer. --- .../pterodactylus/sone/text/PartContainerTest.java | 127 +++++++++++++++++++++ 1 file changed, 127 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..9cefedb --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/text/PartContainerTest.java @@ -0,0 +1,127 @@ +/* + * Sone - PartContainerTest.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.text; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.is; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link PartContainer}. + * + * @author David ‘Bombe’ Roden + */ +public class PartContainerTest { + + private final PartContainer partContainer = new PartContainer(); + + @Before + public void setup() { + partContainer.add(createPart("Test!")); + partContainer.add(createPart(" More Test!")); + } + + @Test + public void partContainerCanReturnContainedParts() { + assertThat(partContainer, contains(matchesPart("Test!"), matchesPart(" More Test!"))); + } + + @Test + public void partContainerCanReturnSpecificParts() { + assertThat(partContainer.getPart(0), matchesPart("Test!")); + assertThat(partContainer.getPart(1), matchesPart(" More Test!")); + } + + @Test + public void partContainerKnowsTheNumberOfContainedParts() { + assertThat(partContainer.size(), is(2)); + } + + @Test + public void partContainerReturnsNestedPartsWhenIteratingOverIt() { + addNestedPartContainer(); + assertThat(partContainer, contains(matchesPart("Test!"), matchesPart(" More Test!"), matchesPart(" (nested)"))); + } + + @Test(expected = NoSuchElementException.class) + public void throwsExceptionWhenIteratingOverTheEndOfTheIterator() { + Iterator parts = partContainer.iterator(); + parts.next(); + parts.next(); + parts.next(); + } + + @Test + public void removingElementsFromTheIteratorDoesNotRemoveThemFromTheContainer() { + Iterator parts = partContainer.iterator(); + parts.remove(); + assertThat(partContainer.size(), is(2)); + } + + @Test + public void textOfContainedPartsIsConcatenated() { + addNestedPartContainer(); + assertThat(partContainer.getText(), is("Test! More Test! (nested)")); + } + + @Test + public void partsCanBeRemoved() { + partContainer.removePart(1); + assertThat(partContainer, contains(matchesPart("Test!"))); + } + + private void addNestedPartContainer() { + Part nestedPart = createPart(" (nested)"); + PartContainer nestedContainer = new PartContainer(); + nestedContainer.add(nestedPart); + partContainer.add(nestedContainer); + } + + private static Matcher matchesPart(final String text) { + return new TypeSafeMatcher() { + @Override + protected boolean matchesSafely(Part part) { + return part.getText().equals(text); + } + + @Override + public void describeTo(Description description) { + description.appendValue(text); + } + }; + } + + private static Part createPart(final String text) { + return new Part() { + @Override + public String getText() { + return text; + } + }; + } + +} -- 2.7.4