From: David ‘Bombe’ Roden Date: Sun, 17 Nov 2019 20:18:07 +0000 (+0100) Subject: 🎨 Replace list notification test with Kotlin version X-Git-Tag: v81^2~49 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e399e8596825ef7480f9803166f0b41b0cad3391;p=Sone.git 🎨 Replace list notification test with Kotlin version --- diff --git a/src/test/java/net/pterodactylus/sone/notify/ListNotificationTest.java b/src/test/java/net/pterodactylus/sone/notify/ListNotificationTest.java deleted file mode 100644 index 5e40f81..0000000 --- a/src/test/java/net/pterodactylus/sone/notify/ListNotificationTest.java +++ /dev/null @@ -1,120 +0,0 @@ -package net.pterodactylus.sone.notify; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.emptyIterable; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.hamcrest.MockitoHamcrest.argThat; - -import java.util.Arrays; - -import net.pterodactylus.util.notify.NotificationListener; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateContext; - -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Unit test for {@link ListNotification}. - */ -public class ListNotificationTest { - - private static final String ID = "notification-id"; - private static final String KEY = "element-key"; - private static final String OTHER_KEY = "other-key"; - - private final Template template = mock(Template.class); - private final TemplateContext templateInitialContext = mock(TemplateContext.class); - private ListNotification listNotification; - - public ListNotificationTest() { - when(template.getInitialContext()).thenReturn(templateInitialContext); - listNotification = new ListNotification<>(ID, KEY, template); - } - - @Test - public void creatingAListNotificationSetsEmptyIterableOnElementKeyInTemplateContext() { - verify(templateInitialContext).set(eq(KEY), argThat(emptyIterable())); - } - - @Test - public void newListNotificationHasNoElement() { - assertThat(listNotification.getElements(), emptyIterable()); - } - - @Test - public void newListNotificationIsEmpty() { - assertThat(listNotification.isEmpty(), is(true)); - } - - @Test - public void listNotificationRetainsSetElements() { - listNotification.setElements(Arrays.asList("a", "b", "c")); - assertThat(listNotification.getElements(), Matchers.contains("a", "b", "c")); - } - - @Test - public void listNotificationRetainsAddedElements() { - listNotification.add("a"); - listNotification.add("b"); - listNotification.add("c"); - assertThat(listNotification.getElements(), Matchers.contains("a", "b", "c")); - } - - @Test - public void listNotificationRemovesCorrectElement() { - listNotification.setElements(Arrays.asList("a", "b", "c")); - listNotification.remove("b"); - assertThat(listNotification.getElements(), Matchers.contains("a", "c")); - } - - @Test - public void removingTheLastElementDismissesTheNotification() { - NotificationListener notificationListener = mock(NotificationListener.class); - listNotification.addNotificationListener(notificationListener); - listNotification.add("a"); - listNotification.remove("a"); - verify(notificationListener).notificationDismissed(listNotification); - } - - @Test - public void dismissingTheListNotificationRemovesAllElements() { - listNotification.setElements(Arrays.asList("a", "b", "c")); - listNotification.dismiss(); - assertThat(listNotification.getElements(), emptyIterable()); - } - - @Test - public void listNotificationWithDifferentElementsIsNotEqual() { - ListNotification secondNotification = new ListNotification(ID, KEY, template); - listNotification.add("a"); - secondNotification.add("b"); - assertThat(listNotification, not(is(secondNotification))); - } - - @Test - public void listNotificationWithDifferentKeyIsNotEqual() { - ListNotification secondNotification = new ListNotification(ID, OTHER_KEY, template); - assertThat(listNotification, not(is(secondNotification))); - } - - @Test - public void copiedNotificationsHaveTheSameHashCode() { - ListNotification secondNotification = new ListNotification(listNotification); - listNotification.add("a"); - secondNotification.add("a"); - listNotification.setLastUpdateTime(secondNotification.getLastUpdatedTime()); - assertThat(listNotification.hashCode(), is(secondNotification.hashCode())); - } - - @Test - public void listNotificationIsNotEqualToOtherObjects() { - assertThat(listNotification, not(is(new Object()))); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationTest.kt new file mode 100644 index 0000000..08d4829 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/notify/ListNotificationTest.kt @@ -0,0 +1,103 @@ +package net.pterodactylus.sone.notify + +import net.pterodactylus.util.notify.* +import net.pterodactylus.util.template.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import java.util.concurrent.atomic.* + +/** + * Unit test for [ListNotification]. + */ +class ListNotificationTest { + + private val template = Template() + private val listNotification = ListNotification(ID, KEY, template) + + @Test + fun `creating a list notification sets empty iterable on element key in template context`() { + assertThat(template.initialContext.get(KEY) as Iterable<*>, emptyIterable()) + } + + @Test + fun `new list notification has no element`() { + assertThat(listNotification.elements, emptyIterable()) + } + + @Test + fun `new list notification is empty`() { + assertThat(listNotification.isEmpty, equalTo(true)) + } + + @Test + fun `list notification retains set elements`() { + listNotification.setElements(listOf("a", "b", "c")) + assertThat(listNotification.elements, contains("a", "b", "c")) + } + + @Test + fun `list notification retains added elements`() { + listNotification.add("a") + listNotification.add("b") + listNotification.add("c") + assertThat(listNotification.elements, contains("a", "b", "c")) + } + + @Test + fun `list notification removes correct element`() { + listNotification.setElements(listOf("a", "b", "c")) + listNotification.remove("b") + assertThat(listNotification.elements, contains("a", "c")) + } + + @Test + fun `removing the last element dismisses the notification`() { + val notificationDismissed = AtomicBoolean() + val notificationListener = NotificationListener { notificationDismissed.set(it == listNotification) } + listNotification.addNotificationListener(notificationListener) + listNotification.add("a") + listNotification.remove("a") + assertThat(notificationDismissed.get(), equalTo(true)) + } + + @Test + fun `dismissing the list notification removes all elements`() { + listNotification.setElements(listOf("a", "b", "c")) + listNotification.dismiss() + assertThat(listNotification.elements, emptyIterable()) + } + + @Test + fun `list notification with different elements is not equal`() { + val secondNotification = ListNotification(ID, KEY, template) + listNotification.add("a") + secondNotification.add("b") + assertThat(listNotification, not(equalTo(secondNotification))) + } + + @Test + fun `list notification with different key is not equal`() { + val secondNotification = ListNotification(ID, OTHER_KEY, template) + assertThat(listNotification, not(equalTo(secondNotification))) + } + + @Test + fun `copied notifications have the same hash code`() { + val secondNotification = ListNotification(listNotification) + listNotification.add("a") + secondNotification.add("a") + listNotification.setLastUpdateTime(secondNotification.lastUpdatedTime) + assertThat(listNotification.hashCode(), equalTo(secondNotification.hashCode())) + } + + @Test + fun `list notification is not equal to other objects`() { + assertThat(listNotification, not(equalTo(Any()))) + } + +} + +private const val ID = "notification-id" +private const val KEY = "element-key" +private const val OTHER_KEY = "other-key"