From aab28b59792d5863268d560b368d0368726bacc2 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 31 Jul 2021 14:23:43 -0700 Subject: [PATCH] Added capitalizeWords extension function. --- src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt | 6 ++++++ .../kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt | 8 +++++++- src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt | 9 +++++++++ .../net/thauvin/erik/mobibot/modules/Weather2Test.kt | 8 ++++---- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt index e671380..986230a 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt @@ -101,6 +101,12 @@ object Utils { @JvmStatic fun String.capitalise(): String = this.replaceFirstChar { it.uppercase() } + /** + * Capitalize words + */ + fun String.capitalizeWords(): String = split(" ").map { it.lowercase().capitalise() }.joinToString(" ") + + /** * Colorize a string. */ diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt index e3162f4..ef6cb1f 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/Weather2.kt @@ -38,6 +38,7 @@ import net.aksingh.owmjapis.model.CurrentWeather import net.thauvin.erik.mobibot.Mobibot import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.capitalise +import net.thauvin.erik.mobibot.Utils.capitalizeWords import net.thauvin.erik.mobibot.Utils.encodeUrl import net.thauvin.erik.mobibot.Utils.helpFormat import net.thauvin.erik.mobibot.msg.ErrorMessage @@ -135,7 +136,12 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) { owm.currentWeatherByCityName(city, country) } if (cwd.hasCityName()) { - messages.add(PublicMessage("City: ${cwd.cityName} [${country.value}]")) + messages.add( + PublicMessage( + "City: ${cwd.cityName}, " + + country.name.replace('_', ' ').capitalizeWords() + " [${country.value}]" + ) + ) with(cwd.mainData) { if (this != null) { if (hasTemp()) { diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt index 905b32f..16434f2 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/UtilsTest.kt @@ -35,6 +35,7 @@ import net.thauvin.erik.mobibot.Utils.appendIfMissing import net.thauvin.erik.mobibot.Utils.bold import net.thauvin.erik.mobibot.Utils.buildCmdSyntax import net.thauvin.erik.mobibot.Utils.capitalise +import net.thauvin.erik.mobibot.Utils.capitalizeWords import net.thauvin.erik.mobibot.Utils.colorize import net.thauvin.erik.mobibot.Utils.cyan import net.thauvin.erik.mobibot.Utils.encodeUrl @@ -117,6 +118,14 @@ class UtilsTest { assertThat("".capitalise()).describedAs("capitalize()").isEqualTo("") } + @Test + fun textCapitaliseWords() { + assertThat(test.capitalizeWords()).describedAs("captiatlizeWords(test)").isEqualTo("This Is A Test.") + assertThat("Already Capitalized".capitalizeWords()).describedAs("already capitalized") + .isEqualTo("Already Capitalized") + assertThat(" a test ".capitalizeWords()).describedAs("with spaces").isEqualTo(" A Test ") + } + @Test fun testColorize() { assertThat(colorize(ascii, Colors.REVERSE)).describedAs("colorize(reverse)").isEqualTo( diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/Weather2Test.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/Weather2Test.kt index aead144..0aea8d2 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/Weather2Test.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/Weather2Test.kt @@ -76,16 +76,16 @@ class Weather2Test : LocalProperties() { @Throws(ModuleException::class) fun testWeather() { var messages = getWeather("98204", getProperty(OWM_API_KEY_PROP)) - assertThat(messages[0].msg).describedAs("is Everett").contains("Everett").contains("US") + assertThat(messages[0].msg).describedAs("is Everett").contains("Everett, United States").contains("US") assertThat(messages[messages.size - 1].msg).describedAs("is Everett zip code").endsWith("98204%2CUS") messages = getWeather("San Francisco", getProperty(OWM_API_KEY_PROP)) assertThat(messages[0].msg).describedAs("is San Francisco").contains("San Francisco").contains("US") assertThat(messages[messages.size - 1].msg).describedAs("is San Fran city code").endsWith("5391959") - messages = getWeather("London, UK", getProperty(OWM_API_KEY_PROP)) - assertThat(messages[0].msg).describedAs("is UK").contains("London").contains("UK") - assertThat(messages[messages.size - 1].msg).describedAs("is London city code").endsWith("4517009") + messages = getWeather("London, GB", getProperty(OWM_API_KEY_PROP)) + assertThat(messages[0].msg).describedAs("is UK").contains("London, United Kingdom").contains("GB") + assertThat(messages[messages.size - 1].msg).describedAs("is London city code").endsWith("2643743") assertThatThrownBy { getWeather("Foo, US", getProperty(OWM_API_KEY_PROP)) } .describedAs("foo not found").hasCauseInstanceOf(APIException::class.java)