From 04e88264cd730ec836d8927e096bfe124ea12c50 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 30 Jul 2021 01:26:52 -0700 Subject: [PATCH] Cleaned up and added more tests. --- .../kotlin/net/thauvin/erik/mobibot/Utils.kt | 2 +- .../erik/mobibot/modules/StockQuote.kt | 4 +-- .../net/thauvin/erik/mobibot/msg/Message.kt | 4 +++ .../thauvin/erik/mobibot/PinboardUtilsTest.kt | 20 +++++++++---- .../mobibot/commands/tell/TellMessageTest.kt | 2 ++ .../erik/mobibot/entries/EntryLinkTest.kt | 30 +++++++++++++++++++ .../mobibot/modules/ModuleExceptionTest.kt | 21 +++++++++---- .../erik/mobibot/modules/StockQuoteTest.kt | 4 ++- version.properties | 6 ++-- 9 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt index a1545b1..9c05336 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/Utils.kt @@ -106,7 +106,7 @@ object Utils { */ @JvmStatic fun colorize(s: String?, color: String): String { - return if (s.isNullOrEmpty()) { + return if (s.isNullOrBlank()) { Colors.NORMAL } else if (Colors.BOLD == color || Colors.REVERSE == color) { color + s + color diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt index e00838d..333d9d4 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -198,8 +198,8 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) { data.forEach { add( NoticeMessage( - "${it.first}:".padEnd(pad).prependIndent(), - unescapeXml(quote.getString(it.second)) + "${it.first}:".padEnd(pad).prependIndent() + + unescapeXml(quote.getString(it.second)) ) ) } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/msg/Message.kt b/src/main/kotlin/net/thauvin/erik/mobibot/msg/Message.kt index 6cdf592..b62c1d9 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/msg/Message.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/msg/Message.kt @@ -72,4 +72,8 @@ open class Message { this.isError = isError this.isPrivate = isPrivate } + + override fun toString(): String { + return "Message(color='$color', isError=$isError, isNotice=$isNotice, isPrivate=$isPrivate, msg='$msg')" + } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/PinboardUtilsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/PinboardUtilsTest.kt index 7fcd726..d9c7354 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/PinboardUtilsTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/PinboardUtilsTest.kt @@ -51,11 +51,15 @@ class PinboardUtilsTest : LocalProperties() { val entry = EntryLink(url, "Test Example", "ErikT", "", "#mobitopia", listOf("test")) PinboardUtils.addPin(pinboard, ircServer, entry) - assertTrue(validatePin(apiToken, ircServer, entry.link), "addPin") - entry.link = "https://www.foo.com/" + assertTrue(validatePin(apiToken, url = entry.link, entry.title, entry.nick, entry.channel), "addPin") + entry.link = "https://www.foo.com/" PinboardUtils.updatePin(pinboard, ircServer, url, entry) - assertTrue(validatePin(apiToken, ircServer, entry.link), "updatePin") + assertTrue(validatePin(apiToken, url = entry.link, ircServer), "updatePin") + + entry.title = "Foo Title" + PinboardUtils.updatePin(pinboard, ircServer, entry.link, entry) + assertTrue(validatePin(apiToken, url = entry.link, entry.title), "update title") PinboardUtils.deletePin(pinboard, entry) assertFalse(validatePin(apiToken, url = entry.link), "deletePin") @@ -67,7 +71,7 @@ class PinboardUtilsTest : LocalProperties() { assertTrue(d.toTimestamp().matches("[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z".toRegex())) } - private fun validatePin(apiToken: String, ircServer: String = "", url: String): Boolean { + private fun validatePin(apiToken: String, url: String, vararg matches: String): Boolean { val response = Utils.urlReader( URL( "https://api.pinboard.in/v1/posts/get?auth_token=${apiToken}&tag=test&" @@ -75,6 +79,12 @@ class PinboardUtilsTest : LocalProperties() { ) ) - return response.contains(url) && response.contains(ircServer) + matches.forEach { + if (!response.contains(it)) { + return false + } + } + + return response.contains(url) } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessageTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessageTest.kt index 4fdd2db..7f5de32 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessageTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessageTest.kt @@ -58,6 +58,8 @@ class TellMessageTest { assertThat(tellMessage.isMatch(sender)).describedAs("match sender").isTrue assertThat(tellMessage.isMatch(recipient)).describedAs("match recipient").isTrue assertThat(tellMessage.isMatch("foo")).describedAs("foo is no match").isFalse + tellMessage.isReceived = false + assertThat(tellMessage.receptionDate).describedAs("reception date not set").isEqualTo(LocalDateTime.MIN) tellMessage.isReceived = true assertThat(tellMessage.isReceived).describedAs("is received").isTrue assertThat(isValidDate(tellMessage.receptionDate)).describedAs("received is valid date/time").isTrue diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt index b6a83dc..3af8f40 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt @@ -32,9 +32,11 @@ package net.thauvin.erik.mobibot.entries import com.rometools.rome.feed.synd.SyndCategory +import com.rometools.rome.feed.synd.SyndCategoryImpl import org.assertj.core.api.Assertions.assertThat import org.testng.annotations.Test import java.security.SecureRandom +import java.util.Date /** * The `EntryUtilsTest` class. @@ -74,6 +76,29 @@ class EntryLinkTest { assertThat(entryLink.getComment(0).comment).describedAs("getComment(something)").isEqualTo("something") } + @Test + fun testConstructor() { + val tag = "test" + val tags = listOf(SyndCategoryImpl().apply { name = tag }) + val link = EntryLink("link", "title", "nick", "channel", Date(), tags) + assertThat(link.tags.size).describedAs("check tag size").isEqualTo(tags.size) + assertThat(link.tags[0].name).describedAs("check tag name").isEqualTo(tag) + assertThat(link.pinboardTags).describedAs("check pinboard tag").isEqualTo("nick,$tag") + } + + @Test + fun testMatches() { + assertThat(entryLink.matches("mobitopia")).describedAs("match mobitopia").isTrue + assertThat(entryLink.matches("skynx")).describedAs("match nick").isTrue + assertThat(entryLink.matches("www.mobitopia.org")).describedAs("match url").isTrue + assertThat(entryLink.matches("foo")).describedAs("match foo").isFalse + assertThat(entryLink.matches("")).describedAs("match empty").isFalse + assertThat(entryLink.matches(null)).describedAs("match null").isFalse + + + } + + @Test fun testTags() { val tags: List = entryLink.tags @@ -88,5 +113,10 @@ class EntryLinkTest { entryLink.setTags("-mobitopia") assertThat(entryLink.pinboardTags).describedAs("getPinboardTags()") .isEqualTo(entryLink.nick + ",tag1,tag2,tag3,tag4,mobitopia") + val size = entryLink.tags.size + entryLink.setTags("") + assertThat(entryLink.tags.size).describedAs("empty tag").isEqualTo(size) + entryLink.setTags(" ") + assertThat(entryLink.tags.size).describedAs("blank tag").isEqualTo(size) } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt index c2d61e3..4e63def 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt @@ -31,7 +31,7 @@ */ package net.thauvin.erik.mobibot.modules -import org.assertj.core.api.Assertions +import org.assertj.core.api.Assertions.assertThat import org.testng.annotations.DataProvider import org.testng.annotations.Test import java.io.IOException @@ -57,19 +57,30 @@ class ModuleExceptionTest { @Test(dataProvider = "dp") fun testGetDebugMessage(e: ModuleException) { - Assertions.assertThat(e.debugMessage).describedAs("get debug message").isEqualTo(debugMessage) + assertThat(e.debugMessage).describedAs("get debug message").isEqualTo(debugMessage) } @Test(dataProvider = "dp") fun testGetMessage(e: ModuleException) { - Assertions.assertThat(e.message).describedAs("get message").isEqualTo(message) + assertThat(e.message).describedAs("get message").isEqualTo(message) } @Test fun testGetSanitizedMessage() { val apiKey = "1234567890" - val e = ModuleException(debugMessage, message, IOException("URL http://foo.com?apiKey=$apiKey&userID=me")) - Assertions.assertThat(e.getSanitizedMessage(apiKey)).describedAs("sanitized url") + var e = ModuleException(debugMessage, message, IOException("URL http://foo.com?apiKey=$apiKey&userID=me")) + assertThat(e.getSanitizedMessage(apiKey)).describedAs("sanitized url") .contains("xxxxxxxxxx").doesNotContain(apiKey) + + e = ModuleException(debugMessage, message, null) + assertThat(e.getSanitizedMessage(apiKey)).describedAs("no cause").contains(message) + + val msg: String? = null + e = ModuleException(debugMessage, msg, IOException(msg)) + assertThat(e.getSanitizedMessage(apiKey)).describedAs("no message").isEqualTo("") + + + e = ModuleException(msg, msg, IOException(apiKey)) + assertThat(e.getSanitizedMessage(apiKey)).describedAs("null message").doesNotContain(apiKey) } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt index 60875cf..7f46935 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt @@ -49,7 +49,9 @@ class StockQuoteTest : LocalProperties() { val messages = getQuote("apple inc", apiKey) assertThat(messages).describedAs("response not empty").isNotEmpty assertThat(messages[0].msg).describedAs("same stock symbol").startsWith("Symbol: AAPL") - assertThat(messages[1].msg).describedAs("price label").startsWith(" Price: ") + assertThat(messages[1].msg).describedAs("price label").startsWith("Price: ".prependIndent()) + assertThat(messages[2].msg).describedAs("previous label").startsWith("Previous: ".prependIndent()) + assertThat(messages[3].msg).describedAs("open label").startsWith("Open: ".prependIndent()) try { getQuote("blahfoo", apiKey) } catch (e: ModuleException) { diff --git a/version.properties b/version.properties index a78c1d5..6cb4e53 100644 --- a/version.properties +++ b/version.properties @@ -1,9 +1,9 @@ #Generated by the Semver Plugin for Gradle -#Thu Jul 08 12:26:26 PDT 2021 -version.buildmeta=930 +#Fri Jul 30 02:43:43 PDT 2021 +version.buildmeta=1111 version.major=0 version.minor=8 version.patch=0 version.prerelease=beta version.project=mobibot -version.semver=0.8.0-beta+930 +version.semver=0.8.0-beta+1111