Added tests
This commit is contained in:
parent
91d1562e0e
commit
e7cb026708
59
bus_test.go
59
bus_test.go
|
@ -1,8 +1,11 @@
|
|||
package events
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNew_Lifecycle(t *testing.T) {
|
||||
|
@ -18,7 +21,7 @@ func TestNew_Lifecycle(t *testing.T) {
|
|||
d.Fire("event", 234, 234)
|
||||
d.Fire("event", "string", "value")
|
||||
|
||||
assertEquals(t, [][]int{
|
||||
assert.Equal(t, [][]int{
|
||||
{1, 123, 123},
|
||||
{2, 123},
|
||||
{3, 123, 0},
|
||||
|
@ -31,8 +34,54 @@ func TestNew_Lifecycle(t *testing.T) {
|
|||
}, receives)
|
||||
}
|
||||
|
||||
func assertEquals(t testing.TB, expected interface{}, actual interface{}) {
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("Expected %v but was %v", expected, actual)
|
||||
func TestTryFire(t *testing.T) {
|
||||
errVal := errors.New("bang")
|
||||
|
||||
tests := []struct {
|
||||
errsRet [3]error
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
errsRet: [3]error{nil, nil, nil},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
errsRet: [3]error{errVal, nil, nil},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
errsRet: [3]error{nil, errVal, nil},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
errsRet: [3]error{nil, nil, errVal},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
errsRet: [3]error{errVal, nil, errVal},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||
d := New()
|
||||
var handlersFired [4]bool
|
||||
|
||||
d.On("event", func() error { handlersFired[0] = true; return tt.errsRet[0] })
|
||||
d.On("event", func(x int) error { handlersFired[1] = true; return tt.errsRet[1] })
|
||||
d.On("event", func(x int, y string) error { handlersFired[2] = true; return tt.errsRet[2] })
|
||||
d.On("event", func(x string, y string) { handlersFired[3] = true })
|
||||
|
||||
err := d.TryFire("event")
|
||||
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, [4]bool{true, true, true, true}, handlersFired)
|
||||
})
|
||||
}
|
||||
}
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
|||
module github.com/lmika/events
|
||||
|
||||
go 1.14
|
||||
|
||||
require github.com/stretchr/testify v1.9.0 // indirect
|
||||
|
|
12
go.sum
12
go.sum
|
@ -1,10 +1,22 @@
|
|||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
Loading…
Reference in a new issue