Cleaned up tests

This commit is contained in:
Erik C. Thauvin 2022-10-03 10:59:44 -07:00
parent ed971a4e7a
commit 7d54bfe756
27 changed files with 198 additions and 158 deletions

View file

@ -35,6 +35,7 @@ package net.thauvin.erik.mobibot
import assertk.assertThat import assertk.assertThat
import assertk.assertions.containsExactly import assertk.assertions.containsExactly
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.size
import net.thauvin.erik.mobibot.commands.ChannelFeed import net.thauvin.erik.mobibot.commands.ChannelFeed
import net.thauvin.erik.mobibot.commands.Cycle import net.thauvin.erik.mobibot.commands.Cycle
import net.thauvin.erik.mobibot.commands.Die import net.thauvin.erik.mobibot.commands.Die
@ -66,8 +67,8 @@ class AddonsTest {
addons.add(War()) addons.add(War())
addons.add(Dice()) addons.add(Dice())
addons.add(Lookup()) addons.add(Lookup())
assertThat(addons.modules.size, "modules = 2").isEqualTo(2) assertThat(addons.modules, "modules").size().isEqualTo(2)
assertThat(addons.names.modules, "module names").containsExactly("Joke", "RockPaperScissors") assertThat(addons.names.modules, "names.modules").containsExactly("Joke", "RockPaperScissors")
// Commands // Commands
addons.add(View()) addons.add(View())
@ -77,11 +78,11 @@ class AddonsTest {
addons.add(ChannelFeed("channel")) // no properties, disabled addons.add(ChannelFeed("channel")) // no properties, disabled
p[Ignore.IGNORE_PROP] = "nick" p[Ignore.IGNORE_PROP] = "nick"
addons.add(Ignore()) addons.add(Ignore())
assertThat(addons.commands.size, "commands = 3").isEqualTo(3) assertThat(addons.commands, "command").size().isEqualTo(3)
assertThat(addons.names.ops, "ops names").containsExactly("cycle") assertThat(addons.names.ops, "names.ops").containsExactly("cycle")
assertThat(addons.names.commands, "command names").containsExactly( assertThat(addons.names.commands, "names.command").containsExactly(
"joke", "joke",
"rock", "rock",
"paper", "paper",

View file

@ -33,11 +33,15 @@ package net.thauvin.erik.mobibot
import assertk.assertThat import assertk.assertThat
import assertk.assertions.contains import assertk.assertions.contains
import assertk.assertions.index
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.isFailure import assertk.assertions.isFailure
import assertk.assertions.isInstanceOf import assertk.assertions.isInstanceOf
import assertk.assertions.prop
import assertk.assertions.size
import com.rometools.rome.io.FeedException import com.rometools.rome.io.FeedException
import net.thauvin.erik.mobibot.FeedReader.Companion.readFeed import net.thauvin.erik.mobibot.FeedReader.Companion.readFeed
import net.thauvin.erik.mobibot.msg.Message
import org.testng.annotations.Test import org.testng.annotations.Test
import java.io.IOException import java.io.IOException
import java.net.MalformedURLException import java.net.MalformedURLException
@ -50,15 +54,15 @@ class FeedReaderTest {
@Test @Test
fun readFeedTest() { fun readFeedTest() {
var messages = readFeed("https://feeds.thauvin.net/ethauvin") var messages = readFeed("https://feeds.thauvin.net/ethauvin")
assertThat(messages.size, "size = 10").isEqualTo(10) assertThat(messages, "messages").size().isEqualTo(10)
assertThat(messages[1].msg, "feed entry url").contains("erik.thauvin.net") assertThat(messages, "messages").index(1).prop(Message::msg).contains("erik.thauvin.net")
messages = readFeed("https://lorem-rss.herokuapp.com/feed?length=0") messages = readFeed("https://lorem-rss.herokuapp.com/feed?length=0")
assertThat(messages[0].msg, "nothing to view").contains("nothing") assertThat(messages, "messages").index(0).prop(Message::msg).contains("nothing")
messages = readFeed("https://lorem-rss.herokuapp.com/feed?length=42", 42) messages = readFeed("https://lorem-rss.herokuapp.com/feed?length=42", 42)
assertThat(messages.size, "messages = 84").isEqualTo(84) assertThat(messages, "messages").size().isEqualTo(84)
assertThat(messages.last().msg, "example entry url").contains("http://example.com/test/") assertThat(messages.last(), "messages.last").prop(Message::msg).contains("http://example.com/test/")
assertThat { readFeed("blah") }.isFailure().isInstanceOf(MalformedURLException::class.java) assertThat { readFeed("blah") }.isFailure().isInstanceOf(MalformedURLException::class.java)

View file

@ -53,18 +53,18 @@ class PinboardTest : LocalProperties() {
pinboard.setApiToken(apiToken) pinboard.setApiToken(apiToken)
pinboard.addPin(ircServer, entry) pinboard.addPin(ircServer, entry)
assertTrue(validatePin(apiToken, url = entry.link, entry.title, entry.nick, entry.channel), "validate add") assertTrue(validatePin(apiToken, url = entry.link, entry.title, entry.nick, entry.channel), "addPin")
entry.link = "https://www.example.com/${(5001..9999).random()}" entry.link = "https://www.example.com/${(5001..9999).random()}"
pinboard.updatePin(ircServer, url, entry) pinboard.updatePin(ircServer, url, entry)
assertTrue(validatePin(apiToken, url = entry.link, ircServer), "validate update") assertTrue(validatePin(apiToken, url = entry.link, ircServer), "updatePin")
entry.title = "Foo Title" entry.title = "Foo Title"
pinboard.updatePin(ircServer, entry.link, entry) pinboard.updatePin(ircServer, entry.link, entry)
assertTrue(validatePin(apiToken, url = entry.link, entry.title), "validate title") assertTrue(validatePin(apiToken, url = entry.link, entry.title), "updatePin(${entry.title}")
pinboard.deletePin(entry) pinboard.deletePin(entry)
assertFalse(validatePin(apiToken, url = entry.link), "validate delete") assertFalse(validatePin(apiToken, url = entry.link), "deletePin")
} }
private fun validatePin(apiToken: String, url: String, vararg matches: String): Boolean { private fun validatePin(apiToken: String, url: String, vararg matches: String): Boolean {

View file

@ -98,8 +98,8 @@ class UtilsTest {
@Test @Test
fun testBold() { fun testBold() {
assertThat(1.bold(), "1.bold()").isEqualTo(Colors.BOLD + "1" + Colors.BOLD) assertThat(1.bold(), "bold(1)").isEqualTo(Colors.BOLD + "1" + Colors.BOLD)
assertThat(2L.bold(), "1.bold()").isEqualTo(Colors.BOLD + "2" + Colors.BOLD) assertThat(2L.bold(), "bold(2L)").isEqualTo(Colors.BOLD + "2" + Colors.BOLD)
assertThat(ascii.bold(), "ascii.bold()").isEqualTo(Colors.BOLD + ascii + Colors.BOLD) assertThat(ascii.bold(), "ascii.bold()").isEqualTo(Colors.BOLD + ascii + Colors.BOLD)
assertThat("test".bold(), "test.bold()").isEqualTo(Colors.BOLD + "test" + Colors.BOLD) assertThat("test".bold(), "test.bold()").isEqualTo(Colors.BOLD + "test" + Colors.BOLD)
} }
@ -132,7 +132,7 @@ class UtilsTest {
.isEqualTo(Colors.BOLD + ascii + Colors.BOLD) .isEqualTo(Colors.BOLD + ascii + Colors.BOLD)
assertThat(null.colorize(Colors.RED), "null.colorize()").isEqualTo("") assertThat(null.colorize(Colors.RED), "null.colorize()").isEqualTo("")
assertThat("".colorize(Colors.RED), "colorize()").isEqualTo("") assertThat("".colorize(Colors.RED), "colorize()").isEqualTo("")
assertThat(ascii.colorize(DEFAULT_COLOR), "none.colorize()").isEqualTo(ascii) assertThat(ascii.colorize(DEFAULT_COLOR), "ascii.colorize()").isEqualTo(ascii)
assertThat(" ".colorize(Colors.NORMAL), "blank.colorize()") assertThat(" ".colorize(Colors.NORMAL), "blank.colorize()")
.isEqualTo(Colors.NORMAL + " " + Colors.NORMAL) .isEqualTo(Colors.NORMAL + " " + Colors.NORMAL)
} }
@ -165,19 +165,19 @@ class UtilsTest {
@Test @Test
fun testHelpCmdSyntax() { fun testHelpCmdSyntax() {
val bot = "mobibot" val bot = "mobibot"
assertThat(helpCmdSyntax("%c $test %n $test", bot, false), "public") assertThat(helpCmdSyntax("%c $test %n $test", bot, false), "helpCmdSyntax(private)")
.isEqualTo("$bot: $test $bot $test") .isEqualTo("$bot: $test $bot $test")
assertThat(helpCmdSyntax("%c %n $test %c $test %n", bot, true), "public") assertThat(helpCmdSyntax("%c %n $test %c $test %n", bot, true), "helpCmdSyntax(public)")
.isEqualTo("/msg $bot $bot $test /msg $bot $test $bot") .isEqualTo("/msg $bot $bot $test /msg $bot $test $bot")
} }
@Test @Test
fun testHelpFormat() { fun testHelpFormat() {
assertThat(helpFormat(test, isBold = true, isIndent = false), "bold") assertThat(helpFormat(test, isBold = true, isIndent = false), "helpFormat(bold)")
.isEqualTo("${Colors.BOLD}$test${Colors.BOLD}") .isEqualTo("${Colors.BOLD}$test${Colors.BOLD}")
assertThat(helpFormat(test, isBold = false, isIndent = true), "indent") assertThat(helpFormat(test, isBold = false, isIndent = true), "helpFormat(indent)")
.isEqualTo(test.prependIndent()) .isEqualTo(test.prependIndent())
assertThat(helpFormat(test, isBold = true, isIndent = true), "bold-indent") assertThat(helpFormat(test, isBold = true, isIndent = true), "helpFormat(bold,indent)")
.isEqualTo(test.colorize(Colors.BOLD).prependIndent()) .isEqualTo(test.colorize(Colors.BOLD).prependIndent())
} }
@ -191,9 +191,9 @@ class UtilsTest {
@Test @Test
fun testLastOrEmpty() { fun testLastOrEmpty() {
val two = listOf("1", "2") val two = listOf("1", "2")
assertThat(two.lastOrEmpty(), "two").isEqualTo("2") assertThat(two.lastOrEmpty(), "lastOrEmpty(1,2)").isEqualTo("2")
val one = listOf("1") val one = listOf("1")
assertThat(one.lastOrEmpty(), "one").isEqualTo("") assertThat(one.lastOrEmpty(), "lastOrEmpty(1)").isEqualTo("")
} }
@Test @Test

View file

@ -40,14 +40,20 @@ import org.testng.annotations.Test
class InfoTest { class InfoTest {
@Test(groups = ["commands"]) @Test(groups = ["commands"])
fun testToUptime() { fun testToUptime() {
assertThat(547800300076L.toUptime(), "full").isEqualTo("17 years 2 months 2 weeks 1 day 6 hours 45 minutes") assertThat(
assertThat(24300000L.toUptime(), "hours minutes").isEqualTo("6 hours 45 minutes") 547800300076L.toUptime(),
assertThat(110700000L.toUptime(), "days hours minutes").isEqualTo("1 day 6 hours 45 minutes") "upTime(full)"
assertThat(1320300000L.toUptime(), "weeks days hours minutes").isEqualTo("2 weeks 1 day 6 hours 45 minutes") ).isEqualTo("17 years 2 months 2 weeks 1 day 6 hours 45 minutes")
assertThat(2700000L.toUptime(), "45 minutes").isEqualTo("45 minutes") assertThat(24300000L.toUptime(), "upTime(hours minutes)").isEqualTo("6 hours 45 minutes")
assertThat(60000L.toUptime(), "1 minute").isEqualTo("1 minute") assertThat(110700000L.toUptime(), "upTime(days hours minutes)").isEqualTo("1 day 6 hours 45 minutes")
assertThat(59000L.toUptime(), "59 seconds").isEqualTo("59 seconds") assertThat(
assertThat(0L.toUptime(), "0 second").isEqualTo("0 second") 1320300000L.toUptime(),
"upTime(weeks days hours minutes)"
).isEqualTo("2 weeks 1 day 6 hours 45 minutes")
assertThat(2700000L.toUptime(), "upTime(45 minutes)").isEqualTo("45 minutes")
assertThat(60000L.toUptime(), "upTime(1 minute)").isEqualTo("1 minute")
assertThat(59000L.toUptime(), "upTime(59 seconds)").isEqualTo("59 seconds")
assertThat(0L.toUptime(), "upTime(0 second)").isEqualTo("0 second")
} }
} }

View file

@ -46,7 +46,7 @@ class RecapTest {
for (i in 1..20) { for (i in 1..20) {
Recap.storeRecap("sender$i", "test $i", false) Recap.storeRecap("sender$i", "test $i", false)
} }
assertThat(Recap.recaps, "recap first and last").all { assertThat(Recap.recaps, "Recap.recaps").all {
size().isEqualTo(Recap.MAX_RECAPS) size().isEqualTo(Recap.MAX_RECAPS)
prop(MutableList<String>::first) prop(MutableList<String>::first)
.matches("[1-2]\\d{3}-[01]\\d-[0-3]\\d [0-2]\\d:[0-6]\\d - sender11: test 11".toRegex()) .matches("[1-2]\\d{3}-[01]\\d-[0-3]\\d [0-2]\\d:[0-6]\\d - sender11: test 11".toRegex())

View file

@ -46,14 +46,14 @@ class LinksMgrTest {
@Test(groups = ["commands", "links"]) @Test(groups = ["commands", "links"])
fun fetchTitle() { fun fetchTitle() {
assertThat(linksMgr.fetchTitle("https://erik.thauvin.net/"), "Erik").contains("Erik's Weblog") assertThat(linksMgr.fetchTitle("https://erik.thauvin.net/"), "fetchTitle(Erik)").contains("Erik's Weblog")
assertThat(linksMgr.fetchTitle("https://www.google.com/foo"), "Foo").isEqualTo(Constants.NO_TITLE) assertThat(linksMgr.fetchTitle("https://www.google.com/foo"), "fetchTitle(Foo)").isEqualTo(Constants.NO_TITLE)
} }
@Test(groups = ["commands", "links"]) @Test(groups = ["commands", "links"])
fun testMatches() { fun testMatches() {
assertThat(linksMgr.matches("https://www.example.com/"), "https").isTrue() assertThat(linksMgr.matches("https://www.example.com/"), "matches(url)").isTrue()
assertThat(linksMgr.matches("HTTP://erik.thauvin.net/blog/ Erik's Weblog"), "HTTP").isTrue() assertThat(linksMgr.matches("HTTP://erik.thauvin.net/blog/ Erik's Weblog"), "matches(HTTP)").isTrue()
} }
@Test(groups = ["commands", "links"]) @Test(groups = ["commands", "links"])
@ -62,11 +62,11 @@ class LinksMgrTest {
val tags = mutableListOf<String>() val tags = mutableListOf<String>()
linksMgr.matchTagKeywords("Test title with key2", tags) linksMgr.matchTagKeywords("Test title with key2", tags)
assertThat(tags, "key2").contains("key2") assertThat(tags, "tags").contains("key2")
tags.clear() tags.clear()
linksMgr.matchTagKeywords("Test key3 title with key1", tags) linksMgr.matchTagKeywords("Test key3 title with key1", tags)
assertThat(tags, "key1 & key 3").all { assertThat(tags, "tags(key1, key3)").all {
contains("key1") contains("key1")
contains("key3") contains("key3")
size().isEqualTo(2) size().isEqualTo(2)

View file

@ -33,10 +33,13 @@
package net.thauvin.erik.mobibot.commands.seen package net.thauvin.erik.mobibot.commands.seen
import assertk.assertThat import assertk.assertThat
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.isGreaterThan import assertk.assertions.isGreaterThan
import assertk.assertions.isNotEqualTo import assertk.assertions.isNotEqualTo
import assertk.assertions.isTrue import assertk.assertions.isNotNull
import assertk.assertions.key
import assertk.assertions.size
import org.testng.annotations.AfterClass import org.testng.annotations.AfterClass
import org.testng.annotations.BeforeClass import org.testng.annotations.BeforeClass
import org.testng.annotations.Test import org.testng.annotations.Test
@ -51,7 +54,7 @@ class SeenTest {
@BeforeClass @BeforeClass
fun saveTest() { fun saveTest() {
seen.add("ErikT") seen.add("ErikT")
assertThat(tmpFile.fileSize(), "temporary file is empty").isGreaterThan(0) assertThat(tmpFile.fileSize(), "tmpFile.size").isGreaterThan(0)
} }
@AfterClass(alwaysRun = true) @AfterClass(alwaysRun = true)
@ -62,18 +65,18 @@ class SeenTest {
@Test(priority = 1, groups = ["commands"]) @Test(priority = 1, groups = ["commands"])
fun loadTest() { fun loadTest() {
seen.clear() seen.clear()
assertThat(seen.seenNicks.isEmpty(), "nicknames map is not empty").isTrue() assertThat(seen.seenNicks, "seenNicks").isEmpty()
seen.load() seen.load()
assertThat(seen.seenNicks.containsKey(nick), "nick is missing").isTrue() assertThat(seen.seenNicks).key(nick).isNotNull()
} }
@Test(groups = ["commands"]) @Test(groups = ["commands"])
fun addTest() { fun addTest() {
val last = seen.seenNicks[nick]?.lastSeen val last = seen.seenNicks[nick]?.lastSeen
seen.add(nick.lowercase()) seen.add(nick.lowercase())
assertThat(seen.seenNicks.size, "nick is duplicated").isEqualTo(1) assertThat(seen.seenNicks, "seenNicks").size().isEqualTo(1)
assertThat(seen.seenNicks[nick]?.lastSeen, "last seen is not different").isNotEqualTo(last) assertThat(seen.seenNicks[nick]?.lastSeen, "seenNicks[$nick].lastSeen").isNotEqualTo(last)
assertThat(seen.seenNicks[nick]?.nick, "nick is not lowercase").isEqualTo(nick.lowercase()) assertThat(seen.seenNicks[nick]?.nick, "seenNicks[$nick].nick").isEqualTo(nick.lowercase())
} }
@Test(priority = 10, groups = ["commands"]) @Test(priority = 10, groups = ["commands"])
@ -81,6 +84,6 @@ class SeenTest {
seen.clear() seen.clear()
seen.save() seen.save()
seen.load() seen.load()
assertThat(seen.seenNicks.size, "nicknames are not empty.").isEqualTo(0) assertThat(seen.seenNicks, "seenNicks").size().isEqualTo(0)
} }
} }

View file

@ -61,13 +61,13 @@ class TellMessageTest {
prop(TellMessage::recipient).isEqualTo(recipient) prop(TellMessage::recipient).isEqualTo(recipient)
prop(TellMessage::message).isEqualTo(message) prop(TellMessage::message).isEqualTo(message)
} }
assertThat(isValidDate(tellMessage.queued), "queued is valid date/time").isTrue() assertThat(isValidDate(tellMessage.queued), "isValidDate()").isTrue()
assertThat(tellMessage.isMatch(sender), "match sender").isTrue() assertThat(tellMessage.isMatch(sender), "isMatch(sender)").isTrue()
assertThat(tellMessage.isMatch(recipient), "match recipient").isTrue() assertThat(tellMessage.isMatch(recipient), "isMatch(recipient)").isTrue()
assertThat(tellMessage.isMatch("foo"), "foo is no match").isFalse() assertThat(tellMessage.isMatch("foo"), "isMatch(foo)").isFalse()
tellMessage.isReceived = false tellMessage.isReceived = false
assertThat(tellMessage.receptionDate, "reception date not set").isEqualTo(LocalDateTime.MIN) assertThat(tellMessage.receptionDate, "receptionDate").isEqualTo(LocalDateTime.MIN)
tellMessage.isReceived = true tellMessage.isReceived = true
assertThat(isValidDate(tellMessage.receptionDate), "received is valid date/time").isTrue() assertThat(isValidDate(tellMessage.receptionDate), "isValidDate(creationDate)").isTrue()
} }
} }

View file

@ -40,6 +40,7 @@ import assertk.assertions.isFalse
import assertk.assertions.isGreaterThan import assertk.assertions.isGreaterThan
import assertk.assertions.isTrue import assertk.assertions.isTrue
import assertk.assertions.prop import assertk.assertions.prop
import assertk.assertions.size
import org.testng.annotations.AfterClass import org.testng.annotations.AfterClass
import org.testng.annotations.BeforeClass import org.testng.annotations.BeforeClass
import org.testng.annotations.Test import org.testng.annotations.Test
@ -74,9 +75,9 @@ class TellMessagesMgrTest {
queued = LocalDateTime.now().minusDays(maxDays) queued = LocalDateTime.now().minusDays(maxDays)
}) })
val size = testMessages.size val size = testMessages.size
assertThat(TellMessagesMgr.clean(testMessages, maxDays + 2), "maxDays = 12").isFalse() assertThat(TellMessagesMgr.clean(testMessages, maxDays + 2), "clean(maxDays=${maxDays + 2})").isFalse()
assertThat(TellMessagesMgr.clean(testMessages, maxDays), "maxDays = 10").isTrue() assertThat(TellMessagesMgr.clean(testMessages, maxDays), "clean(maxDays=$maxDays)").isTrue()
assertThat(testMessages.size, "size-- after clean").isEqualTo(size - 1) assertThat(testMessages, "testMessages").size().isEqualTo(size - 1)
} }
@Test(groups = ["commands", "tell"]) @Test(groups = ["commands", "tell"])

View file

@ -77,8 +77,8 @@ class EntriesUtilsTest {
).isEqualTo("L$i: [Skynx$i] \u0002Mobitopia$i\u0002 ( \u000303https://www.mobitopia.org/$i\u000F )") ).isEqualTo("L$i: [Skynx$i] \u0002Mobitopia$i\u0002 ( \u000303https://www.mobitopia.org/$i\u000F )")
} }
assertThat(links.first().addComment(comment), "add comment").isEqualTo(0) assertThat(links.first().addComment(comment), "addComment()").isEqualTo(0)
assertThat(buildLink(0, links.first(), isView = true), "isView = true").contains("[+1]") assertThat(buildLink(0, links.first(), isView = true), "buildLink(isView=true)").contains("[+1]")
} }
@Test(groups = ["entries"]) @Test(groups = ["entries"])

View file

@ -65,7 +65,7 @@ class EntryLinkTest {
entryLink.addComment("c$i", "u$i") entryLink.addComment("c$i", "u$i")
i++ i++
} }
assertThat(entryLink.comments.size, "getComments().size() == 5").isEqualTo(i) assertThat(entryLink.comments, "comments").size().isEqualTo(i)
i = 0 i = 0
for (comment in entryLink.comments) { for (comment in entryLink.comments) {
assertThat(comment).all { assertThat(comment).all {
@ -83,11 +83,11 @@ class EntryLinkTest {
entryLink.addComment("nothing", "nobody") entryLink.addComment("nothing", "nobody")
entryLink.setComment(0, "something", "somebody") entryLink.setComment(0, "something", "somebody")
val comment = entryLink.getComment(0) val comment = entryLink.getComment(0)
assertThat(comment, "get first comment").all { assertThat(comment, "comment[first]").all {
prop(EntryComment::nick).isEqualTo("somebody") prop(EntryComment::nick).isEqualTo("somebody")
prop(EntryComment::comment).isEqualTo("something") prop(EntryComment::comment).isEqualTo("something")
} }
assertThat(entryLink.deleteComment(comment), "delete comment").isTrue() assertThat(entryLink.deleteComment(comment), "deleteComment").isTrue()
assertThat(entryLink.deleteComment(comment), "comment is already deleted").isFalse() assertThat(entryLink.deleteComment(comment), "comment is already deleted").isFalse()
} }
@ -96,19 +96,19 @@ class EntryLinkTest {
val tag = "test" val tag = "test"
val tags = listOf(SyndCategoryImpl().apply { name = tag }) val tags = listOf(SyndCategoryImpl().apply { name = tag })
val link = EntryLink("link", "title", "nick", "channel", Date(), tags) val link = EntryLink("link", "title", "nick", "channel", Date(), tags)
assertThat(link.tags.size, "check tag size").isEqualTo(tags.size) assertThat(link.tags, "tags").size().isEqualTo(tags.size)
assertThat(link.tags[0].name, "check tag name").isEqualTo(tag) assertThat(link.tags[0].name, "tag[0].name").isEqualTo(tag)
assertThat(link.pinboardTags, "check pinboard tags").isEqualTo("nick,$tag") assertThat(link.pinboardTags, "pinboardTags").isEqualTo("nick,$tag")
} }
@Test(groups = ["entries"]) @Test(groups = ["entries"])
fun testMatches() { fun testMatches() {
assertThat(entryLink.matches("mobitopia"), "match mobitopia").isTrue() assertThat(entryLink.matches("mobitopia"), "matches(mobitopia)").isTrue()
assertThat(entryLink.matches("skynx"), "match nick").isTrue() assertThat(entryLink.matches("skynx"), "match(nick)").isTrue()
assertThat(entryLink.matches("www.mobitopia.org"), "match url").isTrue() assertThat(entryLink.matches("www.mobitopia.org"), "matches(url)").isTrue()
assertThat(entryLink.matches("foo"), "match foo").isFalse() assertThat(entryLink.matches("foo"), "matches(foo)").isFalse()
assertThat(entryLink.matches("<empty>"), "match empty").isFalse() assertThat(entryLink.matches("<empty>"), "matches(empty)").isFalse()
assertThat(entryLink.matches(null), "match null").isFalse() assertThat(entryLink.matches(null), "matches(null)").isFalse()
} }
@ -116,19 +116,19 @@ class EntryLinkTest {
fun testTags() { fun testTags() {
val tags: List<SyndCategory> = entryLink.tags val tags: List<SyndCategory> = entryLink.tags
for ((i, tag) in tags.withIndex()) { for ((i, tag) in tags.withIndex()) {
assertThat(tag.name, "tag.getName($i)").isEqualTo("tag" + (i + 1)) assertThat(tag.name, "tag.name($i)").isEqualTo("tag${i + 1}")
} }
assertThat(entryLink.tags, "size is 5").size().isEqualTo(5) assertThat(entryLink.tags, "tags").size().isEqualTo(5)
entryLink.setTags("-tag5") entryLink.setTags("-tag5")
entryLink.setTags("+mobitopia") entryLink.setTags("+mobitopia")
entryLink.setTags("tag4") entryLink.setTags("tag4")
entryLink.setTags("-mobitopia") entryLink.setTags("-mobitopia")
assertThat(entryLink.pinboardTags, "getPinboardTags()") assertThat(entryLink.pinboardTags, "pinboardTags")
.isEqualTo(entryLink.nick + ",tag1,tag2,tag3,tag4,mobitopia") .isEqualTo(entryLink.nick + ",tag1,tag2,tag3,tag4,mobitopia")
val size = entryLink.tags.size val size = entryLink.tags.size
entryLink.setTags("") entryLink.setTags("")
assertThat(entryLink.tags.size, "empty tag").isEqualTo(size) assertThat(entryLink.tags, "setTags('')").size().isEqualTo(size)
entryLink.setTags(" ") entryLink.setTags(" ")
assertThat(entryLink.tags.size, "blank tag").isEqualTo(size) assertThat(entryLink.tags, "setTags(' ')").size().isEqualTo(size)
} }
} }

View file

@ -35,17 +35,18 @@ package net.thauvin.erik.mobibot.entries
import assertk.all import assertk.all
import assertk.assertThat import assertk.assertThat
import assertk.assertions.endsWith import assertk.assertions.endsWith
import assertk.assertions.exists
import assertk.assertions.index
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.isTrue import assertk.assertions.isTrue
import assertk.assertions.prop import assertk.assertions.prop
import assertk.assertions.size
import net.thauvin.erik.mobibot.Utils.today import net.thauvin.erik.mobibot.Utils.today
import org.testng.annotations.BeforeSuite import org.testng.annotations.BeforeSuite
import org.testng.annotations.Test import org.testng.annotations.Test
import java.nio.file.Paths import java.nio.file.Paths
import java.util.Date import java.util.Date
import kotlin.io.path.absolutePathString
import kotlin.io.path.deleteIfExists import kotlin.io.path.deleteIfExists
import kotlin.io.path.exists
import kotlin.io.path.fileSize import kotlin.io.path.fileSize
import kotlin.io.path.name import kotlin.io.path.name
@ -64,11 +65,11 @@ class FeedMgrTest {
@Test(groups = ["entries"]) @Test(groups = ["entries"])
fun testFeedMgr() { fun testFeedMgr() {
// Load the feed // Load the feed
assertThat(FeedsMgr.loadFeed(entries), "pubDate").isEqualTo("2021-10-31") assertThat(FeedsMgr.loadFeed(entries), "loadFeed()").isEqualTo("2021-10-31")
assertThat(entries.links.size, "2 links").isEqualTo(2) assertThat(entries.links, "entries.links").size().isEqualTo(2)
entries.links.forEachIndexed { i, entryLink -> entries.links.forEachIndexed { i, entryLink ->
assertThat(entryLink, "Example $(i + 1)").all { assertThat(entryLink, "entryLink[${i + 1}]").all {
prop(EntryLink::title).isEqualTo("Example ${i + 1}") prop(EntryLink::title).isEqualTo("Example ${i + 1}")
prop(EntryLink::link).isEqualTo("https://www.example.com/${i + 1}") prop(EntryLink::link).isEqualTo("https://www.example.com/${i + 1}")
prop(EntryLink::channel).isEqualTo(channel) prop(EntryLink::channel).isEqualTo(channel)
@ -79,19 +80,19 @@ class FeedMgrTest {
} }
with(entries.links.first()) { with(entries.links.first()) {
assertThat(nick, "first nick").isEqualTo("ErikT") assertThat(nick, "nick[first]").isEqualTo("ErikT")
assertThat(date, "first date").isEqualTo(Date(1635638400000L)) assertThat(date, "date[first]").isEqualTo(Date(1635638400000L))
assertThat(comments.first(), "first comment").all { assertThat(comments.first(), "comments[first]").all {
prop(EntryComment::comment).endsWith("comment 1.") prop(EntryComment::comment).endsWith("comment 1.")
prop(EntryComment::nick).isEqualTo("ErikT") prop(EntryComment::nick).isEqualTo("ErikT")
} }
assertThat(comments.last(), "last comment").all { assertThat(comments.last(), "comments[last]").all {
prop(EntryComment::comment).endsWith("comment 2.") prop(EntryComment::comment).endsWith("comment 2.")
prop(EntryComment::nick).isEqualTo("Skynx") prop(EntryComment::nick).isEqualTo("Skynx")
} }
} }
assertThat(entries.links[1], "second link").all { assertThat(entries.links, "links").index(1).all {
prop(EntryLink::nick).isEqualTo("Skynx") prop(EntryLink::nick).isEqualTo("Skynx")
prop(EntryLink::date).isEqualTo(Date(1635638460000L)) prop(EntryLink::date).isEqualTo(Date(1635638460000L))
} }
@ -102,20 +103,20 @@ class FeedMgrTest {
// Save the feed // Save the feed
FeedsMgr.saveFeed(entries, currentFile.name) FeedsMgr.saveFeed(entries, currentFile.name)
assertThat(currentFile.exists(), "${currentFile.absolutePathString()} exists").isTrue() assertThat(currentFile, "currentFile").exists()
assertThat(backlogFile.exists(), "${backlogFile.absolutePathString()} exits").isTrue() assertThat(backlogFile, "backlogFile").exists()
assertThat(currentFile.fileSize(), "files are identical").isEqualTo(backlogFile.fileSize()) assertThat(currentFile.fileSize(), "currentFile == backlogFile").isEqualTo(backlogFile.fileSize())
// Load the test feed // Load the test feed
entries.links.clear() entries.links.clear()
FeedsMgr.loadFeed(entries, currentFile.name) FeedsMgr.loadFeed(entries, currentFile.name)
entries.links.forEachIndexed { i, entryLink -> entries.links.forEachIndexed { i, entryLink ->
assertThat(entryLink.title, "${currentFile.name} title ${i + 1}").isEqualTo("Example ${i + 1}") assertThat(entryLink.title, "entryLink.title[${i + 1}]").isEqualTo("Example ${i + 1}")
} }
assertThat(currentFile.deleteIfExists(), "delete ${currentFile.absolutePathString()}").isTrue() assertThat(currentFile.deleteIfExists(), "currentFile.deleteIfExists()").isTrue()
assertThat(backlogFile.deleteIfExists(), "delete ${backlogFile.absolutePathString()}").isTrue() assertThat(backlogFile.deleteIfExists(), "backlogFile.deleteIfExists()").isTrue()
} }
} }

View file

@ -57,14 +57,14 @@ class CryptoPricesTest {
@Throws(ModuleException::class) @Throws(ModuleException::class)
fun testMarketPrice() { fun testMarketPrice() {
var price = currentPrice(listOf("BTC")) var price = currentPrice(listOf("BTC"))
assertThat(price, "BTC in USD").all { assertThat(price, "currentPrice(BTC)").all {
prop(CryptoPrice::base).isEqualTo("BTC") prop(CryptoPrice::base).isEqualTo("BTC")
prop(CryptoPrice::currency).isEqualTo("USD") prop(CryptoPrice::currency).isEqualTo("USD")
prop(CryptoPrice::amount).transform { it.signum() }.isGreaterThan(0) prop(CryptoPrice::amount).transform { it.signum() }.isGreaterThan(0)
} }
price = currentPrice(listOf("ETH", "EUR")) price = currentPrice(listOf("ETH", "EUR"))
assertThat(price, "ETH in EUR").all { assertThat(price, "currentPrice(ETH, EUR)").all {
prop(CryptoPrice::base).isEqualTo("ETH") prop(CryptoPrice::base).isEqualTo("ETH")
prop(CryptoPrice::currency).isEqualTo("EUR") prop(CryptoPrice::currency).isEqualTo("EUR")
prop(CryptoPrice::amount).transform { it.signum() }.isGreaterThan(0) prop(CryptoPrice::amount).transform { it.signum() }.isGreaterThan(0)

View file

@ -60,21 +60,21 @@ class CurrencyConverterTest {
fun testConvertCurrency() { fun testConvertCurrency() {
assertThat( assertThat(
convertCurrency("100 USD to EUR").msg, convertCurrency("100 USD to EUR").msg,
"100 USD to EUR is invalid" "convertCurrency(100 USD to EUR)"
).matches("100 United States Dollar = \\d{2,3}\\.\\d+ Euro".toRegex()) ).matches("100 United States Dollar = \\d{2,3}\\.\\d+ Euro".toRegex())
assertThat( assertThat(
convertCurrency("1 USD to BTC").msg, convertCurrency("1 USD to BTC").msg,
"1 USD to BTC is invalid" "convertCurrency(1 USD to BTC)"
).matches("1 United States Dollar = 0\\.\\d+ Bitcoin".toRegex()) ).matches("1 United States Dollar = 0\\.\\d+ Bitcoin".toRegex())
assertThat( assertThat(
convertCurrency("100,000.00 GBP to BTC").msg, convertCurrency("100,000.00 GBP to BTC").msg,
"100 USD to EUR is invalid" "convertCurrency(100,000.00 GBP to BTC)"
).matches("100,000.00 British Pound Sterling = \\d{1,2}\\.\\d+ Bitcoin".toRegex()) ).matches("100,000.00 British Pound Sterling = \\d{1,2}\\.\\d+ Bitcoin".toRegex())
assertThat(convertCurrency("100 USD to USD"), "100 USD to USD is invalid").all { assertThat(convertCurrency("100 USD to USD"), "convertCurrency(100 USD to USD)").all {
prop(Message::msg).contains("You're kidding, right?") prop(Message::msg).contains("You're kidding, right?")
isInstanceOf(PublicMessage::class.java) isInstanceOf(PublicMessage::class.java)
} }
assertThat(convertCurrency("100 USD"), "100 USD should be invalid").all { assertThat(convertCurrency("100 USD"), "convertCurrency(100 USD)").all {
prop(Message::msg).contains("Invalid query.") prop(Message::msg).contains("Invalid query.")
isInstanceOf(ErrorMessage::class.java) isInstanceOf(ErrorMessage::class.java)
} }

View file

@ -41,14 +41,14 @@ import org.testng.annotations.Test
class DiceTest { class DiceTest {
@Test(groups = ["modules"]) @Test(groups = ["modules"])
fun testRoll() { fun testRoll() {
assertThat(Dice.roll(1, 1), "1d1 is invalid").isEqualTo("\u00021\u0002") assertThat(Dice.roll(1, 1), "roll(1d1)").isEqualTo("\u00021\u0002")
assertThat(Dice.roll(2, 1), "2d1 is invalid") assertThat(Dice.roll(2, 1), "roll(2d1)")
.isEqualTo("\u00021\u0002 + \u00021\u0002 = \u00022\u0002") .isEqualTo("\u00021\u0002 + \u00021\u0002 = \u00022\u0002")
assertThat(Dice.roll(5, 1), "5d1 is invalid") assertThat(Dice.roll(5, 1), "roll(5d1)")
.isEqualTo("\u00021\u0002 + \u00021\u0002 + \u00021\u0002 + \u00021\u0002 + \u00021\u0002 = \u00025\u0002") .isEqualTo("\u00021\u0002 + \u00021\u0002 + \u00021\u0002 + \u00021\u0002 + \u00021\u0002 = \u00025\u0002")
assertThat(Dice.roll(2, 6), "2d6 is invalid") assertThat(Dice.roll(2, 6), "roll(2d6)")
.matches("\u0002[1-6]\u0002 \\+ \u0002[1-6]\u0002 = \u0002[1-9][0-2]?\u0002".toRegex()) .matches("\u0002[1-6]\u0002 \\+ \u0002[1-6]\u0002 = \u0002[1-9][0-2]?\u0002".toRegex())
assertThat(Dice.roll(3, 7), "3d7 is invalid") assertThat(Dice.roll(3, 7), "roll(3d7)")
.matches("\u0002[1-7]\u0002 \\+ \u0002[1-7]\u0002 \\+ \u0002[1-7]\u0002 = \u0002\\d{1,2}\u0002".toRegex()) .matches("\u0002[1-7]\u0002 \\+ \u0002[1-7]\u0002 \\+ \u0002[1-7]\u0002 = \u0002\\d{1,2}\u0002".toRegex())
} }
} }

View file

@ -36,6 +36,7 @@ import assertk.assertThat
import assertk.assertions.contains import assertk.assertions.contains
import assertk.assertions.hasMessage import assertk.assertions.hasMessage
import assertk.assertions.hasNoCause import assertk.assertions.hasNoCause
import assertk.assertions.index
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.isFailure import assertk.assertions.isFailure
import assertk.assertions.isInstanceOf import assertk.assertions.isInstanceOf
@ -56,7 +57,7 @@ class GoogleSearchTest : LocalProperties() {
fun testAPIKeys() { fun testAPIKeys() {
assertThat( assertThat(
searchGoogle("", "apikey", "cssKey").first(), searchGoogle("", "apikey", "cssKey").first(),
"empty query" "searchGoogle(empty)"
).isInstanceOf(ErrorMessage::class.java) ).isInstanceOf(ErrorMessage::class.java)
assertThat { searchGoogle("test", "", "apiKey") }.isFailure() assertThat { searchGoogle("test", "", "apiKey") }.isFailure()
@ -78,12 +79,14 @@ class GoogleSearchTest : LocalProperties() {
val cseKey = getProperty(GoogleSearch.GOOGLE_CSE_KEY_PROP) val cseKey = getProperty(GoogleSearch.GOOGLE_CSE_KEY_PROP)
try { try {
var messages = searchGoogle("mobibot", apiKey, cseKey) var query = "mobibot"
assertThat(messages, "search result should not be empty").isNotEmpty() var messages = searchGoogle(query, apiKey, cseKey)
assertThat(messages[0].msg, "search query not found").contains("mobibot", true) assertThat(messages, "searchGoogle($query)").isNotEmpty()
assertThat(messages, "searchGoogle($query)").index(0).prop(Message::msg).contains(query, true)
messages = searchGoogle("adadflkjl", apiKey, cseKey) query = "adadflkjl"
assertThat(messages[0], "not found").all { messages = searchGoogle(query, apiKey, cseKey)
assertThat(messages, "searchGoogle($query)").index(0).all {
isInstanceOf(ErrorMessage::class.java) isInstanceOf(ErrorMessage::class.java)
prop(Message::msg).isEqualTo("No results found.") prop(Message::msg).isEqualTo("No results found.")
} }

View file

@ -35,7 +35,10 @@ import assertk.assertThat
import assertk.assertions.isGreaterThan import assertk.assertions.isGreaterThan
import assertk.assertions.isInstanceOf import assertk.assertions.isInstanceOf
import assertk.assertions.isNotEmpty import assertk.assertions.isNotEmpty
import assertk.assertions.prop
import assertk.assertions.size
import net.thauvin.erik.mobibot.modules.Joke.Companion.randomJoke import net.thauvin.erik.mobibot.modules.Joke.Companion.randomJoke
import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.PublicMessage import net.thauvin.erik.mobibot.msg.PublicMessage
import org.testng.annotations.Test import org.testng.annotations.Test
@ -47,10 +50,10 @@ class JokeTest {
@Throws(ModuleException::class) @Throws(ModuleException::class)
fun testRandomJoke() { fun testRandomJoke() {
val joke = randomJoke() val joke = randomJoke()
assertThat(joke.size, "joke should not be empty").isGreaterThan(0) assertThat(joke, "randomJoke()").size().isGreaterThan(0)
joke.forEach { joke.forEach {
assertThat(it, "message should be public").isInstanceOf(PublicMessage::class.java) assertThat(it, "randomJoke()").isInstanceOf(PublicMessage::class.java)
assertThat(it.msg, "message should not be empty").isNotEmpty() assertThat(it, "randomJoke()").prop(Message::msg).isNotEmpty()
} }
} }
} }

View file

@ -56,6 +56,6 @@ class LookupTest {
@Throws(Exception::class) @Throws(Exception::class)
fun testWhois() { fun testWhois() {
val result = whois("17.178.96.59", Lookup.WHOIS_HOST) val result = whois("17.178.96.59", Lookup.WHOIS_HOST)
assertThat(result, "whois(17.178.96.59/Apple Inc.").any { it.contains("Apple Inc.") } assertThat(result, "whois(17.178.96.59").any { it.contains("Apple Inc.") }
} }
} }

View file

@ -78,26 +78,32 @@ class ModuleExceptionTest {
fun testSanitizeMessage() { fun testSanitizeMessage() {
val apiKey = "1234567890" val apiKey = "1234567890"
var 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"))
assertThat(e.sanitize(apiKey, "", "me").message, "sanitized url is invalid").isNotNull().all { assertThat(
e.sanitize(apiKey, "", "me").message, "ModuleException(debugMessage, message, IOException(url))"
).isNotNull().all {
contains("xxxxxxxxxx", "userID=xx", "java.io.IOException") contains("xxxxxxxxxx", "userID=xx", "java.io.IOException")
doesNotContain(apiKey, "me") doesNotContain(apiKey, "me")
} }
e = ModuleException(debugMessage, message, null) e = ModuleException(debugMessage, message, null)
assertThat(e.sanitize(apiKey), "cause should be null").hasMessage(message) assertThat(e.sanitize(apiKey), "ModuleException(debugMessage, message, null)").hasMessage(message)
e = ModuleException(debugMessage, message, IOException()) e = ModuleException(debugMessage, message, IOException())
assertThat(e.sanitize(apiKey), "cause message is invalid").hasMessage(message) assertThat(e.sanitize(apiKey), "ModuleException(debugMessage, message, IOException())").hasMessage(message)
e = ModuleException(debugMessage, apiKey) e = ModuleException(debugMessage, apiKey)
assertThat(e.sanitize(apiKey).message, "message should not contain api key").isNotNull().doesNotContain(apiKey) assertThat(e.sanitize(apiKey).message, "ModuleException(debugMessage, apiKey)").isNotNull()
.doesNotContain(apiKey)
val msg: String? = null val msg: String? = null
e = ModuleException(debugMessage, msg, IOException(msg)) e = ModuleException(debugMessage, msg, IOException(msg))
assertThat(e.sanitize(apiKey).message, "message should be null").isNull() assertThat(e.sanitize(apiKey).message, "ModuleException(debugMessage, msg, IOException(msg))").isNull()
e = ModuleException(debugMessage, msg, IOException("foo is $apiKey")) e = ModuleException(debugMessage, msg, IOException("foo is $apiKey"))
assertThat(e.sanitize(" ", apiKey, "foo").message, "message should not contain key").isNotNull().all { assertThat(
e.sanitize(" ", apiKey, "foo").message,
"ModuleException(debugMessage, msg, IOException(foo is $apiKey))"
).isNotNull().all {
doesNotContain(apiKey) doesNotContain(apiKey)
endsWith("xxx is xxxxxxxxxx") endsWith("xxx is xxxxxxxxxx")
} }

View file

@ -43,13 +43,13 @@ import org.testng.annotations.Test
class PingTest { class PingTest {
@Test(groups = ["modules"]) @Test(groups = ["modules"])
fun testPingsArray() { fun testPingsArray() {
assertThat(Ping.PINGS, "Pings array should be empty").isNotEmpty() assertThat(Ping.PINGS, "Ping.PINGS").isNotEmpty()
} }
@Test(groups = ["modules"]) @Test(groups = ["modules"])
fun testRandomPing() { fun testRandomPing() {
for (i in 0..9) { for (i in 0..9) {
assertThat(Ping.PINGS, "Random ping $i").contains(randomPing()) assertThat(Ping.PINGS, "Ping.PINGS[$i]").contains(randomPing())
} }
} }
} }

View file

@ -34,6 +34,7 @@ package net.thauvin.erik.mobibot.modules
import assertk.all import assertk.all
import assertk.assertThat import assertk.assertThat
import assertk.assertions.hasNoCause import assertk.assertions.hasNoCause
import assertk.assertions.index
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.isFailure import assertk.assertions.isFailure
import assertk.assertions.isInstanceOf import assertk.assertions.isInstanceOf
@ -60,18 +61,21 @@ class StockQuoteTest : LocalProperties() {
fun testGetQuote() { fun testGetQuote() {
val apiKey = getProperty(StockQuote.ALPHAVANTAGE_API_KEY_PROP) val apiKey = getProperty(StockQuote.ALPHAVANTAGE_API_KEY_PROP)
try { try {
val messages = getQuote("apple inc", apiKey) var symbol = "apple inc"
val messages = getQuote(symbol, apiKey)
assertThat(messages, "response not empty").isNotEmpty() assertThat(messages, "response not empty").isNotEmpty()
assertThat(messages[0].msg, "stock symbol should be AAPL").matches("Symbol: AAPL .*".toRegex()) assertThat(messages, "getQuote($symbol)").index(0).prop(Message::msg).matches("Symbol: AAPL .*".toRegex())
assertThat(messages[1].msg, "price label is invalid").matches(buildMatch("Price").toRegex()) assertThat(messages, "getQuote($symbol)").index(1).prop(Message::msg).matches(buildMatch("Price").toRegex())
assertThat(messages[2].msg, "previous label is invalid").matches(buildMatch("Previous").toRegex()) assertThat(messages, "getQuote($symbol)").index(2).prop(Message::msg)
assertThat(messages[3].msg, "open label is invalid").matches(buildMatch("Open").toRegex()) .matches(buildMatch("Previous").toRegex())
assertThat(messages, "getQuote($symbol)").index(3).prop(Message::msg).matches(buildMatch("Open").toRegex())
assertThat(getQuote("blahfoo", apiKey).first(), "symbol should be invalid").all { symbol = "blahfoo"
assertThat(getQuote(symbol, apiKey).first(), "getQuote($symbol)").all {
isInstanceOf(ErrorMessage::class.java) isInstanceOf(ErrorMessage::class.java)
prop(Message::msg).isEqualTo(StockQuote.INVALID_SYMBOL) prop(Message::msg).isEqualTo(StockQuote.INVALID_SYMBOL)
} }
assertThat(getQuote("", "apikey").first(), "symbol should be empty").all { assertThat(getQuote("", "apikey").first(), "getQuote(empty)").all {
isInstanceOf(ErrorMessage::class.java) isInstanceOf(ErrorMessage::class.java)
prop(Message::msg).isEqualTo(StockQuote.INVALID_SYMBOL) prop(Message::msg).isEqualTo(StockQuote.INVALID_SYMBOL)
} }

View file

@ -36,11 +36,13 @@ import assertk.assertThat
import assertk.assertions.contains import assertk.assertions.contains
import assertk.assertions.endsWith import assertk.assertions.endsWith
import assertk.assertions.hasNoCause import assertk.assertions.hasNoCause
import assertk.assertions.index
import assertk.assertions.isEqualTo import assertk.assertions.isEqualTo
import assertk.assertions.isFailure import assertk.assertions.isFailure
import assertk.assertions.isInstanceOf import assertk.assertions.isInstanceOf
import assertk.assertions.isNotNull import assertk.assertions.isNotNull
import assertk.assertions.isTrue import assertk.assertions.isTrue
import assertk.assertions.prop
import net.aksingh.owmjapis.api.APIException import net.aksingh.owmjapis.api.APIException
import net.aksingh.owmjapis.core.OWM import net.aksingh.owmjapis.core.OWM
import net.thauvin.erik.mobibot.LocalProperties import net.thauvin.erik.mobibot.LocalProperties
@ -49,6 +51,7 @@ import net.thauvin.erik.mobibot.modules.Weather2.Companion.ftoC
import net.thauvin.erik.mobibot.modules.Weather2.Companion.getCountry import net.thauvin.erik.mobibot.modules.Weather2.Companion.getCountry
import net.thauvin.erik.mobibot.modules.Weather2.Companion.getWeather import net.thauvin.erik.mobibot.modules.Weather2.Companion.getWeather
import net.thauvin.erik.mobibot.modules.Weather2.Companion.mphToKmh import net.thauvin.erik.mobibot.modules.Weather2.Companion.mphToKmh
import net.thauvin.erik.mobibot.msg.Message
import org.testng.annotations.Test import org.testng.annotations.Test
/** /**
@ -82,39 +85,42 @@ class Weather2Test : LocalProperties() {
@Test(groups = ["modules"]) @Test(groups = ["modules"])
@Throws(ModuleException::class) @Throws(ModuleException::class)
fun testWeather() { fun testWeather() {
var messages = getWeather("98204", getProperty(OWM_API_KEY_PROP)) var query = "98204"
assertThat(messages[0].msg, "city is not Everett").all { var messages = getWeather(query, getProperty(OWM_API_KEY_PROP))
assertThat(messages, "getWeather($query)").index(0).prop(Message::msg).all {
contains("Everett, United States") contains("Everett, United States")
contains("US") contains("US")
} }
assertThat(messages[messages.size - 1].msg, "zip code is not Everett").endsWith("98204%2CUS") assertThat(messages, "getWeather($query)").index(messages.size - 1).prop(Message::msg).endsWith("98204%2CUS")
messages = getWeather("San Francisco", getProperty(OWM_API_KEY_PROP)) query = "San Francisco"
assertThat(messages[0].msg, "city is not San Francisco").all { messages = getWeather(query, getProperty(OWM_API_KEY_PROP))
assertThat(messages, "getWeather($query)").index(0).prop(Message::msg).all {
contains("San Francisco") contains("San Francisco")
contains("US") contains("US")
} }
assertThat(messages[messages.size - 1].msg, "city code is not San Fran").endsWith("5391959") assertThat(messages, "getWeather($query)").index(messages.size - 1).prop(Message::msg).endsWith("5391959")
messages = getWeather("London, GB", getProperty(OWM_API_KEY_PROP)) query = "London, GB"
assertThat(messages[0].msg, "city is not London").all { messages = getWeather(query, getProperty(OWM_API_KEY_PROP))
assertThat(messages, "getWeather($query)").index(0).prop(Message::msg).all {
contains("London, United Kingdom") contains("London, United Kingdom")
contains("GB") contains("GB")
} }
assertThat(messages[messages.size - 1].msg, "city code is not London").endsWith("2643743") assertThat(messages, "getWeather($query)").index(messages.size - 1).prop(Message::msg).endsWith("2643743")
try { try {
getWeather("Foo, US", getProperty(OWM_API_KEY_PROP)) query = "Foo, US"
getWeather(query, getProperty(OWM_API_KEY_PROP))
} catch (e: ModuleException) { } catch (e: ModuleException) {
assertThat(e.cause, "cause is not an API exception").isNotNull().isInstanceOf(APIException::class.java) assertThat(e.cause, "getWeather($query)").isNotNull().isInstanceOf(APIException::class.java)
} }
assertThat { getWeather("test", "") }.isFailure() query = "test"
.isInstanceOf(ModuleException::class.java).hasNoCause() assertThat { getWeather(query, "") }.isFailure().isInstanceOf(ModuleException::class.java).hasNoCause()
assertThat { getWeather("test", null) }.isFailure() assertThat { getWeather(query, null) }.isFailure().isInstanceOf(ModuleException::class.java).hasNoCause()
.isInstanceOf(ModuleException::class.java).hasNoCause()
messages = getWeather("", "apikey") messages = getWeather("", "apikey")
assertThat(messages[0].isError, "empty query should be an error").isTrue() assertThat(messages, "getWeather(empty)").index(0).prop(Message::isError).isTrue()
} }
} }

View file

@ -59,11 +59,13 @@ class WolframAlphaTest : LocalProperties() {
fun queryWolframTest() { fun queryWolframTest() {
val apiKey = getProperty(WolframAlpha.WOLFRAM_APPID_KEY) val apiKey = getProperty(WolframAlpha.WOLFRAM_APPID_KEY)
try { try {
assertThat(WolframAlpha.queryWolfram("SFO to SEA", appId = apiKey), "SFO to SEA").contains("miles") var query = "SFO to SEA"
assertThat(WolframAlpha.queryWolfram(query, appId = apiKey), "queryWolfram($query)").contains("miles")
query = "SFO to LAX"
assertThat( assertThat(
WolframAlpha.queryWolfram("SFO to LAX", WolframAlpha.METRIC, apiKey), WolframAlpha.queryWolfram(query, WolframAlpha.METRIC, apiKey),
"SFO to LA" "queryWolfram($query)"
).contains("kilometers") ).contains("kilometers")
} catch (e: ModuleException) { } catch (e: ModuleException) {
// Avoid displaying api key in CI logs // Avoid displaying api key in CI logs

View file

@ -50,17 +50,17 @@ import java.time.ZoneId
class WordTimeTest { class WordTimeTest {
@Test(groups = ["modules"]) @Test(groups = ["modules"])
fun testTime() { fun testTime() {
assertThat(time(), "no zone").matches( assertThat(time(), "time()").matches(
("The time is ${Colors.BOLD}\\d{1,2}:\\d{2}${Colors.BOLD} " + ("The time is ${Colors.BOLD}\\d{1,2}:\\d{2}${Colors.BOLD} " +
"on ${Colors.BOLD}\\w+, \\d{1,2} \\w+ \\d{4}${Colors.BOLD} " + "on ${Colors.BOLD}\\w+, \\d{1,2} \\w+ \\d{4}${Colors.BOLD} " +
"in ${Colors.BOLD}Los Angeles${Colors.BOLD}").toRegex() "in ${Colors.BOLD}Los Angeles${Colors.BOLD}").toRegex()
) )
assertThat(time(""), "zone is not LA").endsWith("Los Angeles".bold()) assertThat(time(""), "time()").endsWith("Los Angeles".bold())
assertThat(time("PST"), "zone is not PST").endsWith("Los Angeles".bold()) assertThat(time("PST"), "time(PST)").endsWith("Los Angeles".bold())
assertThat(time("GB"), "zone is not London").endsWith("London".bold()) assertThat(time("GB"), "time(GB)").endsWith("London".bold())
assertThat(time("FR"), "zone is not Paris").endsWith("Paris".bold()) assertThat(time("FR"), "time(FR)").endsWith("Paris".bold())
assertThat(time("BLAH"), "zone should be unsupported").startsWith("Unsupported") assertThat(time("BLAH"), "time(BLAH)").startsWith("Unsupported")
assertThat(time("BEAT"), "$BEATS_KEYWORD is invalid").matches("[\\w ]+ .?@\\d{3}+.? .beats".toRegex()) assertThat(time("BEAT"), "time($BEATS_KEYWORD)").matches("[\\w ]+ .?@\\d{3}+.? .beats".toRegex())
} }
@Test(groups = ["modules"]) @Test(groups = ["modules"])

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle #Generated by the Semver Plugin for Gradle
#Sun Oct 02 07:45:05 PDT 2022 #Mon Oct 03 10:57:55 PDT 2022
version.buildmeta=689 version.buildmeta=724
version.major=0 version.major=0
version.minor=8 version.minor=8
version.patch=0 version.patch=0
version.prerelease=rc version.prerelease=rc
version.project=mobibot version.project=mobibot
version.semver=0.8.0-rc+689 version.semver=0.8.0-rc+724