refactor: Simplify patch option tests

This commit is contained in:
oSumAtrIX 2023-10-22 02:46:51 +02:00
parent 55f6c2a9fc
commit 0caf6caeb9
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4

View File

@ -13,21 +13,31 @@ import kotlin.test.assertTrue
internal class PatchOptionsTest { internal class PatchOptionsTest {
@Test @Test
fun `should not fail because default value is unvalidated`() { fun `should not fail because default value is unvalidated`() {
assertDoesNotThrow { assertDoesNotThrow { OptionsTestPatch.requiredStringOption }
OptionsTestPatch.options["required"].value
} }
@Test
fun `should not allow setting custom value with validation`() {
// Getter validation on incorrect value.
assertThrows<PatchOptionException.ValueValidationException> { OptionsTestPatch.validatedOption }
// Setter validation on incorrect value.
assertThrows<PatchOptionException.ValueValidationException> { OptionsTestPatch.validatedOption = "invalid" }
// Setter validation on correct value.
assertDoesNotThrow { OptionsTestPatch.validatedOption = "valid" }
} }
@Test @Test
fun `should throw due to incorrect type`() { fun `should throw due to incorrect type`() {
assertThrows<PatchOptionException.InvalidValueTypeException> { assertThrows<PatchOptionException.InvalidValueTypeException> {
OptionsTestPatch.options["bool"] = 0 OptionsTestPatch.options["bool"] = "not a boolean"
} }
} }
@Test @Test
fun `should be nullable`() { fun `should be nullable`() {
OptionsTestPatch.options["bool"] = null OptionsTestPatch.booleanOption = null
} }
@Test @Test
@ -56,37 +66,18 @@ internal class PatchOptionsTest {
} }
@Test @Test
fun `should allow setting custom value`() { fun `should allow setting custom value`() =
assertDoesNotThrow { assertDoesNotThrow { OptionsTestPatch.stringOptionWithChoices = "unknown" }
OptionsTestPatch.options["choices"] = "unknown"
}
}
@Test @Test
fun `should not allow setting custom value with validation`() = fun `should allow resetting value`() = assertDoesNotThrow { OptionsTestPatch.stringOptionWithChoices = null }
@Suppress("UNCHECKED_CAST")
with(OptionsTestPatch.options["validated"] as PatchOption<String>) {
// Getter validation
assertThrows<PatchOptionException.ValueValidationException> { value }
// setter validation on incorrect value
assertThrows<PatchOptionException.ValueValidationException> { value = "invalid" }
// Setter validation on correct value
assertDoesNotThrow { value = "valid" }
}
@Suppress("unused")
private object OptionsTestPatch : BytecodePatch() { private object OptionsTestPatch : BytecodePatch() {
private var stringOption by stringPatchOption("string", "default") var booleanOption by booleanPatchOption("bool", true)
private var booleanOption by booleanPatchOption("bool", true) var requiredStringOption by stringPatchOption("required", "default", required = true)
private var requiredStringOption by stringPatchOption("required", "default", required = true) var stringArrayOption = stringArrayPatchOption("array", arrayOf("1", "2"))
private var nullDefaultRequiredOption by stringPatchOption("null", null, required = true) var stringOptionWithChoices by stringPatchOption("choices", "value", values = setOf("valid"))
var validatedOption by stringPatchOption("validated", "default") { it == "valid" }
val stringArrayOption = stringArrayPatchOption("array", arrayOf("1", "2"))
val stringOptionWithChoices = stringPatchOption("choices", "value", values = setOf("valid"))
val validatedOption = stringPatchOption("validated", "default") { it == "valid" }
override fun execute(context: BytecodeContext) {} override fun execute(context: BytecodeContext) {}
} }