1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 08:08:58 +01:00
liquid/parser/parser_test.go

71 lines
2.2 KiB
Go
Raw Normal View History

2017-07-07 11:41:37 +02:00
package parser
import (
"fmt"
2017-07-07 11:41:37 +02:00
"strings"
"testing"
"github.com/stretchr/testify/require"
)
2017-07-07 11:41:37 +02:00
type grammarFake struct{}
type blockSyntaxFake string
func (g grammarFake) BlockSyntax(w string) (BlockSyntax, bool) {
return blockSyntaxFake(w), true
}
func (g blockSyntaxFake) IsBlock() bool { return true }
func (g blockSyntaxFake) CanHaveParent(p BlockSyntax) bool {
return string(g) == "end"+p.TagName() || (g == "else" && p.TagName() == "if")
}
func (g blockSyntaxFake) IsBlockEnd() bool { return strings.HasPrefix(string(g), "end") }
func (g blockSyntaxFake) IsBlockStart() bool {
return g == "for" || g == "if" || g == "unless"
}
func (g blockSyntaxFake) IsClause() bool { return g == "else" }
2017-07-07 11:41:37 +02:00
func (g blockSyntaxFake) ParentTags() []string { return []string{"unless"} }
func (g blockSyntaxFake) RequiresParent() bool { return g == "else" || g.IsBlockEnd() }
func (g blockSyntaxFake) TagName() string { return string(g) }
var parseErrorTests = []struct{ in, expected string }{
2017-07-17 18:19:14 +02:00
{"{% if test %}", `unterminated "if" block`},
2017-07-06 14:59:10 +02:00
{"{% if test %}{% endunless %}", "not inside unless"},
// TODO tag syntax could specify statement type to catch these in parser
2017-07-19 00:39:38 +02:00
// {"{{ syntax error }}", "syntax error"},
// {"{% for syntax error %}{% endfor %}", "syntax error"},
}
var parserTests = []struct{ in string }{
{`{% for item in list %}{% endfor %}`},
{`{% if test %}{% else %}{% endif %}`},
{`{% if test %}{% if test %}{% endif %}{% endif %}`},
2017-07-07 11:41:37 +02:00
{`{% unless test %}{% endunless %}`},
2017-07-06 14:59:10 +02:00
{`{% for item in list %}{% if test %}{% else %}{% endif %}{% endfor %}`},
2017-06-30 14:45:22 +02:00
{`{% if true %}{% raw %}{% endraw %}{% endif %}`},
2017-07-06 14:59:10 +02:00
{`{% comment %}{% if true %}{% endcomment %}`},
{`{% raw %}{% if true %}{% endraw %}`},
}
func TestParseErrors(t *testing.T) {
2017-07-09 22:24:17 +02:00
cfg := Config{Grammar: grammarFake{}}
for i, test := range parseErrorTests {
2017-06-29 03:45:24 +02:00
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
_, err := cfg.Parse(test.in, SourceLoc{})
require.Errorf(t, err, test.in)
require.Containsf(t, err.Error(), test.expected, test.in)
})
}
}
func TestParser(t *testing.T) {
2017-07-09 22:24:17 +02:00
cfg := Config{Grammar: grammarFake{}}
for i, test := range parserTests {
2017-06-29 03:45:24 +02:00
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
_, err := cfg.Parse(test.in, SourceLoc{})
require.NoError(t, err, test.in)
})
}
}