Last active
June 22, 2018 10:48
-
-
Save dimitris-c/6da4af1b1b6f978a52cf29ec4f1c9ed3 to your computer and use it in GitHub Desktop.
Nimble Matchers to work with RxTest and Events
This file contains 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
import Foundation | |
import RxSwift | |
import RxCocoa | |
import RxTest | |
import Nimble | |
// Nimble Matchers to work with RxTest and Events | |
public func equal<T: Equatable>(_ expectedValue: [Recorded<Event<T>>]) -> Predicate<[Recorded<Event<T>>]> { | |
return Predicate.define("equal <\(stringify(expectedValue))>") { actualExpression, msg in | |
guard let actualValue = try actualExpression.evaluate() else { | |
return PredicateResult( | |
status: .fail, | |
message: msg.appendedBeNilHint() | |
) | |
} | |
let left = expectedValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let right = actualValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let matches = left == right | |
return PredicateResult(bool: matches, message: msg) | |
} | |
} | |
public func equal<T: Equatable>(_ expectedValue: [Recorded<Event<T?>>]) -> Predicate<[Recorded<Event<T?>>]> { | |
return Predicate.define("equal <\(stringify(expectedValue))>") { actualExpression, msg in | |
guard let actualValue = try actualExpression.evaluate() else { | |
return PredicateResult( | |
status: .fail, | |
message: msg.appendedBeNilHint() | |
) | |
} | |
let left = expectedValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let right = actualValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let matches = left == right | |
return PredicateResult(bool: matches, message: msg) | |
} | |
} | |
public func equal<T: Equatable>(_ expectedValue: [MaybeEvent<T>]) -> Predicate<[MaybeEvent<T>]> { | |
return Predicate.define("equal <\(stringify(expectedValue))>") { actualExpression, msg in | |
guard let actualValue = try actualExpression.evaluate() else { | |
return PredicateResult( | |
status: .fail, | |
message: msg.appendedBeNilHint() | |
) | |
} | |
let left = expectedValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let right = actualValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let matches = left == right | |
return PredicateResult(bool: matches, message: msg) | |
} | |
} | |
public func equal<T: Equatable>(_ expectedValue: [SingleEvent<T>]) -> Predicate<[SingleEvent<T>]> { | |
return Predicate.define("equal <\(stringify(expectedValue))>") { actualExpression, msg in | |
guard let actualValue = try actualExpression.evaluate() else { | |
return PredicateResult( | |
status: .fail, | |
message: msg.appendedBeNilHint() | |
) | |
} | |
let left = expectedValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let right = actualValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let matches = left == right | |
return PredicateResult(bool: matches, message: msg) | |
} | |
} | |
public func equal(_ expectedValue: [CompletableEvent]) -> Predicate<[CompletableEvent]> { | |
return Predicate.define("equal <\(stringify(expectedValue))>") { actualExpression, msg in | |
guard let actualValue = try actualExpression.evaluate() else { | |
return PredicateResult( | |
status: .fail, | |
message: msg.appendedBeNilHint() | |
) | |
} | |
let left = expectedValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let right = actualValue.map { AnyEquatable(target: $0, comparer: ==) } | |
let matches = left == right | |
return PredicateResult(bool: matches, message: msg) | |
} | |
} | |
// Taken from RxTest - because RxTest doesn't expose this | |
struct AnyEquatable<Target> : Equatable { | |
typealias Comparer = (Target, Target) -> Bool | |
let _target: Target | |
let _comparer: Comparer | |
init(target: Target, comparer: @escaping Comparer) { | |
_target = target | |
_comparer = comparer | |
} | |
} | |
func == <T>(lhs: AnyEquatable<T>, rhs: AnyEquatable<T>) -> Bool { | |
return lhs._comparer(lhs._target, rhs._target) | |
} | |
extension AnyEquatable | |
: CustomDebugStringConvertible | |
, CustomStringConvertible { | |
var description: String { | |
return "\(_target)" | |
} | |
var debugDescription: String { | |
return "\(_target)" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment