mirror of
https://github.com/revanced/revanced-patcher.git
synced 2025-05-03 14:14:26 +02:00
fix: check if patch option requirement is met
This commit is contained in:
parent
35c6489dba
commit
14a73bfcaf
@ -6,6 +6,7 @@ class NoSuchOptionException(val option: String) : Exception("No such option: $op
|
|||||||
class IllegalValueException(val value: Any?) : Exception("Illegal value: $value")
|
class IllegalValueException(val value: Any?) : Exception("Illegal value: $value")
|
||||||
class InvalidTypeException(val got: String, val expected: String) :
|
class InvalidTypeException(val got: String, val expected: String) :
|
||||||
Exception("Invalid option value type: $got, expected $expected")
|
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.
|
* A registry for an array of [PatchOption]s.
|
||||||
@ -72,6 +73,9 @@ sealed class PatchOption<T>(
|
|||||||
) {
|
) {
|
||||||
var value: T? = default
|
var value: T? = default
|
||||||
set(value) {
|
set(value) {
|
||||||
|
if (value == null && required) {
|
||||||
|
throw RequirementNotMetException()
|
||||||
|
}
|
||||||
if (!validator(value)) {
|
if (!validator(value)) {
|
||||||
throw IllegalValueException(value)
|
throw IllegalValueException(value)
|
||||||
}
|
}
|
||||||
|
@ -39,10 +39,10 @@ internal class PatchOptionsTest {
|
|||||||
@Test
|
@Test
|
||||||
fun `should be able to set value to null`() {
|
fun `should be able to set value to null`() {
|
||||||
// Sadly, doing:
|
// Sadly, doing:
|
||||||
// > options["key1"] = null
|
// > options["key2"] = null
|
||||||
// is not possible because Kotlin
|
// is not possible because Kotlin
|
||||||
// cannot reify the type "Nothing?".
|
// cannot reify the type "Nothing?".
|
||||||
options.nullify("key1")
|
options.nullify("key2")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -65,4 +65,11 @@ internal class PatchOptionsTest {
|
|||||||
options["key3"] = "this value is not an allowed option"
|
options["key3"] = "this value is not an allowed option"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should fail because of the requirement is not met`() {
|
||||||
|
assertThrows<RequirementNotMetException> {
|
||||||
|
options.nullify("key1")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user