From 562adec17760842174548766b0c23527604b093d Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 5 Dec 2021 22:56:36 -0800 Subject: [PATCH] Added more tests. --- .../net/thauvin/erik/mobibot/AddonsTest.kt | 86 +++++++++++++++++ .../erik/mobibot/commands/RecapTest.kt | 13 ++- .../mobibot/commands/tell/TellMessageTest.kt | 3 - .../commands/tell/TellMessagesMgrTest.kt | 88 ++++++++++++++++++ .../erik/mobibot/entries/EntriesUtilsTest.kt | 92 +++++++++++++++++++ .../erik/mobibot/entries/FeedMgrTest.kt | 14 +-- .../thauvin/erik/mobibot/msg/TestMessage.kt | 10 ++ 7 files changed, 293 insertions(+), 13 deletions(-) create mode 100644 src/test/kotlin/net/thauvin/erik/mobibot/AddonsTest.kt create mode 100644 src/test/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessagesMgrTest.kt create mode 100644 src/test/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtilsTest.kt diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/AddonsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/AddonsTest.kt new file mode 100644 index 0000000..b618dc7 --- /dev/null +++ b/src/test/kotlin/net/thauvin/erik/mobibot/AddonsTest.kt @@ -0,0 +1,86 @@ +/* + * AddonsTest.kt + * + * Copyright (c) 2004-2021, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.thauvin.erik.mobibot + +import assertk.assertThat +import assertk.assertions.containsExactly +import assertk.assertions.isEqualTo +import net.thauvin.erik.mobibot.commands.ChannelFeed +import net.thauvin.erik.mobibot.commands.Cycle +import net.thauvin.erik.mobibot.commands.Die +import net.thauvin.erik.mobibot.commands.Ignore +import net.thauvin.erik.mobibot.commands.links.Comment +import net.thauvin.erik.mobibot.commands.links.View +import net.thauvin.erik.mobibot.modules.Joke +import net.thauvin.erik.mobibot.modules.RockPaperScissors +import net.thauvin.erik.mobibot.modules.Twitter +import org.testng.annotations.Test +import java.util.* + +class AddonsTest { + private val addons = Addons() + + @Test + fun addTest() { + val p = Properties() + + // Modules + addons.add(Joke(), p) + addons.add(RockPaperScissors(), p) + addons.add(Twitter(), p) // no properties, disabled. + assertThat(addons.modules.size, "modules = 2").isEqualTo(2) + + assertThat(addons.modulesNames, "module names").containsExactly("Joke", "RockPaperScissors") + + // Commands + addons.add(View(), p) + addons.add(Comment(), p) + addons.add(Cycle(), p) + addons.add(Die(), p) // invisible + addons.add(ChannelFeed("channel"), p) // no properties, disabled + addons.add(Ignore(), p.apply { put(Ignore.IGNORE_PROP, "nick") }) + assertThat(addons.commands.size, "commands = 4").isEqualTo(5) + + assertThat(addons.ops, "ops").containsExactly("cycle") + + assertThat(addons.names, "names").containsExactly( + "joke", + "rock", + "paper", + "scissors", + "view", + "comment", + "ignore" + ) + } +} diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/commands/RecapTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/commands/RecapTest.kt index d347f1a..2b5ebac 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/commands/RecapTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/commands/RecapTest.kt @@ -44,11 +44,18 @@ class RecapTest { @Test fun storeRecapTest() { for (i in 1..20) { - Recap.storeRecap("sender$i", "test $1", false) + Recap.storeRecap("sender$i", "test $i", false) } - assertThat(Recap.recaps).all { + assertThat(Recap.recaps, "recap first and last").all { size().isEqualTo(Recap.MAX_RECAPS) - prop(MutableList::last).contains("sender20") + prop(MutableList::first) + .matches("[1-2]\\d{3}-[01]\\d-[0-3]\\d [0-2]\\d:[0-6]\\d - sender11: test 11".toRegex()) + prop(MutableList::last) + .matches("[1-2]\\d{3}-[01]\\d-[0-3]\\d [0-2]\\d:[0-6]\\d - sender20: test 20".toRegex()) } + + Recap.storeRecap("sender", "test action", true) + assertThat(Recap.recaps.last()) + .matches("[1-2]\\d{3}-[01]\\d-[0-3]\\d [0-2]\\d:[0-6]\\d - sender test action".toRegex()) } } 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 808ea97..6618f90 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 @@ -68,9 +68,6 @@ class TellMessageTest { tellMessage.isReceived = false assertThat(tellMessage.receptionDate, "reception date not set").isEqualTo(LocalDateTime.MIN) tellMessage.isReceived = true - assertThat(tellMessage.isReceived, "is received").isTrue() assertThat(isValidDate(tellMessage.receptionDate), "received is valid date/time").isTrue() - tellMessage.isNotified = true - assertThat(tellMessage.isNotified, "is notified").isTrue() } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessagesMgrTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessagesMgrTest.kt new file mode 100644 index 0000000..97363cd --- /dev/null +++ b/src/test/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessagesMgrTest.kt @@ -0,0 +1,88 @@ +/* + * TellMessagesMgrTest.kt + * + * Copyright (c) 2004-2021, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.thauvin.erik.mobibot.commands.tell + +import assertk.all +import assertk.assertThat +import assertk.assertions.* +import org.testng.annotations.AfterClass +import org.testng.annotations.BeforeClass +import org.testng.annotations.Test +import java.time.LocalDateTime +import kotlin.io.path.createTempFile +import kotlin.io.path.deleteIfExists +import kotlin.io.path.fileSize + +class TellMessagesMgrTest { + private val testFile = createTempFile(suffix = ".ser") + private val maxDays = 10L + private val testMessages = mutableListOf().apply { + for (i in 0..5) { + this.add(i, TellMessage("sender$i", "recipient$i", "message $i")) + } + } + + @BeforeClass + fun saveTest() { + TellMessagesMgr.save(testFile.toAbsolutePath().toString(), testMessages) + assertThat(testFile.fileSize()).isGreaterThan(0) + } + + @AfterClass + fun afterClass() { + testFile.deleteIfExists() + } + + @Test + fun cleanTest() { + testMessages.add(TellMessage("sender", "recipient", "message").apply { + queued = LocalDateTime.now().minusDays(maxDays) + }) + val size = testMessages.size + assertThat(TellMessagesMgr.clean(testMessages, maxDays + 2), "maxDays = 12").isFalse() + assertThat(TellMessagesMgr.clean(testMessages, maxDays), "maxDays = 10").isTrue() + assertThat(testMessages.size, "size-- after clean").isEqualTo(size - 1) + } + + @Test + fun loadTest() { + val messages = TellMessagesMgr.load(testFile.toAbsolutePath().toString()) + for (i in messages.indices) { + assertThat(messages[i]).all { + prop(TellMessage::sender).isEqualTo(testMessages[i].sender) + prop(TellMessage::recipient).isEqualTo(testMessages[i].recipient) + prop(TellMessage::message).isEqualTo(testMessages[i].message) + } + } + } +} diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtilsTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtilsTest.kt new file mode 100644 index 0000000..ab4ec8e --- /dev/null +++ b/src/test/kotlin/net/thauvin/erik/mobibot/entries/EntriesUtilsTest.kt @@ -0,0 +1,92 @@ +/* + * EntriesUtilsTest.kt + * + * Copyright (c) 2004-2021, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.thauvin.erik.mobibot.entries + +import assertk.assertThat +import assertk.assertions.contains +import assertk.assertions.isEqualTo +import net.thauvin.erik.mobibot.Constants +import net.thauvin.erik.mobibot.entries.EntriesUtils.buildComment +import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLink +import net.thauvin.erik.mobibot.entries.EntriesUtils.buildLinkLabel +import net.thauvin.erik.mobibot.entries.EntriesUtils.buildTags +import org.testng.annotations.Test + +class EntriesUtilsTest { + private val comment = EntryComment("comment", "nick") + private val links = buildList { + for (i in 0..5) { + add( + EntryLink( + "https://www.mobitopia.org/$i", + "Mobitopia$i", + "Skynx$i", + "JimH$i", + "#mobitopia$i", + listOf("tag1", "tag2", "tag3", "TAG4", "Tag5") + ) + ) + } + } + + @Test + fun buildLinkLabelTest() { + assertThat(buildLinkLabel(1)).isEqualTo("${Constants.LINK_CMD}2") + } + + @Test + fun buildCommentTest() { + assertThat(buildComment(0, 0, comment)).isEqualTo("${Constants.LINK_CMD}1.1: [nick] comment") + } + + @Test + fun buildLinkTest() { + for (i in links.indices) { + assertThat( + buildLink(i - 1, links[i]), "link $i" + ).isEqualTo("L$i: [Skynx$i] \u0002Mobitopia$i\u0002 ( \u000303https://www.mobitopia.org/$i\u000F )") + } + + assertThat(links.first().addComment(comment), "add comment").isEqualTo(0) + assertThat(buildLink(0, links.first(), isView = true), "isView = true").contains("[+1]") + } + + @Test + fun buildTagsTest() { + for (i in links.indices) { + assertThat( + buildTags(i - 1, links[i]), "tag $i" + ).isEqualTo("L${i}T: Skynx$i, tag1, tag2, tag3, tag4, tag5") + } + } +} diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/entries/FeedMgrTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/entries/FeedMgrTest.kt index 04c36a7..271fc4c 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/entries/FeedMgrTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/entries/FeedMgrTest.kt @@ -81,13 +81,13 @@ class FeedMgrTest { with(entries.links.first()) { assertThat(nick, "first nick").isEqualTo("ErikT") assertThat(date, "first date").isEqualTo(Date(1635638400000L)) - comments.forEachIndexed { i, entryComment -> - assertThat(entryComment.comment, "comment ${i + 1}").endsWith("comment ${i + 1}.") - if (i == 0) { - assertThat(entryComment.nick, "comment ${i + 1} nick").isEqualTo("ErikT") - } else { - assertThat(entryComment.nick, "comment ${i + 1} nick").isEqualTo("Skynx") - } + assertThat(comments.first(), "first comment").all { + prop(EntryComment::comment).endsWith("comment 1.") + prop(EntryComment::nick).isEqualTo("ErikT") + } + assertThat(comments.last(), "last comment").all { + prop(EntryComment::comment).endsWith("comment 2.") + prop(EntryComment::nick).isEqualTo("Skynx") } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/msg/TestMessage.kt b/src/test/kotlin/net/thauvin/erik/mobibot/msg/TestMessage.kt index 3d588c5..50f8843 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/msg/TestMessage.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/msg/TestMessage.kt @@ -71,6 +71,16 @@ class TestMessage { } } + @Test + fun testPrivateMessage() { + val msg = PrivateMessage("foo") + assertThat(msg).all { + prop(Message::isPrivate).isTrue() + prop(Message::isError).isFalse() + prop(Message::isNotice).isFalse() + } + } + @Test fun testPublicMessage() { val msg = PublicMessage("foo")