161 lines
No EOL
5.1 KiB
Kotlin
161 lines
No EOL
5.1 KiB
Kotlin
package nl.kallestruik.dlib.config
|
|
|
|
import kotlinx.serialization.Serializable
|
|
import kotlinx.serialization.json.JsonPrimitive
|
|
import nl.kallestruik.dlib.config.annotations.*
|
|
import kotlin.test.Test
|
|
import kotlin.test.assertEquals
|
|
|
|
|
|
class ConfigSchemaBuilderTest {
|
|
|
|
@Test
|
|
fun `config scheme contains all descriptions`() {
|
|
val schema = Company.serializer().descriptor.toConfigSchema()
|
|
|
|
assertEquals(
|
|
listOf("The name of the company."),
|
|
schema.children!!["name"]!!.description
|
|
)
|
|
assertEquals(
|
|
listOf("The person that owns the company."),
|
|
schema.children!!["owner"]!!.description
|
|
)
|
|
assertEquals(
|
|
listOf("All the employees of the company.", "This list should also contain the owner for legacy reasons ;)."),
|
|
schema.children!!["employees"]!!.description
|
|
)
|
|
|
|
val personSchema = schema.children!!["owner"]!!
|
|
|
|
assertEquals(
|
|
listOf("The list of qualifications that this person has."),
|
|
personSchema.children!!["qualifications"]!!.description
|
|
)
|
|
assertEquals(
|
|
listOf("The age of the person."),
|
|
personSchema.children!!["age"]!!.description
|
|
)
|
|
assertEquals(
|
|
listOf("Whether the person is currently employed at a company."),
|
|
personSchema.children!!["isEmployed"]!!.description
|
|
)
|
|
}
|
|
|
|
@Test
|
|
fun `config schema has correct defaults`() {
|
|
val schema = Company.serializer().descriptor.toConfigSchema()
|
|
val personSchema = schema.children!!["owner"]!!
|
|
|
|
assertEquals(JsonPrimitive("Bob"), personSchema.children!!["firstName"]!!.default)
|
|
assertEquals(JsonPrimitive(18.0), personSchema.children!!["age"]!!.default)
|
|
assertEquals(JsonPrimitive(true), personSchema.children!!["isEmployed"]!!.default)
|
|
|
|
val qualificationSchema = personSchema.children!!["qualifications"]!!.items!!
|
|
|
|
assertEquals(JsonPrimitive("EXPERIENCE"), qualificationSchema.children!!["type"]!!.default)
|
|
assertEquals(JsonPrimitive(0), qualificationSchema.children!!["yearObtained"]!!.default)
|
|
}
|
|
|
|
@Test
|
|
fun `config schema allowed keys are correct`() {
|
|
val schema = Company.serializer().descriptor.toConfigSchema()
|
|
|
|
assertEquals(
|
|
listOf(listOf("Bob"), listOf("Joe"), listOf("Kon"), listOf("Jane", "Kevin")),
|
|
schema.children!!["employees"]!!.constraints.allowedKeys
|
|
)
|
|
}
|
|
|
|
@Test
|
|
fun `config schema enum values are correct`() {
|
|
val schema = Company.serializer().descriptor.toConfigSchema()
|
|
val personSchema = schema.children!!["owner"]!!
|
|
|
|
assertEquals(
|
|
listOf("Bob", "Joe", "Kon", "Jane", "Kevin"),
|
|
personSchema.children!!["firstName"]!!.constraints.enum
|
|
)
|
|
|
|
val qualificationSchema = personSchema.children!!["qualifications"]!!.items!!
|
|
|
|
assertEquals(
|
|
listOf("DIPLOMA", "EXPERIENCE", "CERTIFICATE"),
|
|
qualificationSchema.children!!["type"]!!.constraints.enum
|
|
)
|
|
}
|
|
|
|
@Test
|
|
fun `config schema has correct min and max values`() {
|
|
val schema = Company.serializer().descriptor.toConfigSchema()
|
|
val personSchema = schema.children!!["owner"]!!
|
|
|
|
assertEquals(
|
|
0.0,
|
|
personSchema.children!!["age"]!!.constraints.minValueFloat
|
|
)
|
|
assertEquals(
|
|
75.0,
|
|
personSchema.children!!["age"]!!.constraints.maxValueFloat
|
|
)
|
|
|
|
val qualificationSchema = personSchema.children!!["qualifications"]!!.items!!
|
|
|
|
assertEquals(
|
|
0,
|
|
qualificationSchema.children!!["yearObtained"]!!.constraints.minValueInt
|
|
)
|
|
assertEquals(
|
|
9999,
|
|
qualificationSchema.children!!["yearObtained"]!!.constraints.maxValueInt
|
|
)
|
|
}
|
|
}
|
|
|
|
@Serializable
|
|
data class Company(
|
|
@Description("The name of the company.")
|
|
val name: String,
|
|
@Description("The person that owns the company.")
|
|
val owner: Person,
|
|
@Description("All the employees of the company.", "This list should also contain the owner for legacy reasons ;).")
|
|
@AllowedKeys("Bob", "Joe", "Kon") // Don't question it. I need to test stuff.
|
|
@AllowedKeysExclusive("Jane", "Kevin")
|
|
val employees: Map<String, Person>,
|
|
)
|
|
|
|
@Serializable
|
|
data class Person(
|
|
@DefaultString("Bob")
|
|
@StringEnum("Bob", "Joe", "Kon", "Jane", "Kevin")
|
|
val firstName: String,
|
|
val lastName: String,
|
|
@Description("The list of qualifications that this person has.")
|
|
val qualifications: List<Qualification>,
|
|
@DefaultFloat(18.0)
|
|
@MinFloat(0.0)
|
|
@MaxFloat(75.0)
|
|
@Description("The age of the person.")
|
|
val age: Float,
|
|
@DefaultBoolean(true)
|
|
@Description("Whether the person is currently employed at a company.")
|
|
val isEmployed: Boolean,
|
|
)
|
|
|
|
@Serializable
|
|
data class Qualification(
|
|
val name: String,
|
|
@DefaultString("EXPERIENCE")
|
|
val type: QualificationType,
|
|
@DefaultInt(0)
|
|
@MinInt(0)
|
|
@MaxInt(9999)
|
|
val yearObtained: Int,
|
|
)
|
|
|
|
@Serializable
|
|
enum class QualificationType {
|
|
DIPLOMA,
|
|
EXPERIENCE,
|
|
CERTIFICATE,
|
|
} |