From: David ‘Bombe’ Roden Date: Wed, 13 Nov 2013 05:48:18 +0000 (+0100) Subject: Add unit test for L10nFilter. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=9311f0c20d982557e00ad35054e337525591b828;p=Sone.git Add unit test for L10nFilter. --- diff --git a/src/test/java/net/pterodactylus/sone/freenet/L10nFilterTest.java b/src/test/java/net/pterodactylus/sone/freenet/L10nFilterTest.java new file mode 100644 index 0000000..a020a84 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/freenet/L10nFilterTest.java @@ -0,0 +1,83 @@ +/* + * Sone - L10nFilterTest.java - Copyright © 2013 David 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.freenet; + +import static freenet.l10n.BaseL10n.LANGUAGE.ENGLISH; +import static freenet.l10n.BaseL10n.LANGUAGE.GERMAN; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.Map; + +import net.pterodactylus.sone.web.WebInterface; + +import freenet.l10n.BaseL10n; +import freenet.l10n.BaseL10n.LANGUAGE; + +import com.google.common.collect.ImmutableMap; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link L10nFilter}. + * + * @author David ‘Bombe’ Roden + */ +public class L10nFilterTest { + + private final BaseL10n l10n = mock(BaseL10n.class); + private final WebInterface webInterface = mock(WebInterface.class); + private final L10nFilter l10nFilter = new L10nFilter(webInterface); + + @Before + public void setup() { + when(webInterface.getL10n()).thenReturn(l10n); + when(l10n.getString("SingleString")).thenReturn("Translated Value"); + when(l10n.getString("MultiString")).thenReturn("{0,number}, {1}"); + } + + @Test + public void formatStringWithoutParameters() { + String translatedValue = l10nFilter.format(null, "SingleString", Collections.emptyMap()); + assertThat(translatedValue, is("Translated Value")); + } + + @Test + public void formatStringWithParametersInEnglish() { + setLanguage(ENGLISH); + Map parameters = ImmutableMap.of("0", 17.123, "1", "Value C"); + String translatedValue = l10nFilter.format(null, "MultiString", parameters); + assertThat(translatedValue, is("17.123, Value C")); + } + + @Test + public void formatStringWithParametersInGerman() { + setLanguage(GERMAN); + Map parameters = ImmutableMap.of("0", 17.123, "1", "Value C"); + String translatedValue = l10nFilter.format(null, "MultiString", parameters); + assertThat(translatedValue, is("17,123, Value C")); + } + + private void setLanguage(LANGUAGE language) { + when(l10n.getSelectedLanguage()).thenReturn(language); + } + +}