Cleaned up messages classes.

This commit is contained in:
Erik C. Thauvin 2021-11-14 15:37:26 -08:00
parent 8efb09cea8
commit f1b50d741c
6 changed files with 51 additions and 66 deletions

View file

@ -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)

View file

@ -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')"
}

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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()
}
}
}