tpl/internal: Sync Go template src to Go 1.20

Updates #10691
This commit is contained in:
Bjørn Erik Pedersen
2023-02-06 09:09:27 +01:00
parent 4801e2e8ee
commit 094135ff96
8 changed files with 175 additions and 11 deletions

View File

@@ -394,6 +394,36 @@ func TestParseWithComments(t *testing.T) {
}
}
func TestKeywordsAndFuncs(t *testing.T) {
// Check collisions between functions and new keywords like 'break'. When a
// break function is provided, the parser should treat 'break' as a function,
// not a keyword.
textFormat = "%q"
defer func() { textFormat = "%s" }()
inp := `{{range .X}}{{break 20}}{{end}}`
{
// 'break' is a defined function, don't treat it as a keyword: it should
// accept an argument successfully.
var funcsWithKeywordFunc = map[string]any{
"break": func(in any) any { return in },
}
tmpl, err := New("").Parse(inp, "", "", make(map[string]*Tree), funcsWithKeywordFunc)
if err != nil || tmpl == nil {
t.Errorf("with break func: unexpected error: %v", err)
}
}
{
// No function called 'break'; treat it as a keyword. Results in a parse
// error.
tmpl, err := New("").Parse(inp, "", "", make(map[string]*Tree), make(map[string]any))
if err == nil || tmpl != nil {
t.Errorf("without break func: expected error; got none")
}
}
}
func TestSkipFuncCheck(t *testing.T) {
oldTextFormat := textFormat
textFormat = "%q"