Added capitalizeWords extension function.

This commit is contained in:
Erik C. Thauvin 2021-07-31 14:23:43 -07:00
parent 79efd005d1
commit aab28b5979
4 changed files with 26 additions and 5 deletions

View file

@ -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.
*/

View file

@ -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()) {

View file

@ -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(

View file

@ -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)