Skip to content

Commit

Permalink
Merge branch 'issue-9'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Klein committed Oct 24, 2019
2 parents 44fcd4e + 5f4a82a commit a06a9be
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 21 deletions.
6 changes: 5 additions & 1 deletion chainable.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *Chainable) Unwrap() ([]Argument, error) {

for linkIndex, link := range c.links {
if v, err = link.process(linkIndex, v); err != nil {
return nil, err
return v, err
}
}

Expand Down Expand Up @@ -160,5 +160,9 @@ func reflectArgs(args []Argument) []reflect.Value {
// doesReturnError returns true if the last return
// value for vfn is a type error
func doesReturnError(vfnType reflect.Type) bool {
if vfnType.NumOut() == 0 {
return false
}

return vfnType.Out(vfnType.NumOut()-1) == reflect.TypeOf((*error)(nil)).Elem()
}
144 changes: 124 additions & 20 deletions chainable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,133 @@ import (
)

func TestChain(t *testing.T) {
sum2 := func(x int) int { return x + 2 }
mul2 := func(x int) int { return x * 2 }

// (4 + 2) * 2 = 12
ret, err := New().
From(4).
Chain(sum2, mul2).
Unwrap()

assert.Equal(t, []Argument{12}, ret)
assert.NoError(t, err)
testCases := []struct {
desc string
from []Argument
funcs []Function
returnValue []Argument
err error
}{
{
desc: "Regular chain",
from: []Argument{4},
funcs: []Function{
func(x int) int { return x + 2 },
func(x int) int { return x * 2 },
},
returnValue: []Argument{12},
err: nil,
},
{
desc: "No initial value in chain",
from: []Argument{},
funcs: []Function{
func() int { return 2 },
func(x int) int { return x * 2 },
},
returnValue: []Argument{4},
err: nil,
},
{
desc: "No return value",
from: []Argument{4},
funcs: []Function{
func(x int) int { return x + 2 },
func(x int) int { return x * 2 },
func(int) {},
},
returnValue: []Argument{},
err: nil,
},
{
desc: "With error",
from: []Argument{},
funcs: []Function{
func() int { return 0 },
func(x int) error { return errors.New("a generic error") },
},
returnValue: []Argument{},
err: errors.New("a generic error"),
},
{
desc: "Without argument feedback",
from: []Argument{},
funcs: []Function{
func() {},
func() {},
},
returnValue: []Argument{},
err: nil,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
rv, err := New().From(tc.from...).Chain(tc.funcs...).Unwrap()
assert.Equal(t, tc.returnValue, rv)
assert.Equal(t, tc.err, err)
})
}
}

func TestChainDummy(t *testing.T) {
genericError := errors.New("a generic error")

f1 := func() error { return genericError }
f2 := func(e error) int { return 0 }

ret, err := New().ChainDummy(f1, f2).Unwrap()

assert.Equal(t, []Argument{0}, ret)
assert.NoError(t, err)
testCases := []struct {
desc string
from []Argument
funcs []Function
returnValue []Argument
err error
}{
{
desc: "Regular chain",
from: []Argument{4},
funcs: []Function{
func(x int) int { return x + 2 },
func(x int) int { return x * 2 },
},
returnValue: []Argument{12},
err: nil,
},
{
desc: "With error in the end of the chain",
from: []Argument{},
funcs: []Function{
func() int { return 2 },
func(x int) (int, error) { return x, errors.New("a generic error") },
},
returnValue: []Argument{2, errors.New("a generic error")},
err: nil,
},
{
desc: "With cascading error",
from: []Argument{},
funcs: []Function{
func() error { return errors.New("a generic error") },
func(e error) error { return e },
func(e error) error { return e },
},
returnValue: []Argument{errors.New("a generic error")},
err: nil,
},
{
desc: "With cascading error",
from: []Argument{errors.New("a generic error")},
funcs: []Function{
func(e error) error { return e },
func(e error) error { return e },
},
returnValue: []Argument{errors.New("a generic error")},
err: nil,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
rv, err := New().From(tc.from...).ChainDummy(tc.funcs...).Unwrap()
assert.Equal(t, tc.returnValue, rv)
assert.Equal(t, tc.err, err)
})
}
}

func TestNotAFunction(t *testing.T) {
Expand Down

0 comments on commit a06a9be

Please sign in to comment.