forked from osteele/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters_test.go
72 lines (63 loc) · 2.29 KB
/
filters_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package expressions
import (
"fmt"
"testing"
"github.com/osteele/liquid/values"
"github.com/stretchr/testify/require"
)
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 }) })
require.Panics(t, func() { cfg.AddFilter("f", 10) })
}
func TestContext_runFilter(t *testing.T) {
cfg := NewConfig()
constant := func(value interface{}) valueFn {
return func(Context) values.Value { return values.ValueOf(value) }
}
receiver := constant("self")
// basic
cfg.AddFilter("f1", func(s string) string {
return "<" + s + ">"
})
ctx := NewContext(map[string]interface{}{"x": 10}, cfg)
out, err := ctx.ApplyFilter("f1", receiver, []valueFn{})
require.NoError(t, err)
require.Equal(t, "<self>", out)
// filter argument
cfg.AddFilter("with_arg", func(a, b string) string {
return fmt.Sprintf("(%s, %s)", a, b)
})
ctx = NewContext(map[string]interface{}{"x": 10}, cfg)
out, err = ctx.ApplyFilter("with_arg", receiver, []valueFn{constant("arg")})
require.NoError(t, err)
require.Equal(t, "(self, arg)", out)
// TODO optional argument
// TODO error return
// 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")
// closure
cfg.AddFilter("add", func(a, b int) int {
return a + b
})
cfg.AddFilter("closure", func(a string, expr Closure) (string, error) {
value, e := expr.Bind("y", 1).Evaluate()
if e != nil {
return "", e
}
return fmt.Sprintf("(%v, %v)", a, value), nil
})
ctx = NewContext(map[string]interface{}{"x": 10}, cfg)
out, err = ctx.ApplyFilter("closure", receiver, []valueFn{constant("x |add: y")})
require.NoError(t, err)
require.Equal(t, "(self, 11)", out)
}