-
Notifications
You must be signed in to change notification settings - Fork 7
/
StubTests.swift
195 lines (137 loc) · 4.95 KB
/
StubTests.swift
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// StubTests.swift
// InstantMock
//
// Created by Patrick on 08/05/2017.
// Copyright 2017 pirishd. All rights reserved.
//
import XCTest
@testable import InstantMock
final class StubTests: XCTestCase {
private var stub: Stub!
private var argsConfig: ArgumentsConfiguration!
override func setUp() {
super.setUp()
self.stub = Stub()
self.argsConfig = ArgumentsConfiguration([Argument]())
}
func testHandleCall_nil() {
let ret = try! self.stub.handleCall([])
XCTAssertNil(ret)
}
func testHandleCall_andReturn() {
self.stub.andReturn(36)
let ret = try! self.stub.handleCall([])
XCTAssertEqual(ret as! Int, 36)
}
func testReturns_andThrow() {
enum SomeError: Error {
case some
}
self.stub.andThrow(SomeError.some)
XCTAssertThrowsError(try self.stub.handleCall([])) { (error) -> Void in
XCTAssertEqual(error as? SomeError, SomeError.some)
}
}
func testHandleCall_andReturnClosure() {
self.stub.andReturn(closure: { _ in return 12 })
let ret = try! self.stub.handleCall([])
XCTAssertEqual(ret as! Int, 12)
}
func testHandleCall_andReturn_andReturnClosure() {
self.stub.andReturn(closure: { _ in return 12 }).andReturn(36)
let ret = try! self.stub.handleCall([])
XCTAssertEqual(ret as! Int, 36)
}
func testHandleCall_andDo() {
var something = ""
self.stub.andDo { _ in something = "not_empty" }
_ = try! self.stub.handleCall([])
XCTAssertEqual(something, "not_empty")
}
func testHandleCall_andReturn_andDo() {
var something = ""
self.stub.andDo({ _ in something = "not_empty" }).andReturn(36)
let ret = try! self.stub.handleCall([])
XCTAssertEqual(something, "not_empty")
XCTAssertEqual(ret as! Int, 36)
}
func testHandleCall_andReturnClosure_andDo() {
var something = ""
self.stub.andDo({ _ in something = "not_empty" }).andReturn(closure: { _ in return 12 })
let ret = try! self.stub.handleCall([])
XCTAssertEqual(something, "not_empty")
XCTAssertEqual(ret as! Int, 12)
}
func testHandleCall_capture() {
let captureMock = ArgumentCaptureMock()
let argsConfig = ArgumentsConfiguration([captureMock])
self.stub.configuration = CallConfiguration(for: "func", with: argsConfig)
_ = try! self.stub.handleCall(["Hello"])
XCTAssertEqual(captureMock.value as! String, "Hello")
}
func testReturns_false() {
let returns = self.stub.returns
XCTAssertFalse(returns)
}
func testReturns_andReturn() {
self.stub.andReturn(12)
let returns = self.stub.returns
XCTAssertTrue(returns)
}
func testReturns_andReturnClosure() {
self.stub.andReturn(closure: { _ in return 12})
let returns = self.stub.returns
XCTAssertTrue(returns)
}
func testBest_none() {
let list = [Stub]()
let best = list.best()
XCTAssertNil(best)
}
func testBest_one() {
let stub = Stub()
var list = [Stub]()
list.append(stub)
let best = list.best()
XCTAssertTrue(best === stub)
}
func testBest_several() {
let valueMock12 = ArgumentValueMock(12)
let valueMock37 = ArgumentValueMock(37)
let valueMock42 = ArgumentValueMock(42)
let stub1 = Stub()
stub1.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentAnyMock(), valueMock37, valueMock42]))
let stub2 = Stub()
stub2.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([valueMock12, valueMock37, valueMock42]))
var list = [Stub]()
list.append(stub1)
list.append(stub2)
let best = list.best()
XCTAssertTrue(best === stub2)
}
func testEqual() {
let stub1 = Stub()
stub1.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentValueMock(12)]))
let stub2 = Stub()
stub2.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentValueMock(13)]))
XCTAssertEqual(stub1, stub2)
}
func testEqual_defaultFailure() {
let stub1 = Stub()
let stub2 = Stub()
XCTAssertNotEqual(stub1, stub2)
}
func testGreaterThan() {
let stub1 = Stub()
stub1.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentValueMock(12), ArgumentValueMock(13)]))
let stub2 = Stub()
stub2.configuration = CallConfiguration(for: "func", with: ArgumentsConfiguration([ArgumentValueMock(12), ArgumentAnyMock()]))
XCTAssertGreaterThan(stub1, stub2)
}
func testGreaterThan_defaultFailure() {
let stub1 = Stub()
let stub2 = Stub()
XCTAssertFalse(stub1 > stub2)
}
}