diff --git a/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt b/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt index fbb40263..83a50833 100644 --- a/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt +++ b/src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt @@ -6,6 +6,7 @@ class NoSuchOptionException(val option: String) : Exception("No such option: $op class IllegalValueException(val value: Any?) : Exception("Illegal value: $value") class InvalidTypeException(val got: String, val expected: String) : Exception("Invalid option value type: $got, expected $expected") +class RequirementNotMetException : Exception("null was passed into an option that requires a value") /** * A registry for an array of [PatchOption]s. @@ -72,6 +73,9 @@ sealed class PatchOption( ) { var value: T? = default set(value) { + if (value == null && required) { + throw RequirementNotMetException() + } if (!validator(value)) { throw IllegalValueException(value) } diff --git a/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt b/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt index 140c3c0e..10534303 100644 --- a/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt +++ b/src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt @@ -39,10 +39,10 @@ internal class PatchOptionsTest { @Test fun `should be able to set value to null`() { // Sadly, doing: - // > options["key1"] = null + // > options["key2"] = null // is not possible because Kotlin // cannot reify the type "Nothing?". - options.nullify("key1") + options.nullify("key2") } @Test @@ -65,4 +65,11 @@ internal class PatchOptionsTest { options["key3"] = "this value is not an allowed option" } } + + @Test + fun `should fail because of the requirement is not met`() { + assertThrows { + options.nullify("key1") + } + } } \ No newline at end of file