This repository was archived by the owner on Mar 12, 2025. It is now read-only.
forked from osteele/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
72 lines (62 loc) · 2.25 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package parser
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
type (
grammarFake struct{}
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" }
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 }{
{"{% if test %}", `unterminated "if" block`},
{"{% if test %}{% endunless %}", "not inside unless"},
// TODO tag syntax could specify statement type to catch these in parser
// {"{{ 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 %}`},
{`{% unless test %}{% endunless %}`},
{`{% for item in list %}{% if test %}{% else %}{% endif %}{% endfor %}`},
{`{% if true %}{% raw %}{% endraw %}{% endif %}`},
{`{% comment %}{% if true %}{% endcomment %}`},
{`{% raw %}{% if true %}{% endraw %}`},
}
func TestParseErrors(t *testing.T) {
cfg := Config{Grammar: grammarFake{}}
for i, test := range parseErrorTests {
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) {
cfg := Config{Grammar: grammarFake{}}
for i, test := range parserTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
_, err := cfg.Parse(test.in, SourceLoc{})
require.NoError(t, err, test.in)
})
}
}