From: David ‘Bombe’ Roden Date: Fri, 18 Nov 2016 19:12:10 +0000 (+0100) Subject: Add unit test for l10n filter X-Git-Tag: 0.9.7^2~424 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2972af51b28df3f46cea33e9ed3fe22dd1d5461d;p=Sone.git Add unit test for l10n filter --- diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt new file mode 100644 index 0000000..f070df2 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/L10nFilterTest.kt @@ -0,0 +1,49 @@ +package net.pterodactylus.sone.freenet + +import freenet.l10n.BaseL10n +import freenet.l10n.BaseL10n.LANGUAGE.ENGLISH +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.util.template.TemplateContext +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Before +import org.junit.Test +import org.mockito.ArgumentMatchers.anyString + +/** + * Unit test for [L10nFilter]. + */ +class L10nFilterTest { + + private val webInterface = mock() + private val filter = L10nFilter(webInterface) + private val templateContext = mock() + private val l10n = mock() + private val translations = mutableMapOf() + + @Before + fun setupWebInterface() { + whenever(webInterface.l10n).thenReturn(l10n) + } + + @Before + fun setupL10n() { + whenever(l10n.selectedLanguage).thenReturn(ENGLISH) + whenever(l10n.getString(anyString())).then { translations[it.arguments[0]] } + } + + @Test + fun `translation without parameters returns translated string`() { + translations["data"] = "translated data" + assertThat(filter.format(templateContext, "data", emptyMap()), equalTo("translated data")) + } + + @Test + fun `translation with parameters returned translated string`() { + translations["data"] = "translated {0,number} {1}" + assertThat(filter.format(templateContext, "data", mapOf("0" to 4.5, "1" to "data")), equalTo("translated 4.5 data")) + } + +}