Created
May 12, 2021 07:00
-
-
Save mikehearn/c27d47d67fc376c623b3f4784ee4bf14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hydraulic.utils.config | |
import com.natpryce.hamkrest.assertion.assertThat | |
import com.natpryce.hamkrest.containsSubstring | |
import com.typesafe.config.Config | |
import com.typesafe.config.ConfigException | |
import com.typesafe.config.ConfigFactory | |
import com.typesafe.config.ConfigValueFactory | |
import org.junit.jupiter.api.Assertions.assertEquals | |
import org.junit.jupiter.api.Assertions.assertNull | |
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 | |
import java.math.BigInteger | |
import java.net.URI | |
@Execution(ExecutionMode.CONCURRENT) | |
internal class ConfigReaderTest { | |
companion object { | |
val config: Config = ConfigFactory.parseString(""" | |
an-object { | |
some-value = 123 | |
other-value = [ 1, 2, 3 ] | |
great-url = foobar.com | |
} | |
good-errors = [] | |
""".trimIndent()) | |
} | |
@Test | |
@Suppress("UNUSED_VARIABLE") | |
fun propertyAccess() { | |
val anObject: Config by config.req() | |
val someValue: Int by anObject.req() | |
val otherValue: List<Int> by anObject.req() | |
val greatURL: URI by anObject.req() | |
assertEquals(123, someValue) | |
assertEquals(listOf(1, 2, 3), otherValue) | |
assertEquals(URI.create("https://foobar.com/"), greatURL) | |
val noSuchKey: String? by anObject.opt() | |
assertNull(noSuchKey) | |
assertThrows<ConfigException.WrongType> { val goodErrors: String by config.req() } | |
assertThrows<ConfigException.Missing> { val missing: Long by config.req() } | |
} | |
@Test | |
fun propertyTypeConversions() { | |
val anObject: Config by config.req() | |
var runTimes = 0 | |
val otherValue: List<BigInteger> by anObject.reqAndConvert { ints: List<Int> -> | |
runTimes++ | |
ints.map { it.toBigInteger() } | |
} | |
assertEquals(BigInteger.ONE, otherValue[0]) | |
assertEquals(1, runTimes) | |
assertEquals(BigInteger.TWO, otherValue[1]) | |
assertEquals(1, runTimes) | |
val optionalValue: URI? by anObject.optAndConvert { str: String -> URI(str) } | |
assertNull(optionalValue) | |
} | |
@Test | |
@Suppress("UNUSED_VARIABLE") | |
fun propertyAsserts() { | |
val anObject: Config by config.req() | |
val error: ConfigException.BadValue = assertThrows { | |
val otherValue by anObject.reqAndAlso<List<Int>> { nums -> require(nums.all { it < 0 }) } | |
} | |
assertThat(error.message!!, containsSubstring("other-value")) | |
} | |
@Test | |
fun renderToFlatProperties() { | |
val props = mapOf( | |
"str" to "value", | |
"num" to 123, | |
"bool" to true, | |
"map" to mapOf("a" to "1", "b" to 2), | |
"array" to listOf(1,2,3,4,5) | |
) | |
val config = props.entries.fold(ConfigFactory.empty()) { conf, (k, v) -> conf.withValue(k, ConfigValueFactory.fromAnyRef(v)) } | |
assertEquals(""" | |
array = [1,2,3,4,5] | |
bool = true | |
map.a = "1" | |
map.b = 2 | |
num = 123 | |
str = "value" | |
""".trimIndent(), config.renderToFlatProperties()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment