1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-11 19:40:15 +01:00
liquid/expressions/filters_test.go

73 lines
2.3 KiB
Go
Raw Normal View History

2017-07-14 02:18:23 +02:00
package expressions
2017-07-05 17:17:31 +02:00
import (
"fmt"
"testing"
2017-07-28 00:11:37 +02:00
"github.com/osteele/liquid/values"
2017-07-05 17:17:31 +02:00
"github.com/stretchr/testify/require"
)
2017-07-15 18:13:07 +02:00
func TestContext_AddFilter(t *testing.T) {
cfg := NewConfig()
require.NotPanics(t, func() { cfg.AddFilter("f", func(int) int { return 0 }) })
require.NotPanics(t, func() { cfg.AddFilter("f", func(int) (a int, e error) { return }) })
require.Panics(t, func() { cfg.AddFilter("f", func() int { return 0 }) })
require.Panics(t, func() { cfg.AddFilter("f", func(int) {}) })
// require.Panics(t, func() { cfg.AddFilter("f", func(int) (a int, b int) { return }) })
require.Panics(t, func() { cfg.AddFilter("f", func(int) (a int, e error, b int) { return }) })
2017-07-19 15:42:01 +02:00
require.Panics(t, func() { cfg.AddFilter("f", 10) })
2017-07-15 18:13:07 +02:00
}
2017-07-05 17:17:31 +02:00
func TestContext_runFilter(t *testing.T) {
cfg := NewConfig()
constant := func(value interface{}) valueFn {
2017-07-28 00:11:37 +02:00
return func(Context) values.Value { return values.ValueOf(value) }
2017-07-05 17:17:31 +02:00
}
receiver := constant("self")
// basic
cfg.AddFilter("f1", func(s string) string {
return "<" + s + ">"
})
2017-07-07 11:41:37 +02:00
ctx := NewContext(map[string]interface{}{"x": 10}, cfg)
2017-07-16 19:47:06 +02:00
out, err := ctx.ApplyFilter("f1", receiver, []valueFn{})
require.NoError(t, err)
2017-07-05 17:17:31 +02:00
require.Equal(t, "<self>", out)
// filter argument
cfg.AddFilter("with_arg", func(a, b string) string {
return fmt.Sprintf("(%s, %s)", a, b)
})
2017-07-07 11:41:37 +02:00
ctx = NewContext(map[string]interface{}{"x": 10}, cfg)
2017-07-16 19:47:06 +02:00
out, err = ctx.ApplyFilter("with_arg", receiver, []valueFn{constant("arg")})
require.NoError(t, err)
2017-07-05 17:17:31 +02:00
require.Equal(t, "(self, arg)", out)
// TODO optional argument
// TODO error return
2017-07-16 19:47:06 +02:00
// extra argument
_, err = ctx.ApplyFilter("with_arg", receiver, []valueFn{constant(1), constant(2)})
require.Error(t, err)
require.Contains(t, err.Error(), "wrong number of arguments")
require.Contains(t, err.Error(), "given 2")
require.Contains(t, err.Error(), "expected 1")
2017-07-05 17:17:31 +02:00
// closure
cfg.AddFilter("add", func(a, b int) int {
return a + b
})
cfg.AddFilter("closure", func(a string, expr Closure) (string, error) {
2017-07-16 19:47:06 +02:00
value, e := expr.Bind("y", 1).Evaluate()
if e != nil {
return "", e
2017-07-05 17:17:31 +02:00
}
return fmt.Sprintf("(%v, %v)", a, value), nil
})
2017-07-07 11:41:37 +02:00
ctx = NewContext(map[string]interface{}{"x": 10}, cfg)
2017-07-16 19:47:06 +02:00
out, err = ctx.ApplyFilter("closure", receiver, []valueFn{constant("x |add: y")})
require.NoError(t, err)
2017-07-05 17:17:31 +02:00
require.Equal(t, "(self, 11)", out)
}