tpl/internal: Sync go_templates

Closes #10411
This commit is contained in:
Bjørn Erik Pedersen
2022-11-14 19:13:09 +01:00
parent 58a98c7758
commit f6ab9553f4
34 changed files with 739 additions and 514 deletions

View File

@@ -362,8 +362,7 @@ var lexTests = []lexTest{
{"extra right paren", "{{3)}}", []item{
tLeft,
mkItem(itemNumber, "3"),
tRpar,
mkItem(itemError, `unexpected right paren U+0029 ')'`),
mkItem(itemError, "unexpected right paren"),
}},
// Fixed bugs
@@ -397,7 +396,12 @@ var lexTests = []lexTest{
// collect gathers the emitted items into a slice.
func collect(t *lexTest, left, right string) (items []item) {
l := lex(t.name, t.input, left, right, true)
l := lex(t.name, t.input, left, right)
l.options = lexOptions{
emitComment: true,
breakOK: true,
continueOK: true,
}
for {
item := l.nextItem()
items = append(items, item)
@@ -434,7 +438,9 @@ func TestLex(t *testing.T) {
items := collect(&test, "", "")
if !equal(items, test.items, false) {
t.Errorf("%s: got\n\t%+v\nexpected\n\t%v", test.name, items, test.items)
return // TODO
}
t.Log(test.name, "OK")
}
}
@@ -472,6 +478,39 @@ func TestDelims(t *testing.T) {
}
}
func TestDelimsAlphaNumeric(t *testing.T) {
test := lexTest{"right delimiter with alphanumeric start", "{{hub .host hub}}", []item{
mkItem(itemLeftDelim, "{{hub"),
mkItem(itemSpace, " "),
mkItem(itemField, ".host"),
mkItem(itemSpace, " "),
mkItem(itemRightDelim, "hub}}"),
tEOF,
}}
items := collect(&test, "{{hub", "hub}}")
if !equal(items, test.items, false) {
t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, items, test.items)
}
}
func TestDelimsAndMarkers(t *testing.T) {
test := lexTest{"delims that look like markers", "{{- .x -}} {{- - .x - -}}", []item{
mkItem(itemLeftDelim, "{{- "),
mkItem(itemField, ".x"),
mkItem(itemRightDelim, " -}}"),
mkItem(itemLeftDelim, "{{- "),
mkItem(itemField, ".x"),
mkItem(itemRightDelim, " -}}"),
tEOF,
}}
items := collect(&test, "{{- ", " -}}")
if !equal(items, test.items, false) {
t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, items, test.items)
}
}
var lexPosTests = []lexTest{
{"empty", "", []item{{itemEOF, 0, "", 1}}},
{"punctuation", "{{,@%#}}", []item{
@@ -533,22 +572,6 @@ func TestPos(t *testing.T) {
}
}
// Test that an error shuts down the lexing goroutine.
func TestShutdown(t *testing.T) {
// We need to duplicate template.Parse here to hold on to the lexer.
const text = "erroneous{{define}}{{else}}1234"
lexer := lex("foo", text, "{{", "}}", false)
_, err := New("root").parseLexer(lexer)
if err == nil {
t.Fatalf("expected error")
}
// The error should have drained the input. Therefore, the lexer should be shut down.
token, ok := <-lexer.items
if ok {
t.Fatalf("input was not drained; got %v", token)
}
}
// parseLexer is a local version of parse that lets us pass in the lexer instead of building it.
// We expect an error, so the tree set and funcs list are explicitly nil.
func (t *Tree) parseLexer(lex *lexer) (tree *Tree, err error) {