From 4c5ec477a0f12a4cee0143636fe015e83eec5060 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 29 Nov 2019 16:19:13 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Add=20render()=20method=20for=20rend?= =?utf8?q?erables?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/utils/Renderables.kt | 27 ++++++++++++++++ .../sone/web/ajax/GetNotificationsAjaxPage.kt | 5 +-- .../pterodactylus/sone/utils/RenderablesTest.kt | 36 ++++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/utils/Renderables.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/utils/RenderablesTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Renderables.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Renderables.kt new file mode 100644 index 0000000..d641bfc --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Renderables.kt @@ -0,0 +1,27 @@ +/** + * Sone - Renderables.kt - Copyright © 2019 David ‘Bombe’ 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.utils + +import net.pterodactylus.util.io.* +import java.io.* + +/** + * Renders the [Renderable] into a [String]. + */ +fun Renderable.render() = + StringWriter().use { it.also(::render) }.toString() diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.kt index db6e5c5..66c8ed2 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.kt @@ -3,8 +3,7 @@ package net.pterodactylus.sone.web.ajax import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.data.SoneOptions import net.pterodactylus.sone.main.SonePlugin -import net.pterodactylus.sone.utils.jsonArray -import net.pterodactylus.sone.utils.jsonObject +import net.pterodactylus.sone.utils.* import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.notify.Notification @@ -74,5 +73,3 @@ private val SoneOptions?.asJsonObject "ShowNotification/NewReplies" to options.isShowNewReplyNotifications ) } ?: jsonObject {} - -private fun Notification.render() = StringWriter().use { it.also { render(it) } }.toString() diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/RenderablesTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/RenderablesTest.kt new file mode 100644 index 0000000..d921270 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/utils/RenderablesTest.kt @@ -0,0 +1,36 @@ +/** + * Sone - RenderablesTest.kt - Copyright © 2019 David ‘Bombe’ 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.utils + +import net.pterodactylus.util.io.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import kotlin.test.* + +/** + * Unit tests for tools in `Renderables.kt`. + */ +class RenderablesTest { + + @Test + fun `render method renders notification`() { + val notification = Renderable { writer -> writer.use { it.append("Test!\n") } } + assertThat(notification.render(), equalTo("Test!\n")) + } + +} -- 2.7.4