Skip to content

Commit

Permalink
test: enrich test for Config
Browse files Browse the repository at this point in the history
  • Loading branch information
uchuhimo committed Dec 8, 2017
1 parent 983285c commit 1ad2404
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/kotlin/com/uchuhimo/konf/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,15 @@ private class ConfigImpl constructor(
}
is ValueState.Value -> valueState.value as T
is ValueState.Lazy<*> -> {
val value = valueState.thunk(lazyContext)!!
val value = try {
valueState.thunk(lazyContext)
} catch (exception: UnsetValueException) {
if (errorWhenUnset) {
throw exception
} else {
return null
}
}
if (item.type.rawClass.isInstance(value)) {
value as T
} else {
Expand Down Expand Up @@ -558,7 +566,7 @@ private class ConfigImpl constructor(

private sealed class ValueState {
object Unset : ValueState()
data class Lazy<T>(var thunk: (config: ItemContainer) -> T) : ValueState()
data class Lazy<T : Any>(var thunk: (config: ItemContainer) -> T) : ValueState()
data class Value(var value: Any) : ValueState()
}
}
23 changes: 23 additions & 0 deletions src/test/kotlin/com/uchuhimo/konf/ConfigSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ object ConfigSpek : SubjectSpek<Config>({
assertThat(newConfig[spec.type], equalTo(NetworkBuffer.Type.ON_HEAP))
assertThat(newConfig, equalTo(subject))
}
it("should not contain unset items in map") {
val config = Config { addSpec(spec) }
assertThat(config.toMap(), equalTo(mapOf(
spec.name.name to "buffer",
spec.type.name to NetworkBuffer.Type.OFF_HEAP)))
}
}
on("object methods") {
val map = mapOf(
spec.name.name to "buffer",
spec.type.name to NetworkBuffer.Type.OFF_HEAP)
it("should not equal to object of other class") {
assertFalse(subject.equals(1))
}
it("should equal to itself") {
assertThat(subject, equalTo(subject))
}
it("should have same hash code with map with same content") {
assertThat(subject.hashCode(), equalTo(map.hashCode()))
}
it("should convert to string in map-like format") {
assertThat(subject.toString(), equalTo("Config(items=$map)"))
}
}
group("get operation") {
on("get with valid item") {
Expand Down

0 comments on commit 1ad2404

Please sign in to comment.