1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 05:44:40 +01:00
liquid/parser/scanner_test.go

101 lines
2.7 KiB
Go
Raw Normal View History

2017-07-07 11:41:37 +02:00
package parser
2017-06-27 19:18:11 +02:00
import (
"fmt"
2017-06-27 19:18:11 +02:00
"testing"
"github.com/stretchr/testify/require"
)
var scannerCountTests = []struct {
in string
len int
}{
{`{% tag arg %}`, 1},
{`{% tag arg %}{% tag %}`, 2},
{`{% tag arg %}{% tag arg %}{% tag %}`, 3},
{`{% tag %}{% tag %}`, 2},
{`{% tag arg %}{% tag arg %}{% tag %}{% tag %}`, 4},
{`{{ expr }}`, 1},
{`{{ expr arg }}`, 1},
{`{{ expr }}{{ expr }}`, 2},
{`{{ expr arg }}{{ expr arg }}`, 2},
}
2017-07-09 17:49:24 +02:00
func TestScan(t *testing.T) {
scan := func(src string) []Token { return Scan(src, SourceLoc{}) }
2017-07-09 17:49:24 +02:00
tokens := scan("12")
2017-06-27 19:18:11 +02:00
require.NotNil(t, tokens)
require.Len(t, tokens, 1)
2017-07-09 17:18:35 +02:00
require.Equal(t, TextTokenType, tokens[0].Type)
2017-06-27 19:18:11 +02:00
require.Equal(t, "12", tokens[0].Source)
2017-07-09 17:49:24 +02:00
tokens = scan("{{obj}}")
2017-06-27 19:18:11 +02:00
require.NotNil(t, tokens)
require.Len(t, tokens, 1)
2017-07-09 17:18:35 +02:00
require.Equal(t, ObjTokenType, tokens[0].Type)
2017-06-30 23:33:36 +02:00
require.Equal(t, "obj", tokens[0].Args)
2017-06-27 19:18:11 +02:00
2017-07-09 17:49:24 +02:00
tokens = scan("{{ obj }}")
2017-06-27 19:18:11 +02:00
require.NotNil(t, tokens)
require.Len(t, tokens, 1)
2017-07-09 17:18:35 +02:00
require.Equal(t, ObjTokenType, tokens[0].Type)
2017-06-30 23:33:36 +02:00
require.Equal(t, "obj", tokens[0].Args)
2017-06-27 19:18:11 +02:00
2017-07-09 17:49:24 +02:00
tokens = scan("{%tag args%}")
2017-06-27 19:18:11 +02:00
require.NotNil(t, tokens)
require.Len(t, tokens, 1)
2017-07-09 17:18:35 +02:00
require.Equal(t, TagTokenType, tokens[0].Type)
2017-06-29 18:20:16 +02:00
require.Equal(t, "tag", tokens[0].Name)
2017-06-30 23:33:36 +02:00
require.Equal(t, "args", tokens[0].Args)
2017-06-27 19:18:11 +02:00
2017-07-09 17:49:24 +02:00
tokens = scan("{% tag args %}")
2017-06-27 19:18:11 +02:00
require.NotNil(t, tokens)
require.Len(t, tokens, 1)
2017-07-09 17:18:35 +02:00
require.Equal(t, TagTokenType, tokens[0].Type)
2017-06-29 18:20:16 +02:00
require.Equal(t, "tag", tokens[0].Name)
2017-06-30 23:33:36 +02:00
require.Equal(t, "args", tokens[0].Args)
2017-07-09 17:49:24 +02:00
tokens = scan("pre{% tag args %}mid{{ object }}post")
2017-07-09 17:18:35 +02:00
require.Equal(t, `[TextTokenType{"pre"} TagTokenType{Tag:"tag", Args:"args"} TextTokenType{"mid"} ObjTokenType{"object"} TextTokenType{"post"}]`, fmt.Sprint(tokens))
2017-07-02 13:51:24 +02:00
for i, test := range scannerCountTests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
2017-07-09 17:49:24 +02:00
tokens := scan(test.in)
require.Len(t, tokens, test.len)
})
}
2017-06-27 19:18:11 +02:00
}
2017-07-16 20:15:11 +02:00
func TestScan_ws(t *testing.T) {
// whitespace control
scan := func(src string) []Token { return Scan(src, SourceLoc{}) }
wsTests := []struct {
in, expect string
left, right bool
}{
{`{{ expr }}`, "expr", false, false},
{`{{- expr }}`, "expr", true, false},
{`{{ expr -}}`, "expr", false, true},
{`{% tag arg %}`, "tag", false, false},
{`{%- tag arg %}`, "tag", true, false},
{`{% tag arg -%}`, "tag", false, true},
}
for i, test := range wsTests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
tokens := scan(test.in)
require.Len(t, tokens, 1)
tok := tokens[0]
if test.expect == "tag" {
require.Equalf(t, "tag", tok.Name, test.in)
require.Equalf(t, "arg", tok.Args, test.in)
} else {
require.Equalf(t, "expr", tok.Args, test.in)
}
require.Equalf(t, test.left, tok.TrimLeft, test.in)
require.Equalf(t, test.right, tok.TrimRight, test.in)
})
}
}