Skip to content

Instantly share code, notes, and snippets.

@jnutting
Created May 8, 2022 13:01
Show Gist options
  • Save jnutting/e65b0a0da359789122237fc5f3ce2427 to your computer and use it in GitHub Desktop.
Save jnutting/e65b0a0da359789122237fc5f3ce2427 to your computer and use it in GitHub Desktop.
//
// GivenExpectation.swift
//
// Created by JN on 2022-5-8.
//
import Foundation
import XCTest
class Given<T> {
var sut: T
init(_ sut: T) {
self.sut = sut
}
func the<U>(_ kp: WritableKeyPath<T, U>, is u: U) -> Given {
sut[keyPath: kp] = u
return self
}
func apply(_ f: (T) -> Void) -> Given {
f(sut)
return self
}
func map<U>(_ f: (T) -> U) -> Given<U> {
return Given<U>(f(sut))
}
var expect: Expectation<T> {
return Expectation(sut: sut)
}
}
struct Expectation<T> {
let sut: T
@discardableResult
func the<U: Equatable>(_ kp: KeyPath<T, U>,
toBe value: U) -> Expectation {
XCTAssertEqual(sut[keyPath: kp], value)
return self
}
@discardableResult
func the<U: Comparable>(_ kp: KeyPath<T, U>,
toBeGreaterThan value: U) -> Expectation {
XCTAssertGreaterThan(sut[keyPath: kp], value)
return self
}
@discardableResult
func the<U: Comparable>(_ kp: KeyPath<T, U>,
toBeLessThan value: U) -> Expectation {
XCTAssertLessThan(sut[keyPath: kp], value)
return self
}
@discardableResult
func the<U: Equatable>(_ kp: KeyPath<T, U>,
toNotBe value: U) -> Expectation {
XCTAssertNotEqual(sut[keyPath: kp], value)
return self
}
@discardableResult
func nilValueFor<U: Equatable>(_ kp: KeyPath<T, U?>) -> Expectation {
XCTAssertNil(sut[keyPath: kp])
return self
}
@discardableResult
func nonNilValueFor<U: Equatable>(_ kp: KeyPath<T, U?>) -> Expectation {
XCTAssertNotNil(sut[keyPath: kp])
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment