2017-07-07 11:41:37 +02:00
|
|
|
package parser
|
2017-06-28 04:34:46 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-07 11:41:37 +02:00
|
|
|
"strings"
|
2017-06-28 04:34:46 +02:00
|
|
|
"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"
|
2017-06-28 04:34:46 +02:00
|
|
|
}
|
2017-07-12 14:27:15 +02:00
|
|
|
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) }
|
2017-06-28 04:34:46 +02:00
|
|
|
|
|
|
|
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"},
|
2017-06-28 04:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 %}`},
|
2017-06-28 04:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseErrors(t *testing.T) {
|
2017-07-09 22:24:17 +02:00
|
|
|
cfg := Config{Grammar: grammarFake{}}
|
2017-06-28 04:34:46 +02:00
|
|
|
for i, test := range parseErrorTests {
|
2017-06-29 03:45:24 +02:00
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
2017-07-14 16:38:30 +02:00
|
|
|
_, err := cfg.Parse(test.in, SourceLoc{})
|
2017-06-28 04:34:46 +02:00
|
|
|
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{}}
|
2017-06-28 04:34:46 +02:00
|
|
|
for i, test := range parserTests {
|
2017-06-29 03:45:24 +02:00
|
|
|
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
|
2017-07-14 16:38:30 +02:00
|
|
|
_, err := cfg.Parse(test.in, SourceLoc{})
|
2017-06-28 04:34:46 +02:00
|
|
|
require.NoError(t, err, test.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|