-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise_test.go
124 lines (118 loc) · 3.34 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
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
119
120
121
122
123
124
package promise
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type testMessage struct {
Name string
Subject string
}
func TestPromiseThenExecution(t *testing.T) {
t.Run("Test then execution", func(t *testing.T) {
p := Promisify[testMessage](func(name string, subject string) (testMessage, error) {
return testMessage{
Name: name,
Subject: subject,
}, nil
}, "Someone famous", "Hi famous person")
p.Then(func(tm testMessage) {
assert.Equal(t, tm, testMessage{
Name: "Someone famous",
Subject: "Hi famous person",
})
})
p.Catch(func(err error) {
assert.Fail(t, "This should never be called")
})
})
t.Run("Test then execution and creates a new promise", func(t *testing.T) {
p := Promisify[testMessage](func(name string, subject string) (testMessage, error) {
return testMessage{
Name: name,
Subject: subject,
}, nil
}, "Someone famous", "Hi famous person")
p = Then(p, func(tm testMessage) (testMessage, error) {
assert.Equal(t, tm, testMessage{
Name: "Someone famous",
Subject: "Hi famous person",
})
return testMessage{
Name: "Another famous person",
Subject: "I am too famous to chat",
}, nil
})
obj, err := p.Await()
assert.Equal(t, obj, testMessage{
Name: "Another famous person",
Subject: "I am too famous to chat",
})
assert.NoError(t, err)
})
}
func TestPromiseWithCatchExecution(t *testing.T) {
t.Run("Executes Catch on error", func(t *testing.T) {
p := Promisify[testMessage](func(name string, subject string) (testMessage, error) {
return testMessage{}, fmt.Errorf("Famous people don't shake hands")
}, "Someone famous", "Hi famous person")
p.Then(func(tm testMessage) {
assert.Fail(t, "This block shouldn't execute")
})
p.Catch(func(err error) {
// Should call this function
assert.Error(t, err)
assert.Equal(t, err.Error(), "Famous people don't shake hands")
})
p.Exec()
})
t.Run("Executes Catch on error and creates a new promise", func(t *testing.T) {
p := Promisify[testMessage](func(name string, subject string) (testMessage, error) {
return testMessage{}, fmt.Errorf("Famous people don't shake hands")
}, "Someone famous", "Hi famous person")
p.Then(func(tm testMessage) {
assert.Fail(t, "This should never get called")
})
p1 := Catch(p, func(err error) (testMessage, error) {
assert.Error(t, err)
return testMessage{
Name: "Stunt Double",
Subject: err.Error(),
}, nil
})
msg, err := p1.Await()
assert.NoError(t, err)
assert.Equal(t, msg, testMessage{
Name: "Stunt Double",
Subject: "Famous people don't shake hands",
})
})
}
func TestPromiseWithFinallyExecution(t *testing.T) {
t.Run("Executes finally after then and catch", func(t *testing.T) {
recovery := func() {
if r := recover(); r != nil {
assert.Equal(t, r, "Should panic")
}
}
p := Promisify[testMessage](func(name string, subject string) (testMessage, error) {
return testMessage{
Name: name,
Subject: subject,
}, nil
}, "Someone famous", "Hi famous person")
p.Then(func(tm testMessage) {
assert.Equal(t, tm, testMessage{
Name: "Someone famous",
Subject: "Hi famous person",
})
})
p.Catch(func(err error) {
assert.Fail(t, "This should never get called")
})
p.Finally(func() {
defer recovery()
panic("Should panic")
})
})
}