From f1b50d741c4ea4b9b40192abc5df9afa6a0ef789 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 14 Nov 2021 15:37:26 -0800 Subject: [PATCH] Cleaned up messages classes. --- .../thauvin/erik/mobibot/msg/ErrorMessage.kt | 9 +--- .../net/thauvin/erik/mobibot/msg/Message.kt | 50 ++++++------------- .../thauvin/erik/mobibot/msg/NoticeMessage.kt | 10 ++-- .../erik/mobibot/msg/PrivateMessage.kt | 10 +--- .../thauvin/erik/mobibot/msg/PublicMessage.kt | 7 +-- .../thauvin/erik/mobibot/msg/TestMessage.kt | 31 +++++++++++- 6 files changed, 51 insertions(+), 66 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/msg/ErrorMessage.kt b/src/main/kotlin/net/thauvin/erik/mobibot/msg/ErrorMessage.kt index 35409f7..f071918 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/msg/ErrorMessage.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/msg/ErrorMessage.kt @@ -34,10 +34,5 @@ package net.thauvin.erik.mobibot.msg /** * The `ErrorMessage` class. */ -class ErrorMessage @JvmOverloads constructor(msg: String, color: String = DEFAULT_COLOR) : Message() { - init { - this.msg = msg - this.color = color - isError = true - } -} +class ErrorMessage @JvmOverloads constructor(msg: String, color: String = DEFAULT_COLOR) : + Message(msg, color, isError = true) 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 b410663..471c310 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/msg/Message.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/msg/Message.kt @@ -36,53 +36,31 @@ import net.thauvin.erik.semver.Constants /** * The `Message` class. */ -open class Message { +open class Message @JvmOverloads constructor( + var msg: String, + var color: String = DEFAULT_COLOR, + var isNotice: Boolean = false, + isError: Boolean = false, + var isPrivate: Boolean = false +) { companion object { var DEFAULT_COLOR = Constants.EMPTY + } - /** Message color. */ - var color = DEFAULT_COLOR + init { + if (isError) { + isNotice = true + } + } /** Error flag. */ - var isError = false + var isError = isError set(value) { if (value) isNotice = value field = value } - /** Notice flag. */ - var isNotice = false - - /** Private flag. */ - var isPrivate = false - - /** Message text. */ - var msg = "" - - /** Creates a new message. */ - constructor() { - // This constructor is intentionally empty - } - - /** - * Creates a new message. - */ - @JvmOverloads - constructor( - msg: String, - color: String = DEFAULT_COLOR, - isNotice: Boolean = false, - isError: Boolean = false, - isPrivate: Boolean = false - ) { - this.msg = msg - this.color = color - this.isNotice = isNotice - 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/main/kotlin/net/thauvin/erik/mobibot/msg/NoticeMessage.kt b/src/main/kotlin/net/thauvin/erik/mobibot/msg/NoticeMessage.kt index c9fa9dc..d2353b8 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/msg/NoticeMessage.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/msg/NoticeMessage.kt @@ -34,10 +34,6 @@ package net.thauvin.erik.mobibot.msg /** * The `NoticeMessage` class. */ -class NoticeMessage @JvmOverloads constructor(msg: String, color: String = DEFAULT_COLOR) : Message() { - init { - this.msg = msg - this.color = color - isNotice = true - } -} +class NoticeMessage @JvmOverloads constructor(msg: String, color: String = DEFAULT_COLOR) : + Message(msg, color, isNotice = true) + diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/msg/PrivateMessage.kt b/src/main/kotlin/net/thauvin/erik/mobibot/msg/PrivateMessage.kt index 2953b7a..7515eae 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/msg/PrivateMessage.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/msg/PrivateMessage.kt @@ -34,11 +34,5 @@ package net.thauvin.erik.mobibot.msg /** * The `PrivateMessage` class. */ -@Suppress("unused") -class PrivateMessage @JvmOverloads constructor(msg: String, color: String = DEFAULT_COLOR) : Message() { - init { - this.msg = msg - this.color = color - isPrivate = true - } -} +class PrivateMessage @JvmOverloads constructor(msg: String, color: String = DEFAULT_COLOR) : + Message(msg, color, isPrivate = true) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/msg/PublicMessage.kt b/src/main/kotlin/net/thauvin/erik/mobibot/msg/PublicMessage.kt index 397b3cf..9df2770 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/msg/PublicMessage.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/msg/PublicMessage.kt @@ -34,9 +34,4 @@ package net.thauvin.erik.mobibot.msg /** * The `PublicMessage` class. */ -class PublicMessage @JvmOverloads constructor(msg: String, color: String = DEFAULT_COLOR) : Message() { - init { - this.msg = msg - this.color = color - } -} +class PublicMessage @JvmOverloads constructor(msg: String, color: String = DEFAULT_COLOR) : Message(msg, color) 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 94a2de0..3d588c5 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/msg/TestMessage.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/msg/TestMessage.kt @@ -32,14 +32,17 @@ package net.thauvin.erik.mobibot.msg +import assertk.all import assertk.assertThat +import assertk.assertions.isFalse import assertk.assertions.isTrue +import assertk.assertions.prop import org.testng.annotations.Test class TestMessage { @Test fun testConstructor() { - var msg = Message() + var msg = Message("foo") msg.isError = true assertThat(msg.isNotice, "message is notice").isTrue() @@ -51,6 +54,30 @@ class TestMessage { @Test fun testErrorMessage() { val msg = ErrorMessage("foo") - assertThat(msg.isNotice, "error message is notice").isTrue() + assertThat(msg).all { + prop(Message::isError).isTrue() + prop(Message::isNotice).isTrue() + prop(Message::isPrivate).isFalse() + } + } + + @Test + fun testNoticeMessage() { + val msg = NoticeMessage("food") + assertThat(msg).all { + prop(Message::isError).isFalse() + prop(Message::isNotice).isTrue() + prop(Message::isPrivate).isFalse() + } + } + + @Test + fun testPublicMessage() { + val msg = PublicMessage("foo") + assertThat(msg).all { + prop(Message::isError).isFalse() + prop(Message::isNotice).isFalse() + prop(Message::isPrivate).isFalse() + } } }