-
Notifications
You must be signed in to change notification settings - Fork 18
/
promise_test.go
72 lines (62 loc) · 1.29 KB
/
promise_test.go
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
package channelx_test
import (
"errors"
"testing"
"time"
"github.com/Ksloveyuan/channelx"
)
func TestPromise(t *testing.T) {
promise := channelx.NewPromise(func() (interface{}, error) {
time.Sleep(1 * time.Second)
return 1, nil
})
res, _ := promise.Done()
if res != 1 {
t.Fail()
}
}
func TestPromise_ThenSuccess(t *testing.T) {
promise := channelx.NewPromise(func() (interface{}, error) {
time.Sleep(1 * time.Second)
return 1, nil
}).ThenSuccess(func(input interface{}) (interface{}, error) {
i := input.(int)
return i + 1, nil
})
res, _ := promise.Done()
if res != 2 {
t.Fail()
}
}
func TestPromise_ThenError(t *testing.T) {
promise := channelx.NewPromise(func() (interface{}, error) {
time.Sleep(1 * time.Second)
return nil, errors.New("test")
}).ThenError(func(err error) interface{} {
if err == nil {
t.Fail()
}
return 1
})
res, _ := promise.Done()
if res != 1 {
t.Fail()
}
}
func TestPromise_Then(t *testing.T) {
promise := channelx.NewPromise(func() (interface{}, error) {
time.Sleep(1 * time.Second)
return 1, nil
}).Then(func(input interface{}) (interface{}, error) {
return nil, errors.New("test")
}, func(err error) interface{} {
if err == nil {
t.Fail()
}
return 1
})
_, err := promise.Done()
if err == nil {
t.Fail()
}
}