-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathInvocationDispatchingTests.swift
More file actions
173 lines (155 loc) · 5.59 KB
/
Copy pathInvocationDispatchingTests.swift
File metadata and controls
173 lines (155 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import XCTest
@testable import DSBridge
@Exposed
final class ExampleExposedInterface {
var f1Invoked = false
var f2InvokedWithParameter: String?
static var f3ReturnValue: String {
"F3"
}
static var f4ReturnValue: (String, Bool) {
("F4", true)
}
var f5PassedInParameter: Bool!
static var f5ReturnValue: (Int, Bool) {
(5, false)
}
@unexposed
func reset() {
f1Invoked = false
f2InvokedWithParameter = nil
f5PassedInParameter = nil
}
func f1() {
f1Invoked = true
}
func f2(parameter: String) {
f2InvokedWithParameter = parameter
}
func f3() -> String {
Self.f3ReturnValue
}
func f4(completion: (String, Bool) -> Void) {
completion(Self.f4ReturnValue.0, Self.f4ReturnValue.1)
}
func f41(completion: (String) -> Void) {
completion(Self.f4ReturnValue.0)
}
func f5(flag: Bool, completion: @escaping (Int, Bool) -> Void) {
f5PassedInParameter = flag
completion(Self.f5ReturnValue.0, Self.f5ReturnValue.1)
}
}
final class InvocationDispatchingTests: XCTestCase {
lazy var dispatcher: InvocationDispatcher = {
let dispatcher = InvocationDispatcher { [unowned self] asyncResponse in
self.asyncResponse = asyncResponse
}
dispatcher.addInterface(
exampleInterface, by: exampleNamespace
)
return dispatcher
}()
var asyncResponse: AsyncResponse!
let exampleNamespace = "exampleNamespace"
let exampleInterface = ExampleExposedInterface()
override func setUp() {
exampleInterface.reset()
asyncResponse = nil
}
func testInvokingNoParameterNoReturn() throws {
let returned = dispatcher.dispatch(
IncomingInvocation(
method: Method(
namespace: exampleNamespace, name: "f1"
),
signature: IncomingInvocation.Signature(parameter: nil, callbackFunctionName: nil)
)
)
XCTAssert(returned.code == .success)
XCTAssert(returned.data as! String == "")
XCTAssertTrue(exampleInterface.f1Invoked)
}
func testInvokingStringParameterNoReturn() throws {
let data = "string value"
let returned = dispatcher.dispatch(
IncomingInvocation(
method: Method(
namespace: exampleNamespace, name: "f2"
),
signature: IncomingInvocation.Signature(parameter: data, callbackFunctionName: nil)
)
)
XCTAssert(returned.code == .success)
XCTAssert(returned.data as! String == "")
XCTAssert(exampleInterface.f2InvokedWithParameter == data)
}
func testInvokingNonParameterReturn() throws {
let returned = dispatcher.dispatch(
IncomingInvocation(
method: Method(
namespace: exampleNamespace, name: "f3"
),
signature: IncomingInvocation.Signature(parameter: "string value", callbackFunctionName: nil)
)
)
XCTAssert(returned.data as! String == ExampleExposedInterface.f3ReturnValue)
}
func testInvokingCompletion() throws {
let returned = dispatcher.dispatch(
IncomingInvocation(
method: Method(
namespace: exampleNamespace, name: "f4"
),
signature: IncomingInvocation.Signature(
parameter: "string value",
callbackFunctionName: "funcInJS"
)
)
)
XCTAssert(returned.code == Response.empty.code)
XCTAssert(returned.data as! String == "")
XCTAssert(asyncResponse.functionName == "funcInJS")
XCTAssert(asyncResponse.data as! String == ExampleExposedInterface.f4ReturnValue.0)
XCTAssert(asyncResponse.completed == ExampleExposedInterface.f4ReturnValue.1)
}
func testInvokingSingleParameterCompletion() throws {
let returned = dispatcher.dispatch(
IncomingInvocation(
method: Method(
namespace: exampleNamespace, name: "f41"
),
signature: IncomingInvocation.Signature(
parameter: "string value",
callbackFunctionName: "funcInJS"
)
)
)
XCTAssert(returned.code == Response.empty.code)
XCTAssert(returned.data as! String == "")
XCTAssert(asyncResponse.functionName == "funcInJS")
XCTAssert(asyncResponse.data as! String == ExampleExposedInterface.f4ReturnValue.0)
XCTAssert(asyncResponse.completed == ExampleExposedInterface.f4ReturnValue.1)
}
func testInvokingParameterCompletion() throws {
let returned = dispatcher.dispatch(
IncomingInvocation(
method: Method(
namespace: exampleNamespace, name: "f5"
),
signature: IncomingInvocation.Signature(
parameter: true,
callbackFunctionName: "funcInJSF5"
)
)
)
XCTAssert(exampleInterface.f5PassedInParameter == true)
XCTAssert(returned.code == Response.empty.code)
XCTAssert(returned.data as! String == "")
XCTAssertEqual(asyncResponse.functionName, "funcInJSF5")
XCTAssert(
(asyncResponse.data as! Int, asyncResponse.completed)
== ExampleExposedInterface.f5ReturnValue
)
}
}