1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 17:28:38 +01:00
liquid/expressions/expressions_test.go

117 lines
2.2 KiB
Go
Raw Normal View History

2017-06-26 15:33:07 +02:00
package expressions
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
var evaluatorTests = []struct {
in string
expected interface{}
}{
// Literals
2017-06-28 23:18:48 +02:00
{`12`, 12},
{`12.3`, 12.3},
{`true`, true},
{`false`, false},
{`'abc'`, "abc"},
{`"abc"`, "abc"},
2017-06-26 15:06:55 +02:00
// Variables
2017-06-28 23:18:48 +02:00
{`n`, 123},
2017-06-28 19:53:37 +02:00
// Attributes
2017-06-28 23:18:48 +02:00
{`obj.a`, "first"},
{`obj.b.c`, "d"},
{`obj.x`, nil},
2017-06-28 19:53:37 +02:00
{`fruits.first`, "apples"},
{`fruits.last`, "plums"},
{`empty_list.first`, nil},
{`empty_list.last`, nil},
2017-06-28 20:41:46 +02:00
{`"abc".size`, 3},
{`fruits.size`, 4},
2017-06-28 19:53:37 +02:00
// Indices
{`array[1]`, "second"},
{`array[-1]`, "third"}, // undocumented
{`array[100]`, nil},
2017-06-28 23:18:48 +02:00
{`obj[1]`, nil},
{`obj.c[0]`, "r"},
2017-06-28 19:53:37 +02:00
// Expressions
2017-06-28 23:18:48 +02:00
{`(n)`, 123},
2017-06-26 15:06:55 +02:00
// Operators
2017-06-28 23:18:48 +02:00
{`1 == 1`, true},
{`1 == 2`, false},
{`1.0 == 1.0`, true},
{`1.0 == 2.0`, false},
{`1.0 == 1`, true},
{`1 == 1.0`, true},
{`"a" == "a"`, true},
{`"a" == "b"`, false},
{`1 != 1`, false},
{`1 != 2`, true},
{`1.0 != 1.0`, false},
{`1 != 1.0`, false},
{`1 != 2.0`, true},
{`1 < 2`, true},
{`2 < 1`, false},
{`1.0 < 2.0`, true},
{`1.0 < 2`, true},
{`1 < 2.0`, true},
{`1.0 < 2`, true},
{`"a" < "a"`, false},
{`"a" < "b"`, true},
{`"b" < "a"`, false},
{`1 > 2`, false},
{`2 > 1`, true},
{`1 <= 1`, true},
{`1 <= 2`, true},
{`2 <= 1`, false},
{`"a" <= "a"`, true},
{`"a" <= "b"`, true},
{`"b" <= "a"`, false},
{`1 >= 1`, true},
{`1 >= 2`, false},
{`2 >= 1`, true},
{`true and false`, false},
{`true and true`, true},
{`true and true and true`, true},
{`false or false`, false},
{`false or true`, true},
{`"seafood" contains "foo"`, true},
{`"seafood" contains "bar"`, false},
}
2017-06-26 21:36:05 +02:00
var evaluatorTestContext = NewContext(map[string]interface{}{
2017-06-27 23:29:50 +02:00
"n": 123,
"array": []string{"first", "second", "third"},
2017-06-28 20:41:46 +02:00
"empty_list": []interface{}{},
2017-06-27 23:29:50 +02:00
"fruits": []string{"apples", "oranges", "peaches", "plums"},
"obj": map[string]interface{}{
"a": "first",
"b": map[string]interface{}{"c": "d"},
"c": []string{"r", "g", "b"},
},
2017-06-30 22:13:18 +02:00
}, NewSettings())
func TestEvaluator(t *testing.T) {
for i, test := range evaluatorTests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
2017-06-29 13:54:31 +02:00
val, err := EvaluateString(test.in, evaluatorTestContext)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, val, test.in)
})
}
}