1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 08:39:01 +01:00
liquid/evaluator/call_test.go
Oliver Steele 023536f149 Coverage
2017-07-19 10:49:39 -04:00

45 lines
1.2 KiB
Go

package evaluator
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestCall(t *testing.T) {
fn := func(a, b string) string {
return a + "," + b + "."
}
value, err := Call(reflect.ValueOf(fn), []interface{}{5, 10})
require.NoError(t, err)
require.Equal(t, "5,10.", value)
// extra arguments
_, err = Call(reflect.ValueOf(fn), []interface{}{5, 10, 20})
require.Error(t, err)
require.Contains(t, err.Error(), "wrong number of arguments")
require.Contains(t, err.Error(), "given 3")
require.Contains(t, err.Error(), "expected 2")
// error return
fn2 := func(int) (int, error) { return 0, fmt.Errorf("expected error") }
_, err = Call(reflect.ValueOf(fn2), []interface{}{2})
require.Error(t, err)
require.Contains(t, err.Error(), "expected error")
}
func TestCall_optional(t *testing.T) {
fn := func(a string, b func(string) string) string {
return a + "," + b("default") + "."
}
value, err := Call(reflect.ValueOf(fn), []interface{}{5})
require.NoError(t, err)
require.Equal(t, "5,default.", value)
value, err = Call(reflect.ValueOf(fn), []interface{}{5, 10})
require.NoError(t, err)
require.Equal(t, "5,10.", value)
}