From 2b16109631a3cbf679be0b06f0c2fc1eecffcfc3 Mon Sep 17 00:00:00 2001 From: Mauricio Klein Date: Thu, 24 Oct 2019 17:46:52 +0200 Subject: [PATCH] Add additional test cases for non-primitive parameters --- chainable_test.go | 69 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/chainable_test.go b/chainable_test.go index 84b29f1..4b4f993 100644 --- a/chainable_test.go +++ b/chainable_test.go @@ -7,6 +7,19 @@ import ( "github.com/stretchr/testify/assert" ) +// a generic interface for testing purposes +type genericInterface interface { + DoThing() +} + +// a generic struct for testing purposes +type genericStruct struct{} + +func (gs genericStruct) DoThing() {} + +// a generic error for testing purposes +var errGeneric = errors.New("a generic error") + func TestChain(t *testing.T) { testCases := []struct { desc string @@ -51,10 +64,10 @@ func TestChain(t *testing.T) { from: []Argument{}, funcs: []Function{ func() int { return 0 }, - func(x int) error { return errors.New("a generic error") }, + func(x int) error { return errGeneric }, }, returnValue: []Argument{}, - err: errors.New("a generic error"), + err: errGeneric, }, { desc: "Without argument feedback", @@ -66,6 +79,26 @@ func TestChain(t *testing.T) { returnValue: []Argument{}, err: nil, }, + { + desc: "With non-primitive argument", + from: []Argument{genericStruct{}}, + funcs: []Function{ + func(gs genericStruct) genericStruct { return gs }, + func(gs genericStruct) genericStruct { return gs }, + }, + returnValue: []Argument{genericStruct{}}, + err: nil, + }, + { + desc: "With interface argument", + from: []Argument{genericStruct{}}, + funcs: []Function{ + func(gi genericInterface) genericInterface { return gi }, + func(gi genericInterface) genericInterface { return gi }, + }, + returnValue: []Argument{genericStruct{}}, + err: nil, + }, } for _, tc := range testCases { @@ -100,30 +133,50 @@ func TestChainDummy(t *testing.T) { from: []Argument{}, funcs: []Function{ func() int { return 2 }, - func(x int) (int, error) { return x, errors.New("a generic error") }, + func(x int) (int, error) { return x, errGeneric }, }, - returnValue: []Argument{2, errors.New("a generic error")}, + returnValue: []Argument{2, errGeneric}, err: nil, }, { desc: "With cascading error", from: []Argument{}, funcs: []Function{ - func() error { return errors.New("a generic error") }, + func() error { return errGeneric }, func(e error) error { return e }, func(e error) error { return e }, }, - returnValue: []Argument{errors.New("a generic error")}, + returnValue: []Argument{errGeneric}, err: nil, }, { desc: "With cascading error", - from: []Argument{errors.New("a generic error")}, + from: []Argument{errGeneric}, funcs: []Function{ func(e error) error { return e }, func(e error) error { return e }, }, - returnValue: []Argument{errors.New("a generic error")}, + returnValue: []Argument{errGeneric}, + err: nil, + }, + { + desc: "With non-primitive argument", + from: []Argument{genericStruct{}}, + funcs: []Function{ + func(gs genericStruct) genericStruct { return gs }, + func(gs genericStruct) (genericStruct, error) { return gs, errGeneric }, + }, + returnValue: []Argument{genericStruct{}, errGeneric}, + err: nil, + }, + { + desc: "With interface argument", + from: []Argument{genericStruct{}}, + funcs: []Function{ + func(gi genericInterface) (genericInterface, error) { return gi, errGeneric }, + func(gi genericInterface, e error) (genericInterface, error) { return gi, e }, + }, + returnValue: []Argument{genericStruct{}, errGeneric}, err: nil, }, }