Skip to content

Instantly share code, notes, and snippets.

@johanhaleby
Created January 18, 2019 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johanhaleby/c230e21d086aecdf1ef76f2f397b1f17 to your computer and use it in GitHub Desktop.
Save johanhaleby/c230e21d086aecdf1ef76f2f397b1f17 to your computer and use it in GitHub Desktop.
An attempt to create an example to narrow down my question on Stackoverflow
// Fake Awaitility DSL
data class AwaitilityKtUntilFunCondition<T>(val factory: ConditionFactory, val fn: () -> T) {
infix fun has(pred: T.() -> Boolean) = factory.until(fn) { t: T? ->
if (t == null) {
false
} else {
pred(t)
}
}
}
class ConditionFactory {
fun <T : Any?> until(supplier: () -> T, predicate: (T) -> Boolean): T {
val result = supplier()
return if (predicate(result)) {
result
} else {
throw IllegalArgumentException("Supplied value is not matching predicate")
}
}
}
class Await {
infix fun <T> untilCallTo(supplier: () -> T): AwaitilityKtUntilFunCondition<T> {
val conditionFactory = ConditionFactory()
return AwaitilityKtUntilFunCondition(conditionFactory, supplier)
}
}
// Example
data class Data(var state: String)
interface DataRepository<T> {
fun loadData(): T
}
val nullableDataRepository: DataRepository<Data?> = TODO()
val nonNullableDataRepository: DataRepository<Data> = TODO()
// Test - A want both of these to compile!
val data1: Data = Await() untilCallTo { nonNullableDataRepository.loadData() } has {
state == "something"
}
val data2: Data = Await() untilCallTo { nullableDataRepository.loadData() } has {
state == "something"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment