Skip to content

Instantly share code, notes, and snippets.

@polac24
Last active September 8, 2019 09:47
Show Gist options
  • Save polac24/160d9c5572b36d38cf3535eb35aec7d6 to your computer and use it in GitHub Desktop.
Save polac24/160d9c5572b36d38cf3535eb35aec7d6 to your computer and use it in GitHub Desktop.
enum Efect: Equatable {
case details(String,Int)
case simple(String)
case dummy
}
class EffectAction<AssociatedValues, Effect: Equatable>{
private let factory:(AssociatedValues)->(Effect)
private func associatedValues(of effect: Effect) -> AssociatedValues? {
let associatedValues = Mirror(reflecting: effect).children.first?.value
return (associatedValues ?? ()) as? AssociatedValues
}
func canAccept(_ effect: Effect) -> Bool {
if let associatedValues = associatedValues(of: effect) {
return factory(associatedValues) == effect
}
return false
}
init(_ factory: @escaping (AssociatedValues)->(Effect)) {
self.factory = factory
}
}
extension EffectAction where AssociatedValues == Void{
convenience init(_ effect:Effect){
self.init({_ in effect})
}
}
EffectAction(Efect.details).canAccept(Efect.simple(""))// false
EffectAction(Efect.details).canAccept(Efect.dummy)// false
EffectAction(Efect.details).canAccept(Efect.details("2", 0)) // true
EffectAction(Efect.dummy).canAccept(Efect.details("", 0))// false
EffectAction(Efect.dummy).canAccept(Efect.simple(""))// false
EffectAction(Efect.dummy).canAccept(Efect.dummy)// true
public func XCTAssertEnumType<T>(_ expression1: T, _ expression2: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) where T : Equatable{
XCTAssertEqual(expression1, expression2, message(), file: file, line: line)
}
public func XCTAssertNotEnumType<T>(_ expression1: T, _ expression2: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) where T : Equatable{
XCTAssertNotEqual(expression1, expression2, message(), file: file, line: line)
}
public func XCTAssertEnumType<T,I>(_ expression1: (I) -> T, _ expression2: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) where T : Equatable{
guard let associatedValues = Mirror(reflecting: expression2).children.first?.value as? I else {
XCTFail(message())
return
}
XCTAssertEnumType(expression1(associatedValues), expression2, message(), file: file, line: line)
}
public func XCTAssertEnumType<T,I>(_ expression2: T, _ expression1: (I) -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) where T : Equatable{
guard let associatedValues = Mirror(reflecting: expression2).children.first?.value as? I else {
XCTFail(message())
return
}
XCTAssertEnumType(expression1(associatedValues), expression2, message(), file: file, line: line)
}
public func XCTAssertNotEnumType<T,I>(_ expression1: (I) -> T, _ expression2: T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) where T : Equatable{
guard let associatedValues = Mirror(reflecting: expression2).children.first?.value as? I else {
XCTFail(message())
return
}
XCTAssertNotEnumType(expression1(associatedValues), expression2, message(), file: file, line: line)
}
public func XCTAssertNotEnumType<T,I>(_ expression2: T, _ expression1: (I) -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) where T : Equatable{
guard let associatedValues = Mirror(reflecting: expression2).children.first?.value as? I else {
XCTFail(message())
return
}
XCTAssertNotEnumType(expression1(associatedValues), expression2, message(), file: file, line: line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment