Skip to content

Commit

Permalink
fix: check if patch option requirement is met
Browse files Browse the repository at this point in the history
  • Loading branch information
Sculas committed Aug 2, 2022
1 parent 35c6489 commit 14a73bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/app/revanced/patcher/patch/PatchOption.kt
Expand Up @@ -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.
Expand Down Expand Up @@ -72,6 +73,9 @@ sealed class PatchOption<T>(
) {
var value: T? = default
set(value) {
if (value == null && required) {
throw RequirementNotMetException()
}
if (!validator(value)) {
throw IllegalValueException(value)
}
Expand Down
11 changes: 9 additions & 2 deletions src/test/kotlin/app/revanced/patcher/patch/PatchOptionsTest.kt
Expand Up @@ -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
Expand All @@ -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<RequirementNotMetException> {
options.nullify("key1")
}
}
}

0 comments on commit 14a73bf

Please sign in to comment.