mirror of
https://github.com/mihonapp/mihon.git
synced 2025-10-24 20:18:53 +02:00
@@ -33,6 +33,7 @@ dependencies {
|
||||
|
||||
implementation(libs.unifile)
|
||||
|
||||
implementation(kotlinx.reflect)
|
||||
api(kotlinx.coroutines.core)
|
||||
api(kotlinx.serialization.json)
|
||||
api(kotlinx.serialization.json.okio)
|
||||
@@ -46,4 +47,6 @@ dependencies {
|
||||
|
||||
// JavaScript engine
|
||||
implementation(libs.bundles.js.engine)
|
||||
|
||||
testImplementation(libs.bundles.test)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package tachiyomi.core.util.lang
|
||||
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.full.declaredMemberProperties
|
||||
import kotlin.reflect.full.primaryConstructor
|
||||
|
||||
fun <T : Any> T.asBooleanArray(): BooleanArray {
|
||||
val constructorParams = this::class.primaryConstructor!!.parameters.map { it.name }
|
||||
val properties = this::class.declaredMemberProperties
|
||||
.filterIsInstance<KProperty1<T, Boolean>>()
|
||||
return constructorParams
|
||||
.map { param -> properties.find { it.name == param }!!.get(this) }
|
||||
.toBooleanArray()
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> BooleanArray.asDataClass(): T {
|
||||
val properties = T::class.declaredMemberProperties.filterIsInstance<KProperty1<T, Boolean>>()
|
||||
require(properties.size == this.size) { "Boolean array size does not match data class property count" }
|
||||
return T::class.primaryConstructor!!.call(*this.toTypedArray())
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package tachiyomi.core.util.lang
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertArrayEquals
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.junit.jupiter.api.parallel.Execution
|
||||
import org.junit.jupiter.api.parallel.ExecutionMode
|
||||
|
||||
@Execution(ExecutionMode.CONCURRENT)
|
||||
class BooleanArrayExtensionsTest {
|
||||
|
||||
@Test
|
||||
fun `converts to boolean array`() {
|
||||
assertArrayEquals(booleanArrayOf(true, false), TestClass(foo = true, bar = false).asBooleanArray())
|
||||
assertArrayEquals(booleanArrayOf(false, true), TestClass(foo = false, bar = true).asBooleanArray())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `throws error for invalid data classes`() {
|
||||
assertThrows<ClassCastException> {
|
||||
InvalidTestClass(foo = true, bar = "").asBooleanArray()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `converts from boolean array`() {
|
||||
assertEquals(booleanArrayOf(true, false).asDataClass<TestClass>(), TestClass(foo = true, bar = false))
|
||||
assertEquals(booleanArrayOf(false, true).asDataClass<TestClass>(), TestClass(foo = false, bar = true))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `throws error for invalid boolean array`() {
|
||||
assertThrows<IllegalArgumentException> {
|
||||
booleanArrayOf(true).asDataClass<TestClass>()
|
||||
}
|
||||
}
|
||||
|
||||
data class TestClass(
|
||||
val foo: Boolean,
|
||||
val bar: Boolean,
|
||||
)
|
||||
|
||||
data class InvalidTestClass(
|
||||
val foo: Boolean,
|
||||
val bar: String,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user