From db8c77f40b82bf516255318f20a824d676b4fa53 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 19 Jun 2017 10:27:18 +0200 Subject: [PATCH] Convert search page test to use new web page test base --- .../kotlin/net/pterodactylus/sone/utils/Objects.kt | 3 +++ .../net/pterodactylus/sone/utils/ObjectsTest.kt | 23 ++++++++++++++++++ .../pterodactylus/sone/web/pages/SearchPageTest.kt | 9 ++++--- .../pterodactylus/sone/web/pages/WebPageTest2.kt | 28 +++++++++++++++++++++- 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/utils/Objects.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Objects.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Objects.kt new file mode 100644 index 0000000..36e530c --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Objects.kt @@ -0,0 +1,3 @@ +package net.pterodactylus.sone.utils + +fun T?.asList() = this?.let(::listOf) ?: emptyList() diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt new file mode 100644 index 0000000..1c2d7f5 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/utils/ObjectsTest.kt @@ -0,0 +1,23 @@ +package net.pterodactylus.sone.utils + +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.hamcrest.Matchers.empty +import org.junit.Test + +/** + * Unit test for Object utils. + */ +class ObjectsTest { + + @Test + fun `non-null value is turned into a list with one element`() { + assertThat(5.asList(), contains(5)) + } + + @Test + fun `null value is turned into empty list`() { + assertThat(null.asList(), empty()) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt index 540b8b5..7fab8d6 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt @@ -22,12 +22,11 @@ import java.util.concurrent.atomic.AtomicInteger /** * Unit test for [SearchPage]. */ -class SearchPageTest : WebPageTest() { +class SearchPageTest: WebPageTest2({ template, webInterface -> SearchPage(template, webInterface, ticker) }) { - private val ticker = mock() - private val page = SearchPage(template, webInterface, ticker) - - override fun getPage() = page + companion object { + val ticker = mock() + } @Test fun `page returns correct path`() { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt index 6c4372d..9e57539 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt @@ -4,12 +4,16 @@ import com.google.common.eventbus.EventBus import freenet.clients.http.ToadletContext import freenet.support.api.HTTPRequest import net.pterodactylus.sone.core.Preferences +import net.pterodactylus.sone.data.Album +import net.pterodactylus.sone.data.Image import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.test.deepMock import net.pterodactylus.sone.test.get import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asList import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest @@ -54,6 +58,10 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla private val allSones = mutableMapOf() private val localSones = mutableMapOf() private val allPosts = mutableMapOf() + private val allPostReplies = mutableMapOf() + private val perPostReplies = mutableMapOf() + private val allAlbums = mutableMapOf() + private val allImages = mutableMapOf() private val translations = mutableMapOf() @Before @@ -64,6 +72,11 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla whenever(core.localSones).then { localSones.values } whenever(core.getLocalSone(anyString())).then { localSones[it[0]] } whenever(core.getPost(anyString())).then { allPosts[it[0]].asOptional() } + whenever(core.getPostReply(anyString())).then { allPostReplies[it[0]].asOptional() } + whenever(core.getReplies(anyString())).then { perPostReplies[it[0]].asList() } + whenever(core.getAlbum(anyString())).then { allAlbums[it[0]] } + whenever(core.getImage(anyString())).then { allImages[it[0]]} + whenever(core.getImage(anyString(), anyBoolean())).then { allImages[it[0]]} } @Before @@ -145,6 +158,19 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla allPosts[id] = post } + fun addPostReply(id: String, postReply: PostReply) { + allPostReplies[id] = postReply + postReply.postId?.also { perPostReplies[it] = postReply } + } + + fun addAlbum(id: String, album: Album) { + allAlbums[id] = album + } + + fun addImage(id: String, image: Image) { + allImages[id] = image + } + fun addTranslation(key: String, value: String) { translations[key] = value } @@ -159,7 +185,7 @@ abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTempla caughtException?.run { throw this } ?: assertions() } - fun verifyRedirect(target: String, assertions: () -> Unit) { + fun verifyRedirect(target: String, assertions: () -> Unit = {}) { try { page.handleRequest(freenetRequest, templateContext) fail() -- 2.7.4