From 2a46761dc57c47b268a90faddf7bafdf838a2279 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Mon, 25 Oct 2021 23:41:52 -0700 Subject: [PATCH] Cleaned up tests. --- .../erik/mobibot/commands/tell/TellMessageTest.kt | 5 ++--- .../thauvin/erik/mobibot/entries/EntryLinkTest.kt | 11 +++++------ .../thauvin/erik/mobibot/modules/CryptoPricesTest.kt | 10 ++++------ .../erik/mobibot/modules/CurrencyConverterTest.kt | 5 ++--- .../net/thauvin/erik/mobibot/modules/JokeTest.kt | 2 +- .../net/thauvin/erik/mobibot/modules/LookupTest.kt | 4 ++-- .../erik/mobibot/modules/ModuleExceptionTest.kt | 3 ++- .../thauvin/erik/mobibot/modules/StockQuoteTest.kt | 12 ++++++++---- .../net/thauvin/erik/mobibot/modules/WordTimeTest.kt | 8 +++----- 9 files changed, 29 insertions(+), 31 deletions(-) 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 7f5de32..86422ca 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 @@ -51,9 +51,8 @@ class TellMessageTest { val recipient = "recipient" val sender = "sender" val tellMessage = TellMessage(sender, recipient, message) - assertThat(tellMessage.sender).describedAs(sender).isEqualTo(sender) - assertThat(tellMessage.recipient).describedAs(recipient).isEqualTo(recipient) - assertThat(tellMessage.message).describedAs(message).isEqualTo(message) + assertThat(tellMessage).extracting("sender", "recipient", "message") + .containsExactly(sender, recipient, message) assertThat(isValidDate(tellMessage.queued)).describedAs("queued is valid date/time").isTrue assertThat(tellMessage.isMatch(sender)).describedAs("match sender").isTrue assertThat(tellMessage.isMatch(recipient)).describedAs("match recipient").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 7b4c159..347b508 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntryLinkTest.kt @@ -61,19 +61,18 @@ class EntryLinkTest { assertThat(entryLink.comments.size).describedAs("getComments().size() == 5").isEqualTo(i) i = 0 for (comment in entryLink.comments) { - assertThat(comment.comment).describedAs("getComment($i)").isEqualTo("c$i") - assertThat(comment.nick).describedAs("getNick($i)").isEqualTo("u$i") + assertThat(comment).extracting("comment", "nick").containsExactly("c$i", "u$i") i++ } val r = SecureRandom() while (entryLink.comments.size > 0) { entryLink.deleteComment(r.nextInt(entryLink.comments.size)) } - assertThat(entryLink.comments.isNotEmpty()).describedAs("hasComments()").isFalse + assertThat(entryLink.comments).describedAs("hasComments()").isEmpty() entryLink.addComment("nothing", "nobody") entryLink.setComment(0, "something", "somebody") - assertThat(entryLink.getComment(0).nick).describedAs("getNick(somebody)").isEqualTo("somebody") - assertThat(entryLink.getComment(0).comment).describedAs("getComment(something)").isEqualTo("something") + assertThat(entryLink.getComment(0)).describedAs("get first comment").extracting("nick", "comment") + .containsExactly("somebody", "something") } @Test @@ -104,7 +103,7 @@ class EntryLinkTest { assertThat(tag.name).describedAs("tag.getName($i)").isEqualTo("tag" + (i + 1)) } assertThat(entryLink.tags.size).describedAs("getTags().size() is 5").isEqualTo(5) - assertThat(entryLink.tags.isNotEmpty()).describedAs("hasTags() is true").isTrue + assertThat(entryLink.tags).describedAs("hasTags() is true").isNotEmpty entryLink.setTags("-tag5") entryLink.setTags("+mobitopia") entryLink.setTags("tag4") diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/CryptoPricesTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/CryptoPricesTest.kt index 98a72cb..8d0c70a 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/CryptoPricesTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/CryptoPricesTest.kt @@ -43,13 +43,11 @@ class CryptoPricesTest { @Throws(ModuleException::class) fun testMarketPrice() { var price = currentPrice(listOf("BTC")) - assertThat(price.base).describedAs("is BTC").isEqualTo("BTC") - assertThat(price.currency).describedAs("is USD").isEqualTo("USD") - assertThat(price.amount.signum() > 0).describedAs("BTC > 0").isTrue + assertThat(price).extracting("base", "currency").containsExactly("BTC", "USD") + assertThat(price.amount.signum()).describedAs("BTC > 0").isGreaterThan(0) price = currentPrice(listOf("ETH", "EUR")) - assertThat(price.base).describedAs("is ETH").isEqualTo("ETH") - assertThat(price.currency).describedAs("is EUR").isEqualTo("EUR") - assertThat(price.amount.signum() > 0).describedAs("ETH > 0").isTrue + assertThat(price).extracting("base", "currency").containsExactly("ETH", "EUR") + assertThat(price.amount.signum()).describedAs("ETH > 0").isGreaterThan(0) } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.kt index 037ba4e..84fa190 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.kt @@ -57,8 +57,7 @@ class CurrencyConverterTest { assertThat(convertCurrency("100 USD").msg).describedAs("100 USD").contains("Invalid query.") val rates = currencyRates() assertThat(rates.size).describedAs("currencyRates.size == 33").isEqualTo(33) - assertThat(rates).describedAs("currencyRates(EUR)").contains("EUR: 1") - assertThat(rates.stream().anyMatch { it.matches("USD: .*".toRegex()) }) - .describedAs("currencyRates(USD)").isTrue + assertThat(rates).describedAs("currencyRates(EUR< USD)").contains("EUR: 1") + .anyMatch { it.matches("USD: .*".toRegex()) } } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt index 62397d9..28fd592 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/JokeTest.kt @@ -42,7 +42,7 @@ class JokeTest { @Test @Throws(ModuleException::class) fun testRandomJoke() { - assertThat(randomJoke().msg.isNotEmpty()).describedAs("randomJoke() > 0").isTrue + assertThat(randomJoke().msg).describedAs("randomJoke() > 0").isNotEmpty assertThat(randomJoke().msg).describedAs("randomJoke()").containsIgnoringCase("chuck") } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/LookupTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/LookupTest.kt index bc9c3d4..211afdd 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/LookupTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/LookupTest.kt @@ -51,7 +51,7 @@ class LookupTest { @Throws(Exception::class) fun testWhois() { val result = whois("17.178.96.59", Lookup.WHOIS_HOST) - assertThat(result.stream().anyMatch { m: String -> m.contains("Apple Inc.") }) - .describedAs("whois(17.178.96.59/Apple Inc.").isTrue + assertThat(result).describedAs("whois(17.178.96.59/Apple Inc.") + .anyMatch { it.contains("Apple Inc.") } } } 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 707f617..4ad980b 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt @@ -71,7 +71,8 @@ class ModuleExceptionTest { val apiKey = "1234567890" var e = ModuleException(debugMessage, message, IOException("URL http://foo.com?apiKey=$apiKey&userID=me")) assertThat(sanitizeException(e, apiKey, "", "me")).describedAs("sanitized url") - .hasMessageContainingAll("xxxxxxxxxx", "userID=xx").hasMessageNotContainingAny(apiKey, "me") + .hasMessageContainingAll("xxxxxxxxxx", "userID=xx", "java.io.IOException") + .hasMessageNotContainingAny(apiKey, "me") e = ModuleException(debugMessage, message, null) assertThat(sanitizeException(e, apiKey)).describedAs("no cause").hasMessage(message) 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 b1dc7c0..4eb4081 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt @@ -42,6 +42,10 @@ import org.testng.annotations.Test * The `StockQuoteTest` class. */ class StockQuoteTest : LocalProperties() { + private fun buildMatch(label: String): String { + return "${label}:[ ]+[0-9.]+".prependIndent() + } + @Test @Throws(ModuleException::class) fun testGetQuote() { @@ -49,10 +53,10 @@ class StockQuoteTest : LocalProperties() { try { 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: ".prependIndent()) - assertThat(messages[2].msg).describedAs("previous label").startsWith("Previous: ".prependIndent()) - assertThat(messages[3].msg).describedAs("open label").startsWith("Open: ".prependIndent()) + assertThat(messages[0].msg).describedAs("same stock symbol").matches("Symbol: AAPL .*") + assertThat(messages[1].msg).describedAs("price label").matches(buildMatch("Price")) + assertThat(messages[2].msg).describedAs("previous label").matches(buildMatch("Previous")) + assertThat(messages[3].msg).describedAs("open label").matches(buildMatch("Open")) try { getQuote("blahfoo", apiKey) } catch (e: ModuleException) { diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/WordTimeTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/WordTimeTest.kt index 15f0beb..49e63c1 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/WordTimeTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/WordTimeTest.kt @@ -48,16 +48,14 @@ class WordTimeTest { fun testTime() { assertThat(time("PST").msg).describedAs("PST").endsWith(bold("Los Angeles")) assertThat(time("BLAH").isError).describedAs("BLAH").isTrue - assertThat(time("BEAT").msg).describedAs(BEATS_KEYWORD).endsWith(BEATS_KEYWORD).contains("@") + assertThat(time("BEAT").msg).describedAs(BEATS_KEYWORD).matches("[\\w ]+: .?@\\d{3}+.? .beats") } @Test @Throws(ZoneRulesException::class) fun testCountries() { - COUNTRIES_MAP.toSortedMap().forEach { - if (it.value != BEATS_KEYWORD) { - ZoneId.of(it.value) - } + COUNTRIES_MAP.filter { it.value != BEATS_KEYWORD }.forEach { + ZoneId.of(it.value) } } }