Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eval): fixes variadic helper calls #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func (v *evalVisitor) callFunc(name string, funcVal reflect.Value, options *Opti
}

// check and collect arguments
args := make([]reflect.Value, numIn+vaCount)
args := make([]reflect.Value, 0, numIn+vaCount)
for i, param := range params {
arg := reflect.ValueOf(param)
var argType reflect.Type
Expand Down Expand Up @@ -652,11 +652,11 @@ func (v *evalVisitor) callFunc(name string, funcVal reflect.Value, options *Opti
}
}

args[i] = arg
args = append(args, arg)
}

if addOptions {
args[numIn+vaCount-1] = reflect.ValueOf(options)
args = append(args, reflect.ValueOf(options))
}

result := funcVal.Call(args)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/mailgun/raymond/v2
module github.com/pbedat/raymond

go 1.16

Expand Down
31 changes: 28 additions & 3 deletions handlebars/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,16 @@ func detectDataHelper(options *raymond.Options) string {
return ""
}

//
func variadicHelper(a string, b ...bool) string {
if len(b) > 0 && b[0] {
return "TRUE " + a
}
return "FALSE " + a
}

// Those tests come from:
// https://github.com/wycats/handlebars.js/blob/master/spec/helper.js
//
// https://github.com/wycats/handlebars.js/blob/master/spec/helper.js
var helpersTests = []Test{
{
"helper with complex lookup",
Expand Down Expand Up @@ -656,7 +662,26 @@ var helpersTests = []Test{
nil,
"GOODBYE cruel WORLD goodbye",
},

{"variadic helper",
`{{ variadicHelper "foo" true}}`,
map[string]string{},
nil,
map[string]interface{}{
"variadicHelper": variadicHelper,
},
nil,
"TRUE foo",
},
{"variadic helper without args",
`{{ variadicHelper "foo"}}`,
map[string]string{},
nil,
map[string]interface{}{
"variadicHelper": variadicHelper,
},
nil,
"FALSE foo",
},
// @todo "block params" tests
}

Expand Down