mirror of
https://github.com/danog/liquid.git
synced 2024-11-26 21:24:40 +01:00
Coverage
This commit is contained in:
parent
4e96c15bb3
commit
6f7b67f4d7
@ -36,13 +36,13 @@ func Convert(value interface{}, typ reflect.Type) (interface{}, error) { // noli
|
||||
value = ToLiquid(value)
|
||||
r := reflect.ValueOf(value)
|
||||
switch {
|
||||
case typ.Kind() != reflect.String && r.Type().ConvertibleTo(typ):
|
||||
case typ.Kind() != reflect.String && value != nil && r.Type().ConvertibleTo(typ):
|
||||
// convert int.Convert(string) yields "\x01" not "1"
|
||||
return r.Convert(typ).Interface(), nil
|
||||
case typ == timeType && r.Kind() == reflect.String:
|
||||
return ParseTime(value.(string))
|
||||
case reflect.PtrTo(r.Type()) == typ:
|
||||
return &value, nil
|
||||
// case reflect.PtrTo(r.Type()) == typ:
|
||||
// return &value, nil
|
||||
}
|
||||
switch typ.Kind() {
|
||||
case reflect.Bool:
|
||||
|
@ -32,6 +32,9 @@ var convertTests = []struct {
|
||||
{"1.2", 1.0, float64(1.2)},
|
||||
{true, 1, 1},
|
||||
{false, 1, 0},
|
||||
{nil, true, false},
|
||||
{0, true, true},
|
||||
{"", true, true},
|
||||
{1, "", "1"},
|
||||
{false, "", "false"},
|
||||
{true, "", "true"},
|
||||
@ -80,3 +83,14 @@ func TestConvert_map_to_array(t *testing.T) {
|
||||
sort.Strings(array)
|
||||
require.Equal(t, []string{"a", "b"}, array)
|
||||
}
|
||||
|
||||
// func TestConvert_ptr(t *testing.T) {
|
||||
// typ := reflect.PtrTo(reflect.TypeOf(""))
|
||||
// v, err := Convert("a", typ)
|
||||
// require.NoError(t, err)
|
||||
// ptr, ok := v.(*string)
|
||||
// fmt.Printf("%#v %T\n", v, v)
|
||||
// require.True(t, ok)
|
||||
// require.NotNil(t, ptr)
|
||||
// require.Equal(t, "ab", *ptr)
|
||||
// }
|
||||
|
@ -3,16 +3,10 @@ package evaluator
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/jeffjen/datefmt"
|
||||
)
|
||||
|
||||
var zeroTime time.Time
|
||||
|
||||
var dateFormats = []string{
|
||||
"%Y-%m-%d %H:%M:%S %Z",
|
||||
}
|
||||
|
||||
var dateLayouts = []string{
|
||||
// from the Go library
|
||||
time.ANSIC, // "Mon Jan _2 15:04:05 2006"
|
||||
@ -42,6 +36,7 @@ var dateLayouts = []string{
|
||||
"2006-01-02 15:04:05 -07:00",
|
||||
"2006-01-02 15:04:05 -0700",
|
||||
"2006-01-02 15:04:05 -7",
|
||||
"2006-01-02 15:04:05 MST",
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02 15:04",
|
||||
"January 2, 2006",
|
||||
@ -61,11 +56,5 @@ func ParseTime(s string) (time.Time, error) {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
for _, format := range dateFormats {
|
||||
t, err := datefmt.Strptime(format, s)
|
||||
if err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
return zeroTime, conversionError("", s, reflect.TypeOf(zeroTime))
|
||||
}
|
||||
|
17
evaluator/time_test.go
Normal file
17
evaluator/time_test.go
Normal file
@ -0,0 +1,17 @@
|
||||
package evaluator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConstant(t *testing.T) {
|
||||
dt, err := ParseTime("now")
|
||||
require.NoError(t, err)
|
||||
require.True(t, dt.After(timeMustParse("1970-01-01T00:00:00Z")))
|
||||
|
||||
dt, err = ParseTime("2017-07-09 10:40:00 UTC")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, timeMustParse("2017-07-09T10:40:00Z"), dt)
|
||||
}
|
@ -93,7 +93,9 @@ var evaluatorTests = []struct {
|
||||
{`"seafood" contains "foo"`, true},
|
||||
{`"seafood" contains "bar"`, false},
|
||||
{`array contains "first"`, true},
|
||||
{`obj_array contains "first"`, true},
|
||||
{`"foo" contains "missing"`, false},
|
||||
{`nil contains "missing"`, false},
|
||||
|
||||
// filters
|
||||
{`"seafood" | length`, 8},
|
||||
@ -102,6 +104,7 @@ var evaluatorTests = []struct {
|
||||
var evaluatorTestBindings = (map[string]interface{}{
|
||||
"n": 123,
|
||||
"array": []string{"first", "second", "third"},
|
||||
"obj_array": []interface{}{"first", "second", "third"},
|
||||
"empty_list": []interface{}{},
|
||||
"fruits": []string{"apples", "oranges", "peaches", "plums"},
|
||||
"obj": map[string]interface{}{
|
||||
|
@ -26,6 +26,12 @@ func addRenderTestTags(s Config) {
|
||||
_, err = w.Write([]byte(fmt.Sprint(v)))
|
||||
return err
|
||||
})
|
||||
s.AddTag("tag_name", func(string) (func(io.Writer, Context) error, error) {
|
||||
return func(w io.Writer, c Context) error {
|
||||
_, err := w.Write([]byte(c.TagName()))
|
||||
return err
|
||||
}, nil
|
||||
})
|
||||
s.AddTag("expand_arg", func(string) (func(w io.Writer, c Context) error, error) {
|
||||
return func(w io.Writer, c Context) error {
|
||||
s, err := c.ExpandTagArg()
|
||||
@ -52,6 +58,7 @@ var renderTests = []struct{ in, out string }{
|
||||
{`{% eval x %}{% endeval %}`, "123"},
|
||||
{`{% expand_arg x %}`, "x"},
|
||||
{`{% expand_arg {{x}} %}`, "123"},
|
||||
{`{% tag_name %}`, "tag_name"},
|
||||
}
|
||||
|
||||
var renderErrorTests = []struct{ in, out string }{
|
||||
|
Loading…
Reference in New Issue
Block a user