-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
110 lines (99 loc) · 2.82 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
110
package pubsub_test
import (
"context"
"testing"
"github.com/botchris/go-pubsub"
"github.com/stretchr/testify/require"
)
func TestHandler(t *testing.T) {
tests := []struct {
name string
handlerFunc interface{}
panic error
}{
{
name: "compound interface",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m CompoundProtoMessageInterface) error { return nil },
panic: nil,
},
{
name: "message by value",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m EmptyMessage) error { return nil },
panic: nil,
},
{
name: "message by pointer",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m *EmptyMessage) error { return nil },
panic: nil,
},
{
name: "anything of custom interface",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m CustomInterface) error { return nil },
panic: nil,
},
{
name: "pointer to compound message",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m *CompoundMessage) error { return nil },
panic: nil,
},
{
name: "invalid context",
handlerFunc: func(ctx interface{}, t pubsub.Topic, m *CompoundMessage) error { return nil },
panic: pubsub.ErrHandlerInputNoContext,
},
{
name: "no return",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m *CompoundMessage) {},
panic: pubsub.ErrHandlerOutputLengthMissMatch,
},
{
name: "returns not of error kind",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m *CompoundMessage) *CompoundMessage { return nil },
panic: pubsub.ErrHandlerOutputNoError,
},
{
name: "returns compound error interface",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m *CompoundMessage) CompoundErrorInterface { return nil },
panic: nil,
},
{
name: "returns pointer to compound error interface",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m *CompoundMessage) *CompoundError { return nil },
panic: nil,
},
{
name: "returns value of compound error interface",
handlerFunc: func(ctx context.Context, t pubsub.Topic, m *CompoundMessage) CompoundError { return CompoundError{} },
panic: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
operation := func() {
pubsub.NewHandler(tt.handlerFunc)
}
if tt.panic != nil {
require.PanicsWithError(t, tt.panic.Error(), operation)
} else {
require.NotPanics(t, operation)
}
})
}
}
type CustomInterface interface {
private()
}
type CompoundProtoMessageInterface interface {
CustomInterface
}
type CompoundErrorInterface interface {
error
}
type CompoundError struct {
CompoundErrorInterface
}
type EmptyMessage struct {
}
type CompoundMessage struct {
EmptyMessage
}