diff --git a/Sources/fluentinterface/FluentInterface.swift b/Sources/fluentinterface/FluentInterface.swift index e719869..4eccb22 100644 --- a/Sources/fluentinterface/FluentInterface.swift +++ b/Sources/fluentinterface/FluentInterface.swift @@ -43,5 +43,20 @@ public postfix func - (lhs: FluentInterface) -> T { /// [Flentinterface] get the subject. public func unwrappingSubject() -> Subject { subject - } + } + /// [Fluentinterface] Quick way to touch subject and remain fluent interface + /// - Parameter handlel: A cloure to get the subject + public nonmutating func handlingSubject(_ handle: + (Subject) -> Void) -> Self { + handle(subject) + return self + } + /// [Fluentinterface] Quick way to manipulate subject and remain fluent interface + /// - Parameter handle: A cloure to inout set subject + public nonmutating func manipulateSubjct(_ handle: + (inout Subject) -> Void) -> Self { + var subject = self.subject + handle(&subject) + return FluentInterface(subject: subject) + } } diff --git a/Tests/FluentInterfaceTests/FluentinterfaceHandlerTests.swift b/Tests/FluentInterfaceTests/FluentinterfaceHandlerTests.swift new file mode 100644 index 0000000..7938ebd --- /dev/null +++ b/Tests/FluentInterfaceTests/FluentinterfaceHandlerTests.swift @@ -0,0 +1,36 @@ +import FluentInterface +import XCTest + +class FluentinterfaceHandlerTests: XCTestCase { + + func testHandlingSubject() { + class TestTarget { + var int:Int = 1 + func doubleIt() { + int *= 2 + } + } + let target = TestTarget()+ + .handlingSubject{$0.doubleIt()} + .unwrappingSubject() + + XCTAssertEqual(target.int, 2) + } + func testManipulateSubjct() { + struct TestTarget { + var int:Int = 1 + mutating func doubleIt() { + int *= 2 + } + } + let target = TestTarget()+ + .manipulateSubjct{$0.doubleIt()} + .unwrappingSubject() + + XCTAssertEqual(target.int, 2) + } + static var allTests = [ + ("testHandlingSubject", testHandlingSubject), + ("testManipulateSubjct",testManipulateSubjct), + ] +} diff --git a/Tests/FluentInterfaceTests/fluentinterfaceTests.swift b/Tests/FluentInterfaceTests/fluentinterfaceTests.swift new file mode 100644 index 0000000..e446e2d --- /dev/null +++ b/Tests/FluentInterfaceTests/fluentinterfaceTests.swift @@ -0,0 +1,8 @@ +// +// File.swift +// +// +// Created by 游宗諭 on 2020/3/3. +// + +import Foundation