-
Notifications
You must be signed in to change notification settings - Fork 4
/
event_test.go
89 lines (73 loc) · 1.7 KB
/
event_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
package frizzle_test
import (
"testing"
"github.com/qntfy/frizzle"
"github.com/stretchr/testify/assert"
)
type notEventer struct{}
func TestEventsNotImpl(t *testing.T) {
_, ok := interface{}(¬Eventer{}).(frizzle.Eventer)
if ok {
t.Error("notEventer should not implement frizzle.Eventer")
}
tests := [][]interface{}{
{},
{¬Eventer{}},
{(frizzle.Eventer)(nil)},
{¬Eventer{}, nil},
}
for _, test := range tests {
ch := frizzle.InitEvents(test...)
if _, ok := <-ch; ok {
t.Errorf("InitEvents should return closed channel for: %v", t)
}
}
}
type eventer struct {
evts chan frizzle.Event
}
func newEventer(size int, str string) *eventer {
e := &eventer{
evts: make(chan frizzle.Event, size),
}
for i := 0; i < size; i++ {
e.evts <- frizzle.NewError(str)
}
close(e.evts)
return e
}
func (e *eventer) Events() <-chan frizzle.Event {
return (<-chan frizzle.Event)(e.evts)
}
var (
_ frizzle.Eventer = (*eventer)(nil)
)
func TestEventsSimple(t *testing.T) {
e := newEventer(1, "oh noes")
ch := frizzle.InitEvents(e)
rcv := make([]frizzle.Event, 0)
for evt := range ch {
rcv = append(rcv, evt)
}
assert.Equal(t, 1, len(rcv), "should be one event received")
assert.Equal(t, "oh noes", rcv[0].String(), "output event should match input")
}
func TestEventsMulti(t *testing.T) {
expected := map[string]int{
"oh noes": 3,
"failed": 2,
"final": 7,
}
eventers := []interface{}{nil}
for k, v := range expected {
eventers = append(eventers, newEventer(v, k))
}
eventers = append(eventers, nil)
ch := frizzle.InitEvents(eventers...)
for evt := range ch {
expected[evt.String()]--
}
for k, v := range expected {
assert.Equalf(t, 0, v, "mismatch for key: %s", k)
}
}