From c8ea03828b0645b1fd5725888e44873b75fcfbb6 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Wed, 22 Apr 2020 22:17:56 +0300 Subject: text/template: add CommentNode to template parse tree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #34652 Change-Id: Icf6e3eda593fed826736f34f95a9d66f5450cc98 Reviewed-on: https://go-review.googlesource.com/c/go/+/229398 Reviewed-by: Daniel Martí Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot --- src/text/template/parse/parse_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/text/template/parse/parse_test.go') diff --git a/src/text/template/parse/parse_test.go b/src/text/template/parse/parse_test.go index 4e09a7852c..d9c13c5d95 100644 --- a/src/text/template/parse/parse_test.go +++ b/src/text/template/parse/parse_test.go @@ -348,6 +348,30 @@ func TestParseCopy(t *testing.T) { testParse(true, t) } +func TestParseWithComments(t *testing.T) { + textFormat = "%q" + defer func() { textFormat = "%s" }() + tests := [...]parseTest{ + {"comment", "{{/*\n\n\n*/}}", noError, "{{/*\n\n\n*/}}"}, + {"comment trim left", "x \r\n\t{{- /* hi */}}", noError, `"x"{{/* hi */}}`}, + {"comment trim right", "{{/* hi */ -}}\n\n\ty", noError, `{{/* hi */}}"y"`}, + {"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError, `"x"{{/* */}}"y"`}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + tr := New(test.name) + tr.Mode = ParseComments + tmpl, err := tr.Parse(test.input, "", "", make(map[string]*Tree)) + if err != nil { + t.Errorf("%q: expected error; got none", test.name) + } + if result := tmpl.Root.String(); result != test.result { + t.Errorf("%s=(%q): got\n\t%v\nexpected\n\t%v", test.name, test.input, result, test.result) + } + }) + } +} + type isEmptyTest struct { name string input string @@ -358,6 +382,7 @@ var isEmptyTests = []isEmptyTest{ {"empty", ``, true}, {"nonempty", `hello`, false}, {"spaces only", " \t\n \t\n", true}, + {"comment only", "{{/* comment */}}", true}, {"definition", `{{define "x"}}something{{end}}`, true}, {"definitions and space", "{{define `x`}}something{{end}}\n\n{{define `y`}}something{{end}}\n\n", true}, {"definitions and text", "{{define `x`}}something{{end}}\nx\n{{define `y`}}something{{end}}\ny\n", false}, -- cgit v1.3-5-g9baa