tpl/internal/go_templates: Revert formatting
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 3 Dec 2020 12:47:43 +0000 (13:47 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 3 Dec 2020 12:47:43 +0000 (13:47 +0100)
Should make future fork synch easier.

21 files changed:
tpl/internal/go_templates/fmtsort/sort_test.go
tpl/internal/go_templates/htmltemplate/content_test.go
tpl/internal/go_templates/htmltemplate/css.go
tpl/internal/go_templates/htmltemplate/escape_test.go
tpl/internal/go_templates/htmltemplate/example_test.go
tpl/internal/go_templates/htmltemplate/js.go
tpl/internal/go_templates/htmltemplate/js_test.go
tpl/internal/go_templates/htmltemplate/transition.go
tpl/internal/go_templates/htmltemplate/transition_test.go
tpl/internal/go_templates/testenv/testenv.go
tpl/internal/go_templates/testenv/testenv_windows.go
tpl/internal/go_templates/texttemplate/example_test.go
tpl/internal/go_templates/texttemplate/exec.go
tpl/internal/go_templates/texttemplate/exec_test.go
tpl/internal/go_templates/texttemplate/hugo_template.go
tpl/internal/go_templates/texttemplate/hugo_template_test.go
tpl/internal/go_templates/texttemplate/multi_test.go
tpl/internal/go_templates/texttemplate/parse/node.go
tpl/internal/go_templates/texttemplate/parse/parse.go
tpl/internal/go_templates/texttemplate/parse/parse_test.go
tpl/internal/go_templates/texttemplate/template.go

index 189c693ccde591237f14a266f2b985bc1d4fbb49..364c5bf6d27f45d51de62abf83a33be67944427d 100644 (file)
@@ -6,12 +6,11 @@ package fmtsort_test
 
 import (
        "fmt"
+       "github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort"
        "math"
        "reflect"
        "strings"
        "testing"
-
-       "github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort"
 )
 
 var compareTests = [][]reflect.Value{
index f2c5c5ed1ff7db437eed211792899b9d6ec52e56..b5de701d350a55167d81c533b3d70b50ceba869e 100644 (file)
@@ -427,7 +427,7 @@ func TestStringer(t *testing.T) {
        if err := tmpl.Execute(b, s); err != nil {
                t.Fatal(err)
        }
-       expect := "string=3"
+       var expect = "string=3"
        if b.String() != expect {
                t.Errorf("expected %q got %q", expect, b.String())
        }
index b006a0718fe1efe43041db3edb8aa30318c2f2f8..eb92fc92b55ea65af2a45fe05b7496036d995356 100644 (file)
@@ -210,10 +210,8 @@ var cssReplacementTable = []string{
        '}':  `\7d`,
 }
 
-var (
-       expressionBytes = []byte("expression")
-       mozBindingBytes = []byte("mozbinding")
-)
+var expressionBytes = []byte("expression")
+var mozBindingBytes = []byte("mozbinding")
 
 // cssValueFilter allows innocuous CSS values in the output including CSS
 // quantities (10px or 25%), ID or class literals (#foo, .bar), keyword values
index ee5421cc806529a726e25ef7712318c0866a286c..075db4e13714845ee2440d0760387a9b5575cb03 100644 (file)
@@ -891,6 +891,7 @@ func TestEscapeSet(t *testing.T) {
                        t.Errorf("want\n\t%q\ngot\n\t%q", test.want, got)
                }
        }
+
 }
 
 func TestErrors(t *testing.T) {
@@ -1087,6 +1088,7 @@ func TestErrors(t *testing.T) {
                // Check that we get the same error if we call Execute again.
                if err := tmpl.Execute(buf, nil); err == nil || err.Error() != got {
                        t.Errorf("input=%q: unexpected error on second call %q", test.input, err)
+
                }
        }
 }
index ee1cbbb4a38a2cc130270fc94cf1adf9cc9421b1..a93b8d2fbbdf52b857907eef37f8563390cd7011 100644 (file)
@@ -82,6 +82,7 @@ func Example() {
        //              <div><strong>no rows</strong></div>
        //      </body>
        // </html>
+
 }
 
 func Example_autoescaping() {
@@ -122,6 +123,7 @@ func Example_escape() {
        // \"Fran \u0026 Freddie\'s Diner\" \u003Ctasty@example.com\u003E
        // \"Fran \u0026 Freddie\'s Diner\"32\u003Ctasty@example.com\u003E
        // %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E
+
 }
 
 func ExampleTemplate_Delims() {
index f19e774c7f6e3006c1e3efbb7612c585f40b56ef..cfd413461f36096a5c8d211bf84730ef5182a137 100644 (file)
@@ -337,7 +337,6 @@ var jsStrNormReplacementTable = []string{
        '<':  `\u003c`,
        '>':  `\u003e`,
 }
-
 var jsRegexpReplacementTable = []string{
        0:    `\u0000`,
        '\t': `\t`,
index f7ee3cfaa3d118cc5bdcbfd934b227e7399ad272..e15087f0f741c92bb441bd48390bbf0fc02806dd 100644 (file)
@@ -206,8 +206,7 @@ func TestJSStrEscaper(t *testing.T) {
                {"<!--", `\u003c!--`},
                {"-->", `--\u003e`},
                // From https://code.google.com/p/doctype/wiki/ArticleUtf7
-               {
-                       "+ADw-script+AD4-alert(1)+ADw-/script+AD4-",
+               {"+ADw-script+AD4-alert(1)+ADw-/script+AD4-",
                        `\u002bADw-script\u002bAD4-alert(1)\u002bADw-\/script\u002bAD4-`,
                },
                // Invalid UTF-8 sequence
index 5ba7f8017c1835893be0920cd4ad3dc5632ef12a..06df679330db2c49d1bfc7d2369f2bc2a40f164a 100644 (file)
@@ -41,10 +41,8 @@ var transitionFunc = [...]func(context, []byte) (context, int){
        stateError:       tError,
 }
 
-var (
-       commentStart = []byte("<!--")
-       commentEnd   = []byte("-->")
-)
+var commentStart = []byte("<!--")
+var commentEnd = []byte("-->")
 
 // tText is the context transition function for the text state.
 func tText(c context, s []byte) (context, int) {
index c3257a8394207a04b307f2453bd893e94c74a92f..00b0ff6cabe7f61b928308feb6081e29e309f855 100644 (file)
@@ -42,6 +42,7 @@ func TestFindEndTag(t *testing.T) {
 }
 
 func BenchmarkTemplateSpecialTags(b *testing.B) {
+
        r := struct {
                Name, Gift string
        }{"Aunt Mildred", "bone china tea set"}
index 7c46089f53257b635a59c44481dd0896d4529a7d..90044570d3762bffe10176524da593b159ee2db2 100644 (file)
@@ -13,6 +13,7 @@ package testenv
 import (
        "errors"
        "flag"
+       "github.com/gohugoio/hugo/tpl/internal/go_templates/cfg"
        "os"
        "os/exec"
        "path/filepath"
@@ -21,8 +22,6 @@ import (
        "strings"
        "sync"
        "testing"
-
-       "github.com/gohugoio/hugo/tpl/internal/go_templates/cfg"
 )
 
 // Builder reports the name of the builder running this test
index f9e7da5a75bb13f511c54ca49fcc5a901c5a9e2b..eb8d6ac1650385b80bc1dcaea382d37a6b85d35e 100644 (file)
@@ -12,10 +12,8 @@ import (
        "syscall"
 )
 
-var (
-       symlinkOnce   sync.Once
-       winSymlinkErr error
-)
+var symlinkOnce sync.Once
+var winSymlinkErr error
 
 func initWinHasSymlink() {
        tmpdir, err := ioutil.TempDir("", "symtest")
index 09555c95a85bcc596ef44b776f14b6c7bc88ee05..f192cac4fbe8db66b2c1a2821378e85849a579a3 100644 (file)
@@ -34,7 +34,7 @@ Josie
                Name, Gift string
                Attended   bool
        }
-       recipients := []Recipient{
+       var recipients = []Recipient{
                {"Aunt Mildred", "bone china tea set", true},
                {"Uncle John", "moleskin pants", false},
                {"Cousin Rodney", "", false},
index db533d7358e4422596ee55659ecc27ac3a5fbd6e..879cd08846ea70d3baf5d2d3a3b2b1954df868d5 100644 (file)
@@ -6,13 +6,12 @@ package template
 
 import (
        "fmt"
+       "github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort"
+       "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
        "io"
        "reflect"
        "runtime"
        "strings"
-
-       "github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort"
-       "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
 )
 
 // maxExecDepth specifies the maximum stack depth of templates within
index cc79d214328e748b757efdfa5e066dca6302e893..940a1de6a0543072bcc3a2e867d1cf0282bdf0ef 100644 (file)
@@ -318,16 +318,12 @@ var execTests = []execTest{
        {"$.U.V", "{{$.U.V}}", "v", tVal, true},
        {"declare in action", "{{$x := $.U.V}}{{$x}}", "v", tVal, true},
        {"simple assignment", "{{$x := 2}}{{$x = 3}}{{$x}}", "3", tVal, true},
-       {
-               "nested assignment",
+       {"nested assignment",
                "{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{$x}}",
-               "3", tVal, true,
-       },
-       {
-               "nested assignment changes the last declaration",
+               "3", tVal, true},
+       {"nested assignment changes the last declaration",
                "{{$x := 1}}{{if true}}{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{end}}{{$x}}",
-               "1", tVal, true,
-       },
+               "1", tVal, true},
 
        // Type with String method.
        {"V{6666}.String()", "-{{.V0}}-", "-<6666>-", tVal, true},
@@ -374,21 +370,15 @@ var execTests = []execTest{
        {".Method3(nil constant)", "-{{.Method3 nil}}-", "-Method3: <nil>-", tVal, true},
        {".Method3(nil value)", "-{{.Method3 .MXI.unset}}-", "-Method3: <nil>-", tVal, true},
        {"method on var", "{{if $x := .}}-{{$x.Method2 .U16 $x.X}}{{end}}-", "-Method2: 16 x-", tVal, true},
-       {
-               "method on chained var",
+       {"method on chained var",
                "{{range .MSIone}}{{if $.U.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
-               "true", tVal, true,
-       },
-       {
-               "chained method",
+               "true", tVal, true},
+       {"chained method",
                "{{range .MSIone}}{{if $.GetU.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
-               "true", tVal, true,
-       },
-       {
-               "chained method on variable",
+               "true", tVal, true},
+       {"chained method on variable",
                "{{with $x := .}}{{with .SI}}{{$.GetU.TrueFalse $.True}}{{end}}{{end}}",
-               "true", tVal, true,
-       },
+               "true", tVal, true},
        {".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true},
        {".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true},
        {"method on nil value from slice", "-{{range .}}{{.Method1 1234}}{{end}}-", "-1234-", tSliceOfNil, true},
@@ -474,14 +464,10 @@ var execTests = []execTest{
        {"printf lots", `{{printf "%d %s %g %s" 127 "hello" 7-3i .Method0}}`, "127 hello (7-3i) M0", tVal, true},
 
        // HTML.
-       {
-               "html", `{{html "<script>alert(\"XSS\");</script>"}}`,
-               "&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true,
-       },
-       {
-               "html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
-               "&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true,
-       },
+       {"html", `{{html "<script>alert(\"XSS\");</script>"}}`,
+               "&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true},
+       {"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
+               "&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true},
        {"html", `{{html .PS}}`, "a string", tVal, true},
        {"html typed nil", `{{html .NIL}}`, "&lt;nil&gt;", tVal, true},
        {"html untyped nil", `{{html .Empty0}}`, "&lt;no value&gt;", tVal, true},
@@ -839,7 +825,7 @@ var delimPairs = []string{
 
 func TestDelims(t *testing.T) {
        const hello = "Hello, world"
-       value := struct{ Str string }{hello}
+       var value = struct{ Str string }{hello}
        for i := 0; i < len(delimPairs); i += 2 {
                text := ".Str"
                left := delimPairs[i+0]
@@ -862,7 +848,7 @@ func TestDelims(t *testing.T) {
                if err != nil {
                        t.Fatalf("delim %q text %q parse err %s", left, text, err)
                }
-               b := new(bytes.Buffer)
+               var b = new(bytes.Buffer)
                err = tmpl.Execute(b, value)
                if err != nil {
                        t.Fatalf("delim %q exec err %s", left, err)
@@ -963,7 +949,7 @@ const treeTemplate = `
 `
 
 func TestTree(t *testing.T) {
-       tree := &Tree{
+       var tree = &Tree{
                1,
                &Tree{
                        2, &Tree{
@@ -1209,7 +1195,7 @@ var cmpTests = []cmpTest{
 
 func TestComparison(t *testing.T) {
        b := new(bytes.Buffer)
-       cmpStruct := struct {
+       var cmpStruct = struct {
                Uthree, Ufour  uint
                NegOne, Three  int
                Ptr, NilPtr    *int
index 0dc6331c7f384d9f09e48a4435f064b67d0a594a..7cd6df0fb410d581c0ea35192348e13be3bbf331 100644 (file)
@@ -77,6 +77,7 @@ func (t *executer) Execute(p Preparer, wr io.Writer, data interface{}) error {
        }
 
        return tmpl.executeWithState(state, value)
+
 }
 
 // Prepare returns a template ready for execution.
index 90bb0931570cb8cc9246420c2e7764e5c60d1b2c..98a2575eb983c0ce7b0422d2c0f4d2c11fc14956 100644 (file)
@@ -85,4 +85,5 @@ Method: {{ .Hello1 "v1" }}
        c.Assert(got, qt.Contains, "hello hugo")
        c.Assert(got, qt.Contains, "Map: av")
        c.Assert(got, qt.Contains, "Method: v2 v1")
+
 }
index 569a45dbbfb3b66c1a60bff19252f0fe87e9b241..7323be379515dc0b22421ec093f64083b311ae6c 100644 (file)
@@ -11,9 +11,8 @@ package template
 import (
        "bytes"
        "fmt"
-       "testing"
-
        "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
+       "testing"
 )
 
 const (
@@ -30,32 +29,22 @@ type multiParseTest struct {
 }
 
 var multiParseTests = []multiParseTest{
-       {
-               "empty", "", noError,
+       {"empty", "", noError,
                nil,
-               nil,
-       },
-       {
-               "one", `{{define "foo"}} FOO {{end}}`, noError,
+               nil},
+       {"one", `{{define "foo"}} FOO {{end}}`, noError,
                []string{"foo"},
-               []string{" FOO "},
-       },
-       {
-               "two", `{{define "foo"}} FOO {{end}}{{define "bar"}} BAR {{end}}`, noError,
+               []string{" FOO "}},
+       {"two", `{{define "foo"}} FOO {{end}}{{define "bar"}} BAR {{end}}`, noError,
                []string{"foo", "bar"},
-               []string{" FOO ", " BAR "},
-       },
+               []string{" FOO ", " BAR "}},
        // errors
-       {
-               "missing end", `{{define "foo"}} FOO `, hasError,
-               nil,
-               nil,
-       },
-       {
-               "malformed name", `{{define "foo}} FOO `, hasError,
+       {"missing end", `{{define "foo"}} FOO `, hasError,
                nil,
+               nil},
+       {"malformed name", `{{define "foo}} FOO `, hasError,
                nil,
-       },
+               nil},
 }
 
 func TestMultiParse(t *testing.T) {
@@ -411,7 +400,7 @@ func TestIssue19294(t *testing.T) {
        // by the contents of "stylesheet", but if the internal map associating
        // names with templates is built in the wrong order, the empty block
        // looks non-empty and this doesn't happen.
-       inlined := map[string]string{
+       var inlined = map[string]string{
                "stylesheet": `{{define "stylesheet"}}stylesheet{{end}}`,
                "xhtml":      `{{block "stylesheet" .}}{{end}}`,
        }
index dee67a2ed07c729170e9be627a447f2944d9e5fb..1c116ea6fab3dbf594306b9846dbf97f74715243 100644 (file)
@@ -249,6 +249,7 @@ func (a *ActionNode) tree() *Tree {
 
 func (a *ActionNode) Copy() Node {
        return a.tr.newAction(a.Pos, a.Line, a.Pipe.CopyPipe())
+
 }
 
 // CommandNode holds a command (a pipeline inside an evaluating action).
index 7a58c5788f94548b0eb1722c2701533d47cb98d4..c9b80f4a24836846a54bba632c75c13fe9310169 100644 (file)
@@ -453,7 +453,7 @@ func (t *Tree) parseControl(allowElseIf bool, context string) (pos Pos, line int
        var next Node
        list, next = t.itemList()
        switch next.Type() {
-       case nodeEnd: // done
+       case nodeEnd: //done
        case nodeElse:
                if allowElseIf {
                        // Special case for "else if". If the "else" is followed immediately by an "if",
index 97511190d7cac1b6be3710275a8bdd01855a6c1e..79e7bb5ae5faeb0830a65f692c6cf31b8c9f7dcd 100644 (file)
@@ -178,134 +178,70 @@ const (
 )
 
 var parseTests = []parseTest{
-       {
-               "empty", "", noError,
-               ``,
-       },
-       {
-               "comment", "{{/*\n\n\n*/}}", noError,
-               ``,
-       },
-       {
-               "spaces", " \t\n", noError,
-               `" \t\n"`,
-       },
-       {
-               "text", "some text", noError,
-               `"some text"`,
-       },
-       {
-               "emptyAction", "{{}}", hasError,
-               `{{}}`,
-       },
-       {
-               "field", "{{.X}}", noError,
-               `{{.X}}`,
-       },
-       {
-               "simple command", "{{printf}}", noError,
-               `{{printf}}`,
-       },
-       {
-               "$ invocation", "{{$}}", noError,
-               "{{$}}",
-       },
-       {
-               "variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError,
-               "{{with $x := 3}}{{$x 23}}{{end}}",
-       },
-       {
-               "variable with fields", "{{$.I}}", noError,
-               "{{$.I}}",
-       },
-       {
-               "multi-word command", "{{printf `%d` 23}}", noError,
-               "{{printf `%d` 23}}",
-       },
-       {
-               "pipeline", "{{.X|.Y}}", noError,
-               `{{.X | .Y}}`,
-       },
-       {
-               "pipeline with decl", "{{$x := .X|.Y}}", noError,
-               `{{$x := .X | .Y}}`,
-       },
-       {
-               "nested pipeline", "{{.X (.Y .Z) (.A | .B .C) (.E)}}", noError,
-               `{{.X (.Y .Z) (.A | .B .C) (.E)}}`,
-       },
-       {
-               "field applied to parentheses", "{{(.Y .Z).Field}}", noError,
-               `{{(.Y .Z).Field}}`,
-       },
-       {
-               "simple if", "{{if .X}}hello{{end}}", noError,
-               `{{if .X}}"hello"{{end}}`,
-       },
-       {
-               "if with else", "{{if .X}}true{{else}}false{{end}}", noError,
-               `{{if .X}}"true"{{else}}"false"{{end}}`,
-       },
-       {
-               "if with else if", "{{if .X}}true{{else if .Y}}false{{end}}", noError,
-               `{{if .X}}"true"{{else}}{{if .Y}}"false"{{end}}{{end}}`,
-       },
-       {
-               "if else chain", "+{{if .X}}X{{else if .Y}}Y{{else if .Z}}Z{{end}}+", noError,
-               `"+"{{if .X}}"X"{{else}}{{if .Y}}"Y"{{else}}{{if .Z}}"Z"{{end}}{{end}}{{end}}"+"`,
-       },
-       {
-               "simple range", "{{range .X}}hello{{end}}", noError,
-               `{{range .X}}"hello"{{end}}`,
-       },
-       {
-               "chained field range", "{{range .X.Y.Z}}hello{{end}}", noError,
-               `{{range .X.Y.Z}}"hello"{{end}}`,
-       },
-       {
-               "nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError,
-               `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`,
-       },
-       {
-               "range with else", "{{range .X}}true{{else}}false{{end}}", noError,
-               `{{range .X}}"true"{{else}}"false"{{end}}`,
-       },
-       {
-               "range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError,
-               `{{range .X | .M}}"true"{{else}}"false"{{end}}`,
-       },
-       {
-               "range []int", "{{range .SI}}{{.}}{{end}}", noError,
-               `{{range .SI}}{{.}}{{end}}`,
-       },
-       {
-               "range 1 var", "{{range $x := .SI}}{{.}}{{end}}", noError,
-               `{{range $x := .SI}}{{.}}{{end}}`,
-       },
-       {
-               "range 2 vars", "{{range $x, $y := .SI}}{{.}}{{end}}", noError,
-               `{{range $x, $y := .SI}}{{.}}{{end}}`,
-       },
-       {
-               "constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError,
-               `{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`,
-       },
-       {
-               "template", "{{template `x`}}", noError,
-               `{{template "x"}}`,
-       },
-       {
-               "template with arg", "{{template `x` .Y}}", noError,
-               `{{template "x" .Y}}`,
-       },
-       {
-               "with", "{{with .X}}hello{{end}}", noError,
-               `{{with .X}}"hello"{{end}}`,
-       },
-       {
-               "with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError,
-               `{{with .X}}"hello"{{else}}"goodbye"{{end}}`,
-       },
+       {"empty", "", noError,
+               ``},
+       {"comment", "{{/*\n\n\n*/}}", noError,
+               ``},
+       {"spaces", " \t\n", noError,
+               `" \t\n"`},
+       {"text", "some text", noError,
+               `"some text"`},
+       {"emptyAction", "{{}}", hasError,
+               `{{}}`},
+       {"field", "{{.X}}", noError,
+               `{{.X}}`},
+       {"simple command", "{{printf}}", noError,
+               `{{printf}}`},
+       {"$ invocation", "{{$}}", noError,
+               "{{$}}"},
+       {"variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError,
+               "{{with $x := 3}}{{$x 23}}{{end}}"},
+       {"variable with fields", "{{$.I}}", noError,
+               "{{$.I}}"},
+       {"multi-word command", "{{printf `%d` 23}}", noError,
+               "{{printf `%d` 23}}"},
+       {"pipeline", "{{.X|.Y}}", noError,
+               `{{.X | .Y}}`},
+       {"pipeline with decl", "{{$x := .X|.Y}}", noError,
+               `{{$x := .X | .Y}}`},
+       {"nested pipeline", "{{.X (.Y .Z) (.A | .B .C) (.E)}}", noError,
+               `{{.X (.Y .Z) (.A | .B .C) (.E)}}`},
+       {"field applied to parentheses", "{{(.Y .Z).Field}}", noError,
+               `{{(.Y .Z).Field}}`},
+       {"simple if", "{{if .X}}hello{{end}}", noError,
+               `{{if .X}}"hello"{{end}}`},
+       {"if with else", "{{if .X}}true{{else}}false{{end}}", noError,
+               `{{if .X}}"true"{{else}}"false"{{end}}`},
+       {"if with else if", "{{if .X}}true{{else if .Y}}false{{end}}", noError,
+               `{{if .X}}"true"{{else}}{{if .Y}}"false"{{end}}{{end}}`},
+       {"if else chain", "+{{if .X}}X{{else if .Y}}Y{{else if .Z}}Z{{end}}+", noError,
+               `"+"{{if .X}}"X"{{else}}{{if .Y}}"Y"{{else}}{{if .Z}}"Z"{{end}}{{end}}{{end}}"+"`},
+       {"simple range", "{{range .X}}hello{{end}}", noError,
+               `{{range .X}}"hello"{{end}}`},
+       {"chained field range", "{{range .X.Y.Z}}hello{{end}}", noError,
+               `{{range .X.Y.Z}}"hello"{{end}}`},
+       {"nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError,
+               `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`},
+       {"range with else", "{{range .X}}true{{else}}false{{end}}", noError,
+               `{{range .X}}"true"{{else}}"false"{{end}}`},
+       {"range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError,
+               `{{range .X | .M}}"true"{{else}}"false"{{end}}`},
+       {"range []int", "{{range .SI}}{{.}}{{end}}", noError,
+               `{{range .SI}}{{.}}{{end}}`},
+       {"range 1 var", "{{range $x := .SI}}{{.}}{{end}}", noError,
+               `{{range $x := .SI}}{{.}}{{end}}`},
+       {"range 2 vars", "{{range $x, $y := .SI}}{{.}}{{end}}", noError,
+               `{{range $x, $y := .SI}}{{.}}{{end}}`},
+       {"constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError,
+               `{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`},
+       {"template", "{{template `x`}}", noError,
+               `{{template "x"}}`},
+       {"template with arg", "{{template `x` .Y}}", noError,
+               `{{template "x" .Y}}`},
+       {"with", "{{with .X}}hello{{end}}", noError,
+               `{{with .X}}"hello"{{end}}`},
+       {"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError,
+               `{{with .X}}"hello"{{else}}"goodbye"{{end}}`},
        // Trimming spaces.
        {"trim left", "x \r\n\t{{- 3}}", noError, `"x"{{3}}`},
        {"trim right", "{{3 -}}\n\n\ty", noError, `{{3}}"y"`},
@@ -314,10 +250,8 @@ var parseTests = []parseTest{
        {"comment trim left", "x \r\n\t{{- /* hi */}}", noError, `"x"`},
        {"comment trim right", "{{/* hi */ -}}\n\n\ty", noError, `"y"`},
        {"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError, `"x""y"`},
-       {
-               "block definition", `{{block "foo" .}}hello{{end}}`, noError,
-               `{{template "foo" .}}`,
-       },
+       {"block definition", `{{block "foo" .}}hello{{end}}`, noError,
+               `{{template "foo" .}}`},
        // Errors.
        {"unclosed action", "hello{{range", hasError, ""},
        {"unmatched end", "{{end}}", hasError, ""},
@@ -467,143 +401,89 @@ func TestErrorContextWithTreeCopy(t *testing.T) {
 // All failures, and the result is a string that must appear in the error message.
 var errorTests = []parseTest{
        // Check line numbers are accurate.
-       {
-               "unclosed1",
+       {"unclosed1",
                "line1\n{{",
-               hasError, `unclosed1:2: unexpected unclosed action in command`,
-       },
-       {
-               "unclosed2",
+               hasError, `unclosed1:2: unexpected unclosed action in command`},
+       {"unclosed2",
                "line1\n{{define `x`}}line2\n{{",
-               hasError, `unclosed2:3: unexpected unclosed action in command`,
-       },
+               hasError, `unclosed2:3: unexpected unclosed action in command`},
        // Specific errors.
-       {
-               "function",
+       {"function",
                "{{foo}}",
-               hasError, `function "foo" not defined`,
-       },
-       {
-               "comment",
+               hasError, `function "foo" not defined`},
+       {"comment",
                "{{/*}}",
-               hasError, `unclosed comment`,
-       },
-       {
-               "lparen",
+               hasError, `unclosed comment`},
+       {"lparen",
                "{{.X (1 2 3}}",
-               hasError, `unclosed left paren`,
-       },
-       {
-               "rparen",
+               hasError, `unclosed left paren`},
+       {"rparen",
                "{{.X 1 2 3)}}",
-               hasError, `unexpected ")"`,
-       },
-       {
-               "space",
+               hasError, `unexpected ")"`},
+       {"space",
                "{{`x`3}}",
-               hasError, `in operand`,
-       },
-       {
-               "idchar",
+               hasError, `in operand`},
+       {"idchar",
                "{{a#}}",
-               hasError, `'#'`,
-       },
-       {
-               "charconst",
+               hasError, `'#'`},
+       {"charconst",
                "{{'a}}",
-               hasError, `unterminated character constant`,
-       },
-       {
-               "stringconst",
+               hasError, `unterminated character constant`},
+       {"stringconst",
                `{{"a}}`,
-               hasError, `unterminated quoted string`,
-       },
-       {
-               "rawstringconst",
+               hasError, `unterminated quoted string`},
+       {"rawstringconst",
                "{{`a}}",
-               hasError, `unterminated raw quoted string`,
-       },
-       {
-               "number",
+               hasError, `unterminated raw quoted string`},
+       {"number",
                "{{0xi}}",
-               hasError, `number syntax`,
-       },
-       {
-               "multidefine",
+               hasError, `number syntax`},
+       {"multidefine",
                "{{define `a`}}a{{end}}{{define `a`}}b{{end}}",
-               hasError, `multiple definition of template`,
-       },
-       {
-               "eof",
+               hasError, `multiple definition of template`},
+       {"eof",
                "{{range .X}}",
-               hasError, `unexpected EOF`,
-       },
-       {
-               "variable",
+               hasError, `unexpected EOF`},
+       {"variable",
                // Declare $x so it's defined, to avoid that error, and then check we don't parse a declaration.
                "{{$x := 23}}{{with $x.y := 3}}{{$x 23}}{{end}}",
-               hasError, `unexpected ":="`,
-       },
-       {
-               "multidecl",
+               hasError, `unexpected ":="`},
+       {"multidecl",
                "{{$a,$b,$c := 23}}",
-               hasError, `too many declarations`,
-       },
-       {
-               "undefvar",
+               hasError, `too many declarations`},
+       {"undefvar",
                "{{$a}}",
-               hasError, `undefined variable`,
-       },
-       {
-               "wrongdot",
+               hasError, `undefined variable`},
+       {"wrongdot",
                "{{true.any}}",
-               hasError, `unexpected . after term`,
-       },
-       {
-               "wrongpipeline",
+               hasError, `unexpected . after term`},
+       {"wrongpipeline",
                "{{12|false}}",
-               hasError, `non executable command in pipeline`,
-       },
-       {
-               "emptypipeline",
+               hasError, `non executable command in pipeline`},
+       {"emptypipeline",
                `{{ ( ) }}`,
-               hasError, `missing value for parenthesized pipeline`,
-       },
-       {
-               "multilinerawstring",
+               hasError, `missing value for parenthesized pipeline`},
+       {"multilinerawstring",
                "{{ $v := `\n` }} {{",
-               hasError, `multilinerawstring:2: unexpected unclosed action`,
-       },
-       {
-               "rangeundefvar",
+               hasError, `multilinerawstring:2: unexpected unclosed action`},
+       {"rangeundefvar",
                "{{range $k}}{{end}}",
-               hasError, `undefined variable`,
-       },
-       {
-               "rangeundefvars",
+               hasError, `undefined variable`},
+       {"rangeundefvars",
                "{{range $k, $v}}{{end}}",
-               hasError, `undefined variable`,
-       },
-       {
-               "rangemissingvalue1",
+               hasError, `undefined variable`},
+       {"rangemissingvalue1",
                "{{range $k,}}{{end}}",
-               hasError, `missing value for range`,
-       },
-       {
-               "rangemissingvalue2",
+               hasError, `missing value for range`},
+       {"rangemissingvalue2",
                "{{range $k, $v := }}{{end}}",
-               hasError, `missing value for range`,
-       },
-       {
-               "rangenotvariable1",
+               hasError, `missing value for range`},
+       {"rangenotvariable1",
                "{{range $k, .}}{{end}}",
-               hasError, `range can only initialize variables`,
-       },
-       {
-               "rangenotvariable2",
+               hasError, `range can only initialize variables`},
+       {"rangenotvariable2",
                "{{range $k, 123 := .}}{{end}}",
-               hasError, `range can only initialize variables`,
-       },
+               hasError, `range can only initialize variables`},
 }
 
 func TestErrors(t *testing.T) {
index 755a8426eae9bcf4a93738f6185796566ec8b251..9c6ba6dfca089fcbb26a0ec7d4e43a2a81b5ea90 100644 (file)
@@ -5,10 +5,9 @@
 package template
 
 import (
+       "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
        "reflect"
        "sync"
-
-       "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
 )
 
 // common holds the information shared by related templates.