-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpromise_test.go
88 lines (76 loc) · 1.7 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
package go_promise
import (
"errors"
"testing"
)
func TestPromise_With(t *testing.T) {
t.Run("it should wrap a promise with then clause", func(t *testing.T) {
promise := Function(func() (int, error) {
return 10, nil
}).With(Then(func(value int) (float64, error) {
return float64(value), nil
}))
result, err := Await[float64](promise)
if err != nil {
t.Error("error is not expected")
}
if result != 10.0 {
t.Error("result is not 10")
}
})
t.Run("it should wrap a promise with catch clause", func(t *testing.T) {
promise := Function(func() (int, error) {
return 0, errors.New("error")
}).With(Catch(func(error) int {
return 10
}))
result, err := Await[int](promise)
if err != nil {
t.Error("error is not expected")
}
if result != 10 {
t.Error("result is not 10")
}
})
}
func TestPromise_Reset(t *testing.T) {
t.Run("it should not raise counter without reset", func(t *testing.T) {
counter := 0
promise := Function(func() (int, error) {
counter++
return 10, nil
})
for i := 0; i < 10; i++ {
result, err := Await[int](promise)
if err != nil {
t.Error("error is not expected")
}
if result != 10 {
t.Error("result is not 10")
}
if counter != 1 {
t.Error("counter is not 1")
}
}
})
t.Run("it should raise counter with reset", func(t *testing.T) {
counter := 0
promise := Function(func() (int, error) {
counter++
return 10, nil
})
for i := 0; i < 10; i++ {
result, err := Await[int](promise)
if err != nil {
t.Error("error is not expected")
}
if result != 10 {
t.Error("result is not 10")
}
if counter != i+1 {
t.Errorf("counter is not %d", i+1)
}
promise.Reset()
}
})
}