forked from Thomvis/BrightFutures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultTests.swift
226 lines (184 loc) · 7.07 KB
/
ResultTests.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// The MIT License (MIT)
//
// Copyright (c) 2014 Thomas Visser
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import XCTest
import Result
import BrightFutures
extension Result {
var isSuccess: Bool {
return self.analysis(ifSuccess: { _ in return true }, ifFailure: { _ in return false })
}
var isFailure: Bool {
return !isSuccess
}
}
class ResultTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testSuccess() {
let result = Result<Int,NSError>(value: 3)
XCTAssert(result.isSuccess)
XCTAssertFalse(result.isFailure)
XCTAssertEqual(result.value!, 3)
XCTAssertNil(result.error)
let result1 = Result<Int,NSError>(value: 4)
XCTAssert(result1.isSuccess)
XCTAssertFalse(result1.isFailure)
XCTAssertEqual(result1.value!, 4)
XCTAssertNil(result1.error)
}
func testFailure() {
let error = NSError(domain: "TestDomain", code: 2, userInfo: nil)
let result = Result<Int, NSError>(error: error)
XCTAssert(result.isFailure)
XCTAssertFalse(result.isSuccess)
XCTAssertEqual(result.error!, error)
XCTAssertNil(result.value)
}
func testMapSuccess() {
let r = Result<Int,NSError>(value: 2).map { i -> Bool in
XCTAssertEqual(i, 2)
return i % 2 == 0
}
XCTAssertTrue(r.isSuccess)
XCTAssertEqual(r.value!, true)
}
func testMapFailure() {
let r = Result<Int, NSError>(error: NSError(domain: "error", code: 1, userInfo: nil)).map { i -> Int in
XCTAssert(false, "map should not get called if the result failed")
return i * 2
}
XCTAssert(r.isFailure)
XCTAssertEqual(r.error!.domain, "error")
}
func testFlatMapResultSuccess() {
let r = divide(20, 5).flatMap {
divide($0, 2)
}
XCTAssertEqual(r.value!, 2)
}
func testFlatMapResultFailure() {
let r = divide(20, 0).flatMap { i -> Result<Int, MathError> in
XCTAssert(false, "flatMap should not get called if the result failed")
return divide(i, 2)
}
XCTAssert(r.isFailure)
XCTAssertEqual(r.error!, MathError.DivisionByZero)
}
func testFlatMapFutureSuccess() {
let f = divide(100, 10).flatMap { i -> Future<Int, MathError> in
return future {
fibonacci(i)
}.promoteError()
}
let e = self.expectation()
f.onSuccess { i in
XCTAssertEqual(i, 55)
e.fulfill()
}
self.waitForExpectationsWithTimeout(2, handler: nil)
}
func testFlatMapFutureFailure() {
let f = divide(100, 0).flatMap { i -> Future<Int, MathError> in
XCTAssert(false, "flatMap should not get called if the result failed")
return future {
fibonacci(i)
}.promoteError()
}
let e = self.expectation()
f.onFailure { err in
XCTAssertEqual(err, MathError.DivisionByZero)
e.fulfill()
}
self.waitForExpectationsWithTimeout(2, handler: nil)
}
func testSequenceSuccess() {
let results: [Result<Int, MathError>] = (1...10).map { i in
return divide(123, i)
}
let result: Result<[Int], MathError> = results.sequence()
let outcome = [123, 61, 41, 30, 24, 20, 17, 15, 13, 12]
XCTAssertEqual(result.value!, outcome)
}
func testSequenceFailure() {
let results: [Result<Int, MathError>] = (-10...10).map { i in
return divide(123, i)
}
let r = results.sequence()
XCTAssert(r.isFailure)
XCTAssertEqual(r.error!, MathError.DivisionByZero)
}
func testRecoverNeeded() {
let r = divide(10, 0).recover(2)
XCTAssertEqual(r, 2)
XCTAssertEqual(divide(10, 0) ?? 2, 2)
}
func testRecoverUnneeded() {
let r = divide(10, 3).recover(10)
XCTAssertEqual(r, 3)
XCTAssertEqual(divide(10, 3) ?? 10, 3)
}
func testFlattenInnerSuccess() {
let fr = Result<Result<Int, NoError>, NoError>.Success(.Success(3)).flatten()
XCTAssert(fr.isSuccess)
XCTAssertEqual(fr.value, 3)
}
func testFlattenOuterFailure() {
let fr = Result<Result<Int, TestError>, TestError>.Failure(.JustAnError).flatten()
XCTAssert(fr.isFailure)
XCTAssertEqual(fr.error, .JustAnError)
}
func testFlattenInnerFailure() {
let fr = Result<Result<Int, TestError>, TestError>.Success(.Failure(.JustAnotherError)).flatten()
XCTAssert(fr.isFailure)
XCTAssertEqual(fr.error, .JustAnotherError)
}
func testFlattenFutureInResultSuccess() {
let f = Result<Future<Int, NoError>, NoError>.Success(Future<Int, NoError>(value: 1)).flatten()
XCTAssert(f.isSuccess)
XCTAssertEqual(f.value, 1)
}
func testFlattenFutureInResultFailed() {
let f = Result<Future<Int, TestError>, TestError>.Failure(.JustAnError).flatten()
XCTAssert(f.isFailure)
XCTAssertEqual(f.error, .JustAnError)
}
func testFlattenFailedFutureInSucceededResult() {
let f = Result<Future<Int, TestError>, TestError>.Success(Future<Int, TestError>(error: .JustAnotherError)).flatten()
XCTAssert(f.isFailure)
XCTAssertEqual(f.error, .JustAnotherError)
}
}
enum MathError: ErrorType {
case DivisionByZero
}
func divide(a: Int, _ b: Int) -> Result<Int, MathError> {
if (b == 0) {
return Result(error: .DivisionByZero)
}
return Result(value: a / b)
}