Created
January 18, 2019 14:02
-
-
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
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
// 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