Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
more options
Browse files Browse the repository at this point in the history
  • Loading branch information
joehinkle11 committed Sep 19, 2021
1 parent 1a8e73f commit c5125c3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Tests/TypeWrapperTests/ExampleImplementations/MoreOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// MoreOptions.swift
//
//
// Created by Joseph Hinkle on 9/19/21.
//

import TypeWrapper

//
// implement for simple type (Bool) with more options
//
extension TypeWrapper {
func boolMoreOptions(_ options: _BoolExtraOptions) throws -> AnyWithTypeWrapper {
try self.send(options, as: {
($0 as? _Bool)?.onReceive(input:)
})
}
}
protocol _Bool {
func onReceive(input: Any) throws -> AnyWithTypeWrapper
}
struct _BoolExtraOptions {
let someBool: Any
let otherBool: Any
let op: String
}
extension AttemptIfConformsStruct: _Bool where Wrapped == Bool {
public func onReceive(input: Any) throws -> AnyWithTypeWrapper {
let options = input as! _BoolExtraOptions
if let someBool: Bool = options.someBool as? Bool,
let otherBool: Bool = options.otherBool as? Bool {
if options.op == "||" {
let result = someBool || otherBool
return addTypeWrapper(result)
} else if options.op == "&&" {
let result = someBool && otherBool
return addTypeWrapper(result)
} else {
fatalError("bad op")
}
} else {
fatalError("bad data")
}
}
}
16 changes: 16 additions & 0 deletions Tests/TypeWrapperTests/TypeWrapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,20 @@ final class TypeWrapperTests: XCTestCase {
XCTFail()
} catch {}
}
func testMoreOptionsExample() throws {
let bool1 = false
let bool2 = true

let and = bool1 && bool2
let or = bool1 || bool2

let anyBool1: AnyWithTypeWrapper = addTypeWrapper(bool1)
let anyBool2: AnyWithTypeWrapper = addTypeWrapper(bool2)

let andResult = try anyBool1.typeWrapper.boolMoreOptions(_BoolExtraOptions(someBool: anyBool1.any, otherBool: anyBool2.any, op: "&&"))
let orResult = try anyBool1.typeWrapper.boolMoreOptions(_BoolExtraOptions(someBool: anyBool1.any, otherBool: anyBool2.any, op: "||"))

XCTAssertEqual("\(and)", "\(andResult.any)")
XCTAssertEqual("\(or)", "\(orResult.any)")
}
}

0 comments on commit c5125c3

Please sign in to comment.