-
Notifications
You must be signed in to change notification settings - Fork 27
/
DeferredTests.swift
71 lines (58 loc) · 1.93 KB
/
DeferredTests.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@testable import Shared
import Deferred
import XCTest
// Trivial test for using Deferred.
class DeferredTests: XCTestCase {
func testDeferred() {
let d = Deferred<Int>()
XCTAssertNil(d.peek(), "Value not yet filled.")
let expectation = expectationWithDescription("Waiting on value.")
d.upon({ x in
expectation.fulfill()
})
d.fill(5)
waitForExpectationsWithTimeout(10) { (error) in
XCTAssertNil(error, "\(error)")
}
XCTAssertEqual(5, d.peek()!, "Value is filled.");
}
func testMultipleUponBlocks() {
let e1 = self.expectationWithDescription("First.")
let e2 = self.expectationWithDescription("Second.")
let d = Deferred<Int>()
d.upon { x in
XCTAssertEqual(x, 5)
e1.fulfill()
}
d.upon { x in
XCTAssertEqual(x, 5)
e2.fulfill()
}
d.fill(5)
waitForExpectationsWithTimeout(10, handler: nil)
}
func testOperators() {
let e1 = self.expectationWithDescription("First.")
let e2 = self.expectationWithDescription("Second.")
let f1: () -> Deferred<Maybe<Int>> = {
return deferMaybe(5)
}
let f2: (x: Int) -> Deferred<Maybe<String>> = {
if $0 == 5 {
e1.fulfill()
}
return deferMaybe("Hello!")
}
// Type signatures:
let combined: () -> Deferred<Maybe<String>> = { f1() >>== f2 }
let result: Deferred<Maybe<String>> = combined()
result.upon {
XCTAssertEqual("Hello!", $0.successValue!)
e2.fulfill()
}
waitForExpectationsWithTimeout(10, handler: nil)
}
}