diff --git a/indispensable/build.gradle.kts b/indispensable/build.gradle.kts index 998772c26..7336852dc 100644 --- a/indispensable/build.gradle.kts +++ b/indispensable/build.gradle.kts @@ -76,6 +76,12 @@ kotlin { } } + jvmTest { + dependencies { + implementation(kotlin("reflect")) + } + } + } } diff --git a/indispensable/src/commonTest/kotlin/CryptoSignatureTest.kt b/indispensable/src/commonTest/kotlin/CryptoSignatureTest.kt index 36e9753f0..a17391bbb 100644 --- a/indispensable/src/commonTest/kotlin/CryptoSignatureTest.kt +++ b/indispensable/src/commonTest/kotlin/CryptoSignatureTest.kt @@ -41,7 +41,7 @@ class CryptoSignatureTest : FreeSpec({ rsa1.hashCode() shouldNotBe rsa3.hashCode() val ec4 = ec3.guessCurve() - ec4.scalarByteLength shouldBe ECCurve.values().minOf { it.scalarLength.bytes } + ec4.scalarByteLength shouldBe ECCurve.entries.minOf { it.scalarLength.bytes } ec4 shouldBe ec4 ec4 shouldBe ec3 ec4.hashCode() shouldBe ec4.hashCode() @@ -81,7 +81,7 @@ class CryptoSignatureTest : FreeSpec({ sig3.scalarByteLength shouldBe sig1.scalarByteLength sig3.rawByteArray shouldBe encoded - val r2 = BigInteger.ONE.shl(ECCurve.values().maxOf { it.scalarLength.bits }.toInt() + 1) + val r2 = BigInteger.ONE.shl(ECCurve.entries.maxOf { it.scalarLength.bits }.toInt() + 1) shouldThrow { CryptoSignature.EC.fromRS(r2, s).guessCurve() } } }) diff --git a/indispensable/src/jvmTest/kotlin/at/asitplus/signum/indispensable/EnumConsistencyTests.kt b/indispensable/src/jvmTest/kotlin/at/asitplus/signum/indispensable/EnumConsistencyTests.kt new file mode 100644 index 000000000..fb0453414 --- /dev/null +++ b/indispensable/src/jvmTest/kotlin/at/asitplus/signum/indispensable/EnumConsistencyTests.kt @@ -0,0 +1,44 @@ +package at.asitplus.signum.indispensable + +import at.asitplus.signum.indispensable.mac.MAC +import io.kotest.core.spec.style.FreeSpec +import io.kotest.matchers.nulls.shouldNotBeNull +import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeInstanceOf +import java.util.Stack +import kotlin.reflect.KClass +import kotlin.reflect.KProperty1 +import kotlin.reflect.full.companionObject +import kotlin.reflect.full.memberProperties + +inline fun FreeSpec.enumConsistencyTest() { + T::class.simpleName!! { + val listed = T::class.companionObject!!.let { companion -> + @Suppress("UNCHECKED_CAST") + (companion.memberProperties.find { it.name == "entries" }.shouldNotBeNull() + as KProperty1).get(companion.objectInstance!!) + }.shouldBeInstanceOf>() + + val discovered = mutableSetOf() + val queue = Stack>().also { it.push(T::class) } + while (!queue.empty()) { + val cls = queue.pop() + if (cls.java.isEnum) { + discovered.addAll(cls.java.enumConstants!!) + continue + } + val o = cls.objectInstance + if (o != null) { + discovered.add(o) + continue + } + cls.sealedSubclasses.forEach(queue::push) + } + + listed.toSet() shouldBe discovered.toSet() + } +} + +class EnumConsistencyTests : FreeSpec({ + enumConsistencyTest() +}) \ No newline at end of file