Cleaned up and added more tests.

This commit is contained in:
Erik C. Thauvin 2021-07-30 01:26:52 -07:00
parent af9546e3d1
commit 04e88264cd
9 changed files with 76 additions and 17 deletions

View file

@ -106,7 +106,7 @@ object Utils {
*/ */
@JvmStatic @JvmStatic
fun colorize(s: String?, color: String): String { fun colorize(s: String?, color: String): String {
return if (s.isNullOrEmpty()) { return if (s.isNullOrBlank()) {
Colors.NORMAL Colors.NORMAL
} else if (Colors.BOLD == color || Colors.REVERSE == color) { } else if (Colors.BOLD == color || Colors.REVERSE == color) {
color + s + color color + s + color

View file

@ -198,8 +198,8 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) {
data.forEach { data.forEach {
add( add(
NoticeMessage( NoticeMessage(
"${it.first}:".padEnd(pad).prependIndent(), "${it.first}:".padEnd(pad).prependIndent()
unescapeXml(quote.getString(it.second)) + unescapeXml(quote.getString(it.second))
) )
) )
} }

View file

@ -72,4 +72,8 @@ open class Message {
this.isError = isError this.isError = isError
this.isPrivate = isPrivate this.isPrivate = isPrivate
} }
override fun toString(): String {
return "Message(color='$color', isError=$isError, isNotice=$isNotice, isPrivate=$isPrivate, msg='$msg')"
}
} }

View file

@ -51,11 +51,15 @@ class PinboardUtilsTest : LocalProperties() {
val entry = EntryLink(url, "Test Example", "ErikT", "", "#mobitopia", listOf("test")) val entry = EntryLink(url, "Test Example", "ErikT", "", "#mobitopia", listOf("test"))
PinboardUtils.addPin(pinboard, ircServer, entry) PinboardUtils.addPin(pinboard, ircServer, entry)
assertTrue(validatePin(apiToken, ircServer, entry.link), "addPin") assertTrue(validatePin(apiToken, url = entry.link, entry.title, entry.nick, entry.channel), "addPin")
entry.link = "https://www.foo.com/"
entry.link = "https://www.foo.com/"
PinboardUtils.updatePin(pinboard, ircServer, url, entry) 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) PinboardUtils.deletePin(pinboard, entry)
assertFalse(validatePin(apiToken, url = entry.link), "deletePin") 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())) 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( val response = Utils.urlReader(
URL( URL(
"https://api.pinboard.in/v1/posts/get?auth_token=${apiToken}&tag=test&" "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)
} }
} }

View file

@ -58,6 +58,8 @@ class TellMessageTest {
assertThat(tellMessage.isMatch(sender)).describedAs("match sender").isTrue assertThat(tellMessage.isMatch(sender)).describedAs("match sender").isTrue
assertThat(tellMessage.isMatch(recipient)).describedAs("match recipient").isTrue assertThat(tellMessage.isMatch(recipient)).describedAs("match recipient").isTrue
assertThat(tellMessage.isMatch("foo")).describedAs("foo is no match").isFalse 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 tellMessage.isReceived = true
assertThat(tellMessage.isReceived).describedAs("is received").isTrue assertThat(tellMessage.isReceived).describedAs("is received").isTrue
assertThat(isValidDate(tellMessage.receptionDate)).describedAs("received is valid date/time").isTrue assertThat(isValidDate(tellMessage.receptionDate)).describedAs("received is valid date/time").isTrue

View file

@ -32,9 +32,11 @@
package net.thauvin.erik.mobibot.entries package net.thauvin.erik.mobibot.entries
import com.rometools.rome.feed.synd.SyndCategory import com.rometools.rome.feed.synd.SyndCategory
import com.rometools.rome.feed.synd.SyndCategoryImpl
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.Test import org.testng.annotations.Test
import java.security.SecureRandom import java.security.SecureRandom
import java.util.Date
/** /**
* The `EntryUtilsTest` class. * The `EntryUtilsTest` class.
@ -74,6 +76,29 @@ class EntryLinkTest {
assertThat(entryLink.getComment(0).comment).describedAs("getComment(something)").isEqualTo("something") 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 @Test
fun testTags() { fun testTags() {
val tags: List<SyndCategory> = entryLink.tags val tags: List<SyndCategory> = entryLink.tags
@ -88,5 +113,10 @@ class EntryLinkTest {
entryLink.setTags("-mobitopia") entryLink.setTags("-mobitopia")
assertThat(entryLink.pinboardTags).describedAs("getPinboardTags()") assertThat(entryLink.pinboardTags).describedAs("getPinboardTags()")
.isEqualTo(entryLink.nick + ",tag1,tag2,tag3,tag4,mobitopia") .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)
} }
} }

View file

@ -31,7 +31,7 @@
*/ */
package net.thauvin.erik.mobibot.modules 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.DataProvider
import org.testng.annotations.Test import org.testng.annotations.Test
import java.io.IOException import java.io.IOException
@ -57,19 +57,30 @@ class ModuleExceptionTest {
@Test(dataProvider = "dp") @Test(dataProvider = "dp")
fun testGetDebugMessage(e: ModuleException) { 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") @Test(dataProvider = "dp")
fun testGetMessage(e: ModuleException) { fun testGetMessage(e: ModuleException) {
Assertions.assertThat(e.message).describedAs("get message").isEqualTo(message) assertThat(e.message).describedAs("get message").isEqualTo(message)
} }
@Test @Test
fun testGetSanitizedMessage() { fun testGetSanitizedMessage() {
val apiKey = "1234567890" val apiKey = "1234567890"
val e = ModuleException(debugMessage, message, IOException("URL http://foo.com?apiKey=$apiKey&userID=me")) var e = ModuleException(debugMessage, message, IOException("URL http://foo.com?apiKey=$apiKey&userID=me"))
Assertions.assertThat(e.getSanitizedMessage(apiKey)).describedAs("sanitized url") assertThat(e.getSanitizedMessage(apiKey)).describedAs("sanitized url")
.contains("xxxxxxxxxx").doesNotContain(apiKey) .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)
} }
} }

View file

@ -49,7 +49,9 @@ class StockQuoteTest : LocalProperties() {
val messages = getQuote("apple inc", apiKey) val messages = getQuote("apple inc", apiKey)
assertThat(messages).describedAs("response not empty").isNotEmpty assertThat(messages).describedAs("response not empty").isNotEmpty
assertThat(messages[0].msg).describedAs("same stock symbol").startsWith("Symbol: AAPL") 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 { try {
getQuote("blahfoo", apiKey) getQuote("blahfoo", apiKey)
} catch (e: ModuleException) { } catch (e: ModuleException) {

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle #Generated by the Semver Plugin for Gradle
#Thu Jul 08 12:26:26 PDT 2021 #Fri Jul 30 02:43:43 PDT 2021
version.buildmeta=930 version.buildmeta=1111
version.major=0 version.major=0
version.minor=8 version.minor=8
version.patch=0 version.patch=0
version.prerelease=beta version.prerelease=beta
version.project=mobibot version.project=mobibot
version.semver=0.8.0-beta+930 version.semver=0.8.0-beta+1111