Skip to content

Commit

Permalink
add feature manipulateSubjct (#5)
Browse files Browse the repository at this point in the history
* add handlingSubject method

* add doc for handlingSubject

* add manipulatesubject

* add test case
  • Loading branch information
ytyubox committed Mar 3, 2020
1 parent bb18245 commit 617aa5d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Sources/fluentinterface/FluentInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,20 @@ public postfix func - <T>(lhs: FluentInterface<T>) -> 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)
}
}
36 changes: 36 additions & 0 deletions Tests/FluentInterfaceTests/FluentinterfaceHandlerTests.swift
Original file line number Diff line number Diff line change
@@ -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),
]
}
8 changes: 8 additions & 0 deletions Tests/FluentInterfaceTests/fluentinterfaceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// File.swift
//
//
// Created by 游宗諭 on 2020/3/3.
//

import Foundation

0 comments on commit 617aa5d

Please sign in to comment.