This repository has been archived by the owner on Jul 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
/
PromiseTests.swift
118 lines (90 loc) · 3.21 KB
/
PromiseTests.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
//
// PromiseTests.swift
// BrightFutures
//
// Created by Thomas Visser on 16/10/15.
// Copyright © 2015 Thomas Visser. All rights reserved.
//
import XCTest
import BrightFutures
class PromiseTests: XCTestCase {
func testSuccessPromise() {
let p = Promise<Int, Never>()
DispatchQueue.global().async {
p.success(fibonacci(10))
}
let e = self.expectation(description: "complete expectation")
p.future.onComplete { result in
switch result {
case .success(let val):
XCTAssert(Int(55) == val)
case .failure(_):
XCTAssert(false)
}
e.fulfill()
}
self.waitForExpectations(timeout: 2, handler: nil)
}
func testFailurePromise() {
let p = Promise<Int, TestError>()
DispatchQueue.global().async {
p.tryFailure(TestError.justAnError)
}
let e = self.expectation(description: "complete expectation")
p.future.onComplete { result in
switch result {
case .success(_):
XCTFail("should not be success")
case .failure(let err):
XCTAssertEqual(err, TestError.justAnError)
}
e.fulfill()
}
self.waitForExpectations(timeout: 2, handler: nil)
}
func testCompletePromise() {
let p = Promise<Int, TestError>()
p.complete(.success(2))
XCTAssertEqual(p.future.value, 2)
}
func testPromiseCompleteWithSuccess() {
let p = Promise<Int, TestError>()
p.tryComplete(.success(2))
XCTAssert(p.future.isSuccess)
XCTAssert(p.future.forced() == Result<Int, TestError>(value:2))
}
func testPromiseCompleteWithFailure() {
let p = Promise<Int, TestError>()
p.tryComplete(.failure(.justAnError))
XCTAssert(p.future.isFailure)
XCTAssert(p.future.forced() == Result<Int, TestError>(error:TestError.justAnError))
}
func testPromiseTrySuccessTwice() {
let p = Promise<Int, Never>()
XCTAssert(p.trySuccess(1))
XCTAssertFalse(p.trySuccess(2))
XCTAssertEqual(p.future.forced().value!, 1)
}
func testPromiseTryFailureTwice() {
let p = Promise<Int, TestError>()
XCTAssert(p.tryFailure(TestError.justAnError))
XCTAssertFalse(p.tryFailure(TestError.justAnotherError))
XCTAssertEqual(p.future.forced().error!, TestError.justAnError)
}
func testPromiseCompleteWithSucceedingFuture() {
let p = Promise<Int, Never>()
let q = Promise<Int, Never>()
p.completeWith(q.future)
XCTAssert(!p.future.isCompleted)
q.success(1)
XCTAssertEqual(p.future.value, 1)
}
func testPromiseCompleteWithFailingFuture() {
let p = Promise<Int, TestError>()
let q = Promise<Int, TestError>()
p.completeWith(q.future)
XCTAssert(!p.future.isCompleted)
q.failure(.justAnError)
XCTAssertEqual(p.future.error, .justAnError)
}
}