2017-07-14 02:18:23 +02:00
|
|
|
package expressions
|
2017-06-27 14:46:06 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-07-12 20:25:41 +02:00
|
|
|
var parseTests = []struct {
|
|
|
|
in string
|
|
|
|
expect interface{}
|
|
|
|
}{
|
|
|
|
{`a | filter: b`, 3},
|
2017-07-13 15:55:36 +02:00
|
|
|
// {`%assign a = 3`, nil},
|
|
|
|
// {`{%cycle 'a'`, []interface{}{"a"}},
|
|
|
|
// {`{%cycle 'a', 'b'`, []interface{}{"a", "b"}},
|
2017-07-06 05:25:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 14:46:06 +02:00
|
|
|
var parseErrorTests = []struct{ in, expected string }{
|
2017-07-02 13:51:24 +02:00
|
|
|
{"a syntax error", "parse error"},
|
2017-06-27 14:46:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 05:25:03 +02:00
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
cfg := NewConfig()
|
|
|
|
cfg.AddFilter("filter", func(a, b int) int { return a + b })
|
|
|
|
ctx := NewContext(map[string]interface{}{"a": 1, "b": 2}, cfg)
|
|
|
|
for i, test := range parseTests {
|
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
|
|
|
expr, err := Parse(test.in)
|
|
|
|
require.NoError(t, err, test.in)
|
2017-07-13 15:55:36 +02:00
|
|
|
_ = expr
|
2017-07-06 05:25:03 +02:00
|
|
|
value, err := expr.Evaluate(ctx)
|
|
|
|
require.NoError(t, err, test.in)
|
2017-07-12 20:25:41 +02:00
|
|
|
require.Equal(t, test.expect, value, test.in)
|
2017-07-06 05:25:03 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParse_errors(t *testing.T) {
|
2017-06-27 14:46:06 +02:00
|
|
|
for i, test := range parseErrorTests {
|
2017-06-30 22:13:18 +02:00
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
2017-06-27 14:46:06 +02:00
|
|
|
expr, err := Parse(test.in)
|
|
|
|
require.Nilf(t, expr, test.in)
|
|
|
|
require.Errorf(t, err, test.in, test.in)
|
|
|
|
require.Containsf(t, err.Error(), test.expected, test.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|