Skip to content

Instantly share code, notes, and snippets.

@robfletcher
Created June 30, 2016 23:34
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 robfletcher/4aebfbe034123ec4b624a04eb4fd6cca to your computer and use it in GitHub Desktop.
Save robfletcher/4aebfbe034123ec4b624a04eb4fd6cca to your computer and use it in GitHub Desktop.
fun <T1, T2> table(body: (T1, T2) -> Unit): TableBuilder2<T1, T2> {
return TableBuilder2(body)
}
fun <T1, T2, T3> table(body: (T1, T2, T3) -> Unit): TableBuilder3<T1, T2, T3> {
return TableBuilder3(body)
}
class TableBuilder2<T1, T2>(private val tableBody: (T1, T2) -> Unit) {
infix fun where(whereBody: RowBuilder.() -> Unit) {
RowBuilder().whereBody()
}
inner class RowBuilder {
fun row(col1: T1, col2: T2) = tableBody.invoke(col1, col2)
}
}
class TableBuilder3<T1, T2, T3>(private val tableBody: (T1, T2, T3) -> Unit) {
infix fun where(whereBody: RowBuilder.() -> Unit) {
RowBuilder().whereBody()
}
inner class RowBuilder {
fun row(col1: T1, col2: T2, col3: T3) {
tableBody.invoke(col1, col2, col3)
}
}
}
import org.jetbrains.spek.api.Spek
class TableSpec : Spek({
describe("using a table") {
table { a: String, b: Int ->
describe("$a and $b are parameters") {
it("should work") {
assert(a is String)
assert(b is Int)
}
}
} where {
row("foo", 1)
row("bar", 2)
}
}
table { a: Int, b: Int, c: Int ->
it("adds up $a and $b to equal $c") {
assert(a + b == c)
}
} where {
row(1, 2, 3)
row(4, 6, 10)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment