-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise_test.go
79 lines (64 loc) · 1.45 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
73
74
75
76
77
78
79
package abuse
import (
"testing"
"github.com/stretchr/testify/assert"
)
func fooPromise() *Promise[string] {
return New(func() string {
return "foo"
})
}
func barPromise() *Promise[string] {
return New(func() string {
return "bar"
})
}
func TestSingle(t *testing.T) {
assert.Equal(t, "foo", fooPromise().Result())
}
func TestThen(t *testing.T) {
result := fooPromise().
Then(func(arg string) string {
return arg + "bar"
}).Result()
assert.Equal(t, "foobar", result)
}
func TestPanic(t *testing.T) {
p := New(func() string {
panic("aaa")
})
result, exception := p.Await()
assert.Equal(t, "", result)
assert.Equal(t, "aaa", exception)
}
func TestElse(t *testing.T) {
action1 := false
action2 := false
action3 := false
action4 := false
elseFlag := false
chain := New(func() string {
action1 = true
return "action1"
}).Then(func(string) string {
action2 = true
return "action2"
}).Then(func(string) string {
action3 = true
panic("aaa")
}).Then(func(string) string {
action4 = true
return "action4"
}).Else(func(interface{}) string {
elseFlag = true
return "else"
})
result, exception := chain.Await()
assert.Equal(t, "else", result, "result")
assert.Equal(t, nil, exception, "exception")
assert.Equal(t, true, action1, "action1")
assert.Equal(t, true, action2, "action2")
assert.Equal(t, true, action3, "action3")
assert.Equal(t, false, action4, "action4")
assert.Equal(t, true, elseFlag, "elseFlag")
}