-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathBasicTests.swift
More file actions
75 lines (52 loc) · 2.07 KB
/
BasicTests.swift
File metadata and controls
75 lines (52 loc) · 2.07 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
//
// BasicTests.swift
// SwiftTask
//
// Created by Yasuhiro Inami on 2014/08/27.
// Copyright (c) 2014年 Yasuhiro Inami. All rights reserved.
//
import SwiftTask
import Async
import XCTest
class BasicTests: _TestCase
{
func testExample()
{
typealias Task = SwiftTask.Task<Float, String, ErrorString>
let expect = self.expectation(description: #function)
// define task
let task = Task { progress, fulfill, reject, configure in
Async.main(after: 0.1) {
progress(0.0)
progress(1.0)
if arc4random_uniform(2) == 0 {
fulfill("OK")
}
else {
reject("ERROR")
}
}
return
}
task.progress { oldProgress, newProgress in
print("progress = \(newProgress)")
}.success { value -> String in // `task.success {...}` = JavaScript's `promise.then(onFulfilled)`
XCTAssertEqual(value, "OK")
return "Now OK"
}.failure { error, isCancelled -> String in // `task.failure {...}` = JavaScript's `promise.catch(onRejected)`
XCTAssertEqual(error!, "ERROR")
return "Now RECOVERED"
}.then { value, errorInfo -> Task in // `task.then {...}` = JavaScript's `promise.then(onFulfilled, onRejected)`
print("value = \(value ?? "")") // either "Now OK" or "Now RECOVERED"
XCTAssertTrue(value!.hasPrefix("Now"))
XCTAssertTrue(errorInfo == nil)
return Task(error: "ABORT")
}.then { value, errorInfo -> Void in
print("errorInfo = \(errorInfo ?? "")")
XCTAssertTrue(value == nil)
XCTAssertEqual(errorInfo!.error!, "ABORT")
expect.fulfill()
}
self.wait()
}
}