forked from Brightify/Cuckoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassTest.swift
More file actions
196 lines (152 loc) · 5.3 KB
/
Copy pathClassTest.swift
File metadata and controls
196 lines (152 loc) · 5.3 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// ClassTest.swift
// Cuckoo
//
// Created by Filip Dolnik on 04.07.16.
// Copyright © 2016 Brightify. All rights reserved.
//
import XCTest
import Cuckoo
extension TestedClass: Mocked {
typealias MockType = MockTestedClass
}
class ClassTest: XCTestCase {
private var mock: MockTestedClass!
override func setUp() {
super.setUp()
mock = MockTestedClass()
}
func testReadOnlyPropertyWithMockCreator() {
let mock = createMock(for: TestedClass.self) { builder, stub in
when(stub.readOnlyProperty.get).thenReturn("a")
return MockTestedClass()
}
XCTAssertEqual(mock.readOnlyProperty, "a")
_ = verify(mock).readOnlyProperty.get
}
func testReadOnlyProperty() {
let mock = MockTestedClass()
stub(mock) { mock in
when(mock.readOnlyProperty.get).thenReturn("a")
}
XCTAssertEqual(mock.readOnlyProperty, "a")
_ = verify(mock).readOnlyProperty.get
}
func testReadWriteProperty() {
var called = false
stub(mock) { mock in
when(mock.readWriteProperty.get).thenReturn(1)
when(mock.readWriteProperty.set(anyInt())).then { _ in called = true }
}
mock.readWriteProperty = 0
XCTAssertEqual(mock.readWriteProperty, 1)
XCTAssertTrue(called)
_ = verify(mock).readWriteProperty.get
verify(mock).readWriteProperty.set(0)
}
func testOptionalProperty() {
var called = false
stub(mock) { mock in
when(mock.optionalProperty.get).thenReturn(nil)
when(mock.optionalProperty.set(anyInt())).then { _ in called = true }
}
mock.optionalProperty = 0
XCTAssertNil(mock.optionalProperty)
XCTAssertTrue(called)
_ = verify(mock).optionalProperty.get
verify(mock).optionalProperty.set(equal(to: 0))
}
func testNoReturn() {
var called = false
stub(mock) { mock in
when(mock.noReturn()).then { _ in called = true }
}
mock.noReturn()
XCTAssertTrue(called)
verify(mock).noReturn()
}
func testCountCharacters() {
stub(mock) { mock in
when(mock.count(characters: "a")).thenReturn(1)
}
XCTAssertEqual(mock.count(characters: "a"), 1)
verify(mock).count(characters: "a")
}
func testWithThrows() {
stub(mock) { mock in
when(mock.withThrows()).thenThrow(TestError.unknown)
}
var catched = false
do {
_ = try mock.withThrows()
} catch {
catched = true
}
XCTAssertTrue(catched)
verify(mock).withThrows()
}
func testWithNoReturnThrows() {
stub(mock) { mock in
when(mock.withNoReturnThrows()).thenThrow(TestError.unknown)
}
var catched = false
do {
try mock.withNoReturnThrows()
} catch {
catched = true
}
XCTAssertTrue(catched)
verify(mock).withNoReturnThrows()
}
func testWithClosure() {
stub(mock) { mock in
when(mock.withClosure(anyClosure())).then { $0("a") }
}
XCTAssertEqual(mock.withClosure { _ in 1 }, 1)
verify(mock).withClosure(anyClosure())
}
func testWithEscape() {
var called = false
stub(mock) { mock in
when(mock.withEscape(anyString(), action: anyClosure())).then { text, closure in closure(text) }
}
mock.withEscape("a") { called = $0 == "a" }
XCTAssertTrue(called)
verify(mock).withEscape(anyString(), action: anyClosure())
}
func testWithOptionalClosure() {
var called = false
stub(mock) { mock in
when(mock.withOptionalClosure(anyString(), closure: anyClosure())).then { text, closure in closure?(text) }
}
mock.withOptionalClosure("a") { called = $0 == "a" }
XCTAssertTrue(called)
verify(mock).withOptionalClosure(anyString(), closure: anyClosure())
}
func testWithLabel() {
var called = false
stub(mock) { mock in
when(mock.withLabelAndUnderscore(labelA: anyString(), anyString())).then { _ in called = true }
}
mock.withLabelAndUnderscore(labelA: "a", "b")
XCTAssertTrue(called)
verify(mock).withLabelAndUnderscore(labelA: anyString(), anyString())
}
func testCallingCountCharactersMethodWithHello() {
stub(mock) { mock in
when(mock.callingCountCharactersMethodWithHello()).thenCallRealImplementation()
when(mock.count(characters: any())).thenReturn(0)
}
XCTAssertEqual(mock.callingCountCharactersMethodWithHello(), 0)
verify(mock).callingCountCharactersMethodWithHello()
verify(mock).count(characters: "Hello")
}
func testDefaultImplCall() {
mock.enableDefaultImplementation(TestedClassStub())
XCTAssertEqual(mock.callingCountCharactersMethodWithHello(), 0)
verify(mock).callingCountCharactersMethodWithHello()
}
private enum TestError: Error {
case unknown
}
}