Created
May 5, 2022 21:57
-
-
Save jnutting/2d177f6a7760ba4a402790c6b3b3b01b to your computer and use it in GitHub Desktop.
A very small TDD DSL in Swift
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
class Given<T> { | |
var sut: T | |
init(_ sut: T) { | |
self.sut = sut | |
} | |
func the<U>(_ keyPath: WritableKeyPath<T, U>, is u: U) -> Given { | |
sut[keyPath: keyPath] = u | |
return self | |
} | |
func apply(_ c: (T) -> Void) -> Given { | |
c(sut) | |
return self | |
} | |
func map<U>(_ c: (T) -> U) -> Given<U> { | |
return Given<U>(c(sut)) | |
} | |
var expect: Expectation<T> { | |
return Expectation(sut: sut) | |
} | |
} | |
struct Expectation<T> { | |
let sut: T | |
@discardableResult | |
func the<U: Equatable>(_ keyPath: KeyPath<T, U>, | |
toBe value: U) -> Expectation { | |
XCTAssertEqual(sut[keyPath: keyPath], value) | |
return self | |
} | |
@discardableResult | |
func the<U: Comparable>(_ keyPath: KeyPath<T, U>, | |
toBeGreaterThan value: U) -> Expectation { | |
XCTAssertGreaterThan(sut[keyPath: keyPath], value) | |
return self | |
} | |
@discardableResult | |
func the<U: Comparable>(_ keyPath: KeyPath<T, U>, | |
toBeLessThan value: U) -> Expectation { | |
XCTAssertLessThan(sut[keyPath: keyPath], value) | |
return self | |
} | |
@discardableResult | |
func the<U: Equatable>(_ keyPath: KeyPath<T, U>, | |
toNotBe value: U) -> Expectation { | |
XCTAssertNotEqual(sut[keyPath: keyPath], value) | |
return self | |
} | |
@discardableResult | |
func nilValueFor<U: Equatable>(_ keyPath: KeyPath<T, U?>) -> Expectation { | |
XCTAssertNil(sut[keyPath: keyPath]) | |
return self | |
} | |
@discardableResult | |
func nonNilValueFor<U: Equatable>(_ keyPath: KeyPath<T, U?>) -> Expectation { | |
XCTAssertNotNil(sut[keyPath: keyPath]) | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment