-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
109 lines (90 loc) · 1.85 KB
/
handler_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
package gaw
import (
"context"
"errors"
"testing"
)
func TestNewHandler(t *testing.T) {
r := handle[string](context.Background(), func() (string, error) {
return "hello", nil
})
if r == nil {
t.Error("error: newHandler should return non nil")
}
}
func TestMultiHandlerHandleShouldReturnValue(t *testing.T) {
r1 := handle[string](context.Background(), func() (string, error) {
return "hello 1", nil
})
r2 := handle[string](context.Background(), func() (string, error) {
return "hello 2", nil
})
// the test cases
testCases := []struct {
result *Result[string]
want string
}{
{
result: r1,
want: "hello 1",
},
{
result: r2,
want: "hello 2",
},
}
for _, tc := range testCases {
tc.result.Await()
val := tc.result.Get()
if val != tc.want {
t.Error("error: handle val should match want")
}
}
}
func TestMultiHandlerHandleShouldReturnError(t *testing.T) {
r1 := handle[string](context.Background(), func() (string, error) {
return "hello 1", errors.New("error: r1")
})
r2 := handle[string](context.Background(), func() (string, error) {
return "", errors.New("error: r2")
})
// the test cases
testCases := []struct {
result *Result[string]
want bool
}{
{
result: r1,
want: true,
},
{
result: r2,
want: true,
},
}
for _, tc := range testCases {
tc.result.Await()
err := tc.result.Err()
if (err != nil) != tc.want {
t.Error("error: handle Err should return err")
}
}
}
func TestOneHandlerHandleShouldReturnValue(t *testing.T) {
r1 := handle[string](context.Background(), func() (string, error) {
return "hello 1", nil
})
// the test cases
tc := struct {
result *Result[string]
want string
}{
result: r1,
want: "hello 1",
}
tc.result.Await()
val := tc.result.Get()
if val != tc.want {
t.Error("error: handle val should match want")
}
}