Skip to content

Commit 617aa5d

Browse files
authored
add feature manipulateSubjct (#5)
* add handlingSubject method * add doc for handlingSubject * add manipulatesubject * add test case
1 parent bb18245 commit 617aa5d

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

Sources/fluentinterface/FluentInterface.swift

+16-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,20 @@ public postfix func - <T>(lhs: FluentInterface<T>) -> T {
4343
/// [Flentinterface] get the subject.
4444
public func unwrappingSubject() -> Subject {
4545
subject
46-
}
46+
}
47+
/// [Fluentinterface] Quick way to touch subject and remain fluent interface
48+
/// - Parameter handlel: A cloure to get the subject
49+
public nonmutating func handlingSubject(_ handle:
50+
(Subject) -> Void) -> Self {
51+
handle(subject)
52+
return self
53+
}
54+
/// [Fluentinterface] Quick way to manipulate subject and remain fluent interface
55+
/// - Parameter handle: A cloure to inout set subject
56+
public nonmutating func manipulateSubjct(_ handle:
57+
(inout Subject) -> Void) -> Self {
58+
var subject = self.subject
59+
handle(&subject)
60+
return FluentInterface(subject: subject)
61+
}
4762
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import FluentInterface
2+
import XCTest
3+
4+
class FluentinterfaceHandlerTests: XCTestCase {
5+
6+
func testHandlingSubject() {
7+
class TestTarget {
8+
var int:Int = 1
9+
func doubleIt() {
10+
int *= 2
11+
}
12+
}
13+
let target = TestTarget()+
14+
.handlingSubject{$0.doubleIt()}
15+
.unwrappingSubject()
16+
17+
XCTAssertEqual(target.int, 2)
18+
}
19+
func testManipulateSubjct() {
20+
struct TestTarget {
21+
var int:Int = 1
22+
mutating func doubleIt() {
23+
int *= 2
24+
}
25+
}
26+
let target = TestTarget()+
27+
.manipulateSubjct{$0.doubleIt()}
28+
.unwrappingSubject()
29+
30+
XCTAssertEqual(target.int, 2)
31+
}
32+
static var allTests = [
33+
("testHandlingSubject", testHandlingSubject),
34+
("testManipulateSubjct",testManipulateSubjct),
35+
]
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by 游宗諭 on 2020/3/3.
6+
//
7+
8+
import Foundation

0 commit comments

Comments
 (0)