From 1a9e553013e3b8bffa6515f39a57e8f9fb678720 Mon Sep 17 00:00:00 2001 From: Pavel Punegov Date: Fri, 21 Apr 2023 16:51:26 +0200 Subject: [PATCH] [K/N][test] Set acceptable values for test class level properties Adds @AcceptablePropertyValues annotation that accepts comma-separated property values that could be used. If the value set from the command line or as an enforced one is not in the list the default value will be used instead. --- .../support/ConfigurationProperties.kt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/ConfigurationProperties.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/ConfigurationProperties.kt index 4e5e2872ac0..b84ef264217 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/ConfigurationProperties.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/blackboxtest/support/ConfigurationProperties.kt @@ -27,6 +27,9 @@ internal annotation class EnforcedProperty(val property: ClassLevelProperty, val @Target(AnnotationTarget.CLASS) internal annotation class EnforcedHostTarget +@Target(AnnotationTarget.CLASS) +internal annotation class AcceptablePropertyValues(val property: ClassLevelProperty, val acceptableValues: Array) + internal class EnforcedProperties(testClass: Class<*>) { private val enforcedAnnotations: Map = buildMap { testClass.annotations.forEach { annotation -> @@ -38,6 +41,15 @@ internal class EnforcedProperties(testClass: Class<*>) { } operator fun get(propertyType: ClassLevelProperty): String? = enforcedAnnotations[propertyType] + + private val acceptableAnnotations: Map> = testClass.annotations + .filterIsInstance() + .associate { + it.property to it.acceptableValues + } + + fun isAcceptableValue(propertyType: ClassLevelProperty, value: String?): Boolean = + acceptableAnnotations[propertyType]?.contains(value) ?: true } internal enum class ClassLevelProperty(shortName: String) { @@ -62,7 +74,8 @@ internal enum class ClassLevelProperty(shortName: String) { fun readValue(enforcedProperties: EnforcedProperties, transform: (String) -> T?, default: T): T { val propertyValue = enforcedProperties[this] ?: System.getProperty(propertyName) - return if (propertyValue != null) { + val acceptable = enforcedProperties.isAcceptableValue(this, propertyValue) + return if (propertyValue != null && acceptable) { transform(propertyValue) ?: fail { "Invalid value for $propertyName system property: $propertyValue" } } else default @@ -75,7 +88,8 @@ internal inline fun > ClassLevelProperty.readValue( default: E ): E { val optionName = enforcedProperties[this] ?: System.getProperty(propertyName) - return if (optionName != null) { + val acceptable = enforcedProperties.isAcceptableValue(this, optionName) + return if (optionName != null && acceptable) { values.firstOrNull { it.name == optionName } ?: fail { buildString { appendLine("Unknown ${E::class.java.simpleName} name $optionName.")