diff options
| author | Russ Cox <rsc@golang.org> | 2020-09-10 18:53:26 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2020-10-12 16:30:36 +0000 |
| commit | 9384d34c58099657bb1b133beaf3ff37ada9b017 (patch) | |
| tree | f1b31fd3061478b340178700e87b2a912aefc8bf /src/text/template/parse/parse_test.go | |
| parent | 39b527691495902279da7ac8405a070ded7dd4a2 (diff) | |
| download | go-9384d34c58099657bb1b133beaf3ff37ada9b017.tar.xz | |
text/template: allow newlines inside action delimiters
This allows multiline constructs like:
{{"hello" |
printf}}
Now that unclosed actions can span multiple lines,
track and report the start of the action when reporting errors.
Also clean up a few "unexpected <error message>" to be just "<error message>".
Fixes #29770.
Change-Id: I54c6c016029a8328b7902a4b6d85eab713ec3285
Reviewed-on: https://go-review.googlesource.com/c/go/+/254257
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/text/template/parse/parse_test.go')
| -rw-r--r-- | src/text/template/parse/parse_test.go | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/text/template/parse/parse_test.go b/src/text/template/parse/parse_test.go index d9c13c5d95..220f984777 100644 --- a/src/text/template/parse/parse_test.go +++ b/src/text/template/parse/parse_test.go @@ -250,6 +250,13 @@ var parseTests = []parseTest{ {"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError, `"x""y"`}, {"block definition", `{{block "foo" .}}hello{{end}}`, noError, `{{template "foo" .}}`}, + + {"newline in assignment", "{{ $x \n := \n 1 \n }}", noError, "{{$x := 1}}"}, + {"newline in empty action", "{{\n}}", hasError, "{{\n}}"}, + {"newline in pipeline", "{{\n\"x\"\n|\nprintf\n}}", noError, `{{"x" | printf}}`}, + {"newline in comment", "{{/*\nhello\n*/}}", noError, ""}, + {"newline in comment", "{{-\n/*\nhello\n*/\n-}}", noError, ""}, + // Errors. {"unclosed action", "hello{{range", hasError, ""}, {"unmatched end", "{{end}}", hasError, ""}, @@ -426,23 +433,38 @@ var errorTests = []parseTest{ // Check line numbers are accurate. {"unclosed1", "line1\n{{", - hasError, `unclosed1:2: unexpected unclosed action in command`}, + hasError, `unclosed1:2: unclosed action`}, {"unclosed2", "line1\n{{define `x`}}line2\n{{", - hasError, `unclosed2:3: unexpected unclosed action in command`}, + hasError, `unclosed2:3: unclosed action`}, + {"unclosed3", + "line1\n{{\"x\"\n\"y\"\n", + hasError, `unclosed3:4: unclosed action started at unclosed3:2`}, + {"unclosed4", + "{{\n\n\n\n\n", + hasError, `unclosed4:6: unclosed action started at unclosed4:1`}, + {"var1", + "line1\n{{\nx\n}}", + hasError, `var1:3: function "x" not defined`}, // Specific errors. {"function", "{{foo}}", hasError, `function "foo" not defined`}, - {"comment", + {"comment1", "{{/*}}", - hasError, `unclosed comment`}, + hasError, `comment1:1: unclosed comment`}, + {"comment2", + "{{/*\nhello\n}}", + hasError, `comment2:1: unclosed comment`}, {"lparen", "{{.X (1 2 3}}", hasError, `unclosed left paren`}, {"rparen", - "{{.X 1 2 3)}}", - hasError, `unexpected ")"`}, + "{{.X 1 2 3 ) }}", + hasError, `unexpected ")" in command`}, + {"rparen2", + "{{(.X 1 2 3", + hasError, `unclosed action`}, {"space", "{{`x`3}}", hasError, `in operand`}, @@ -488,7 +510,7 @@ var errorTests = []parseTest{ hasError, `missing value for parenthesized pipeline`}, {"multilinerawstring", "{{ $v := `\n` }} {{", - hasError, `multilinerawstring:2: unexpected unclosed action`}, + hasError, `multilinerawstring:2: unclosed action`}, {"rangeundefvar", "{{range $k}}{{end}}", hasError, `undefined variable`}, |
