Repository navigation
Expand file tree
/
Copy pathparse_function_test.go
More file actions
43 lines (39 loc) · 1.86 KB
/
Copy pathparse_function_test.go
File metadata and controls
43 lines (39 loc) · 1.86 KB
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
package htt
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/typelate/htt/lex"
)
// TestParseTreeUndefinedFunction checks text/template's parse-time check
// that a called function is defined, which lex.Options.Functions turns on.
func TestParseTreeUndefinedFunction(t *testing.T) {
for _, tt := range []struct {
name string
text string
functions map[string]bool
html bool
wantErr string
}{
{name: "undefined", text: `{{foo .}}`, functions: map[string]bool{}, wantErr: `template: t:1: function "foo" not defined`},
{name: "undefined in pipeline in attribute", text: `<p title="{{. | foo}}">x</p>`, functions: map[string]bool{}, html: true, wantErr: `template: t:1: function "foo" not defined`},
{name: "undefined in define", text: `{{define "d"}}{{foo}}{{end}}`, functions: map[string]bool{}, wantErr: `template: t:1: function "foo" not defined`},
{name: "undefined on a later line", text: "a\n{{if .}}\n{{foo}}{{end}}", functions: map[string]bool{}, wantErr: `template: t:3: function "foo" not defined`},
{name: "html/template escaper is not defined", text: `{{_html_template_htmlescaper .}}`, functions: map[string]bool{}, html: true, wantErr: `template: t:1: function "_html_template_htmlescaper" not defined`},
{name: "builtins", text: `{{and 1 2}}{{len .}}{{printf "%d" 1 | html}}{{urlquery .}}{{eq 1 1}}{{slice .}}`, functions: map[string]bool{}},
{name: "registered", text: `{{foo .}}`, functions: map[string]bool{"foo": true}},
{name: "no check", text: `{{foo .}}`},
} {
t.Run(tt.name, func(t *testing.T) {
options := lex.NewOptions()
options.ParseHTML = tt.html
options.Functions = tt.functions
_, err := ParseTree("t", "t", tt.text, nil, options)
if tt.wantErr == "" {
require.NoError(t, err)
return
}
assert.EqualError(t, err, tt.wantErr)
})
}
}