https://kotlinlang.org logo
Title
ł

Łukasz Bednarczyk

04/14/2023, 6:50 AM
hi 😉 I have a problem with test my sealed class.
sealed class EmbeddedRule : NormalizationRule {
    object SingleSpaceRule : EmbeddedRule() {
        override val ruleName: String
            get() = "single_space_rule"

        override fun normalize(value: String): Result<String> {
            val charIterator = value.iterator()
            val output = StringBuilder("")

            while (charIterator.hasNext()) {
                val currentChar = charIterator.nextChar()

                if (output.isEmpty())
                    output.append(currentChar)
                else
                    if (output.last().code != CharDirectionality.WHITESPACE.value && currentChar.code != CharDirectionality.WHITESPACE.value)
                        output.append(currentChar)
            }

            return Result.success(output.toString())
        }
    }

    companion object {
        fun values(): List<EmbeddedRule> {
            return EmbeddedRule::class.sealedSubclasses.mapNotNull { it.objectInstance }
        }

        fun findByRuleName(name: String): Result<EmbeddedRule> {
            return when (name) {
                UpperCase.ruleName -> Result.success(UpperCase)
                else -> Result.failure(IllegalArgumentException(name))
            }
        }
    }
}
My test code and exception:
context("SingleSpaceRule") {
        describe(".normalize") {
            it("returns only 1 spaces") {
                val result = EmbeddedRule.SingleSpaceRule.normalize("   ")

                result.isSuccess
                result.getOrThrow() == " "

            }
        }
    }

io.kotest.core.SpecInstantiationException: Could not create instance of class com.vascoelectronics.nlptool.normalizetext.domain.models.rules.EmbeddedRuleSpec
s

sam

04/14/2023, 2:25 PM
what's the constructor for EmbeddedRuleSpec
l

LeoColman

04/14/2023, 5:37 PM
Also, if possible, is there any more to that stacktrace?
ł

Łukasz Bednarczyk

04/16/2023, 6:25 PM
I decided to change sealed class to interface 😉 I don't have any problem now