From: David ‘Bombe’ Roden Date: Fri, 29 Nov 2019 15:19:13 +0000 (+0100) Subject: ✨ Add render() method for renderables X-Git-Tag: v81^2~15^2~8 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=4c5ec477a0f12a4cee0143636fe015e83eec5060;p=Sone.git ✨ Add render() method for renderables --- 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")) + } + +}