Skip to content

Commit

Permalink
Merge pull request #23 from gobuffalo/allow-no-param-for-variadic
Browse files Browse the repository at this point in the history
Variadic function allows 0 to n params, so must Plush
  • Loading branch information
markbates authored Nov 18, 2017
2 parents b038cde + a9c4939 commit 07945e7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
9 changes: 4 additions & 5 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,14 @@ func (c *compiler) evalCallExpression(node *ast.CallExpression) (interface{}, er
if len(args) > rtNumIn {
return nil, errors.WithStack(errors.Errorf("%s too many arguments (%d for %d) - %+v", node.String(), len(args), rtNumIn, args))
}
if len(args) < rtNumIn {
return nil, errors.WithStack(errors.Errorf("%s too few arguments (%d for %d) - %+v", node.String(), len(args), rtNumIn, args))
}
} else {
// Variadic func
nodeArgs := node.Arguments
nodeArgsLen := len(nodeArgs)
if nodeArgsLen < rtNumIn {
if nodeArgsLen < rtNumIn-1 {
return nil, errors.WithStack(errors.Errorf("%s too few arguments (%d for %d) - %+v", node.String(), len(args), rtNumIn, args))
}
var pos int
Expand Down Expand Up @@ -551,10 +554,6 @@ func (c *compiler) evalCallExpression(node *ast.CallExpression) (interface{}, er
}
}

if len(args) < rtNumIn {
return nil, errors.WithStack(errors.Errorf("%s too few arguments (%d for %d) - %+v", node.String(), len(args), rtNumIn, args))
}

res := rv.Call(args)
if len(res) > 0 {
if e, ok := res[len(res)-1].Interface().(error); ok {
Expand Down
26 changes: 26 additions & 0 deletions plush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,32 @@ func Test_VariadicHelper(t *testing.T) {
r.Equal("3", s)
}

func Test_VariadicHelperNoParam(t *testing.T) {
r := require.New(t)
input := `<%= foo() %>`
ctx := NewContext()
ctx.Set("foo", func(args ...int) int {
return len(args)
})

s, err := Render(input, ctx)
r.NoError(err)
r.Equal("0", s)
}

func Test_VariadicHelperNoVariadicParam(t *testing.T) {
r := require.New(t)
input := `<%= foo(1) %>`
ctx := NewContext()
ctx.Set("foo", func(a int, args ...int) int {
return a + len(args)
})

s, err := Render(input, ctx)
r.NoError(err)
r.Equal("1", s)
}

func Test_VariadicHelperWithWrongParam(t *testing.T) {
r := require.New(t)
input := `<%= foo(1, 2, "test") %>`
Expand Down

0 comments on commit 07945e7

Please sign in to comment.