Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Last active February 14, 2020 18:13
Show Gist options
  • Save IanKeen/ebaf3df426a082ad0421f6d905b5f676 to your computer and use it in GitHub Desktop.
Save IanKeen/ebaf3df426a082ad0421f6d905b5f676 to your computer and use it in GitHub Desktop.
FunctionBuilder mvp for a given/when/then test setup
test {
"Hello world"
given { print("GIVEN:", $0) }
when { print("WHEN:", $0) }
then { print("Then:", $0) }
}
import XCTest
@_functionBuilder
struct Test<T> {
var data: T
var given: given<T>
var when: when<T>
var then: then<T>
func execute() {
given.execute(data)
when.execute(data)
then.execute(data)
}
static func buildBlock(_ data: T, _ given: given<T>, _ when: when<T>, _ then: then<T>) -> Test {
return Test(data: data, given: given, when: when, then: then)
}
}
struct given<T> {
let execute: (T) -> Void
}
struct when<T> {
let execute: (T) -> Void
}
struct then<T> {
let execute: (T) -> Void
}
func test<T>(@Test<T> test: () -> Test<T>) {
test().execute()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment