Skip to content

Commit

Permalink
move parameters into simple and polymorphic contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Feb 7, 2024
1 parent 13a6d21 commit 5b69739
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ interface SimpleClassFields {
var list: List<String>
var optional: String?
var optionalList: List<String>?
var mapped: Int
val listMapped: List<String>
var optionalMapped: Int?
var optionalMappedList: List<Int>?

}

Expand All @@ -38,6 +34,11 @@ interface PolymorphicClassFields : SimpleClassFields {

var value: MyType

val listMapped: List<String>
var mapped: Int
var optionalMapped: Int?
var optionalMappedList: List<Int>?

val optionalValue: MyType?
var optionalValueList: List<MyType>?
}
Expand All @@ -55,7 +56,6 @@ interface PolymorphicClassFieldsContract : SimpleClassFieldsContract {
fun `can read polymorphic values`() {
val input = container(
mapOf(
"string" to "string",
"boolean" to true,
"int" to 123,
"long" to Long.MAX_VALUE,
Expand All @@ -65,14 +65,9 @@ interface PolymorphicClassFieldsContract : SimpleClassFieldsContract {
"value" to 123,
"mapped" to "123",
"optionalValue" to 123,
"optional" to "optional",
)
)

expectThat(input.standardField).isEqualTo("foobar")

expectThat(input.string).isEqualTo("string")
expectThrows<NoSuchElementException> { container(mapOf()).string }.message.isEqualTo("Field <string> is missing")
expectThrows<NoSuchElementException> { input.notAnInt }.message.isEqualTo("Value for field <notAnInt> is not a class kotlin.Int but class kotlin.String")

expectThat(input.boolean).isEqualTo(true)
Expand All @@ -84,9 +79,6 @@ interface PolymorphicClassFieldsContract : SimpleClassFieldsContract {
expectThrows<ClassCastException> { container(mapOf("mapped" to 123)).mapped }
expectThat(input.value).isEqualTo(MyType.of(123))

expectThat(input.optional).isEqualTo("optional")
expectThat(container(mapOf()).optional).isNull()

expectThat(input.optionalValue).isEqualTo(MyType.of(123))
expectThat(container(mapOf()).optionalValue).isNull()
}
Expand All @@ -95,16 +87,13 @@ interface PolymorphicClassFieldsContract : SimpleClassFieldsContract {
fun `can write polymorphic values`() {
val input = container(
mapOf(
"string" to "string",
"boolean" to true,
"int" to 123,
"long" to Long.MAX_VALUE,
"double" to 1.1234,
"value" to 123,
"mapped" to "123",

"optionalValue" to 123,
"optional" to "optional",
)
)

Expand All @@ -119,8 +108,6 @@ interface PolymorphicClassFieldsContract : SimpleClassFieldsContract {
expectSetWorks(input::optional, null)
expectSetWorks(input::mapped, 123)
}


}

fun <T> expectSetWorks(prop: KMutableProperty0<T>, value: T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ class PropertiesDataContainerTest : SimpleClassFieldsContract {
override var standardField = "foobar"
override var string by required<String>(foo, bar)
override var list by list<String>(foo, bar)
override val listMapped by list(Int::toString, foo, bar)
override var mapped by required(String::toInt, Int::toString, foo, bar)

override var optional by optional<String>(foo, bar)
override var optionalMapped by optional(String::toInt, Int::toString, foo, bar)
override var optionalList by optionalList<String>(foo, bar)
override var optionalMappedList by optionalList(String::toInt, Int::toString, foo, bar)
}

override fun container(input: Map<String, Any?>) = PropertiesBacked(Properties().also { props ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,54 @@ import dev.forkhandles.data.DataContainer
import dev.forkhandles.data.PropertyMetadata
import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.api.expectThrows
import strikt.assertions.isEqualTo
import strikt.assertions.isNull
import strikt.assertions.message
import kotlin.reflect.full.starProjectedType

interface SimpleClassFieldsContract {
fun container(input: Map<String, Any?>): SimpleClassFields

@Test
fun `can read simple values`() {
val input = container(
mapOf(
"string" to "string",
"mapped" to "123",
"optional" to "optional",
)
)

expectThat(input.standardField).isEqualTo("foobar")

expectThat(input.string).isEqualTo("string")
expectThrows<NoSuchElementException> { container(mapOf()).string }.message.isEqualTo("Field <string> is missing")

expectThat(input.optional).isEqualTo("optional")
expectThat(container(mapOf()).optional).isNull()
}

@Test
fun `can write simple values`() {
val input = container(
mapOf(
"string" to "string",
"value" to 123,
"mapped" to "123",

"optionalValue" to 123,
"optional" to "optional",
)
)

expectSetWorks(input::standardField, "123")
expectSetWorks(input::string, "123")

expectSetWorks(input::optional, "123123")
expectSetWorks(input::optional, null)
}

@Test
fun `get meta data from the container`() {
val input = container(emptyMap()) as DataContainer<*>
Expand Down

0 comments on commit 5b69739

Please sign in to comment.