github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/src/html/template/clone_test.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package template
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"io/ioutil"
    11  	"sync"
    12  	"testing"
    13  	"text/template/parse"
    14  )
    15  
    16  func TestAddParseTree(t *testing.T) {
    17  	root := Must(New("root").Parse(`{{define "a"}} {{.}} {{template "b"}} {{.}} "></a>{{end}}`))
    18  	tree, err := parse.Parse("t", `{{define "b"}}<a href="{{end}}`, "", "", nil, nil)
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	added := Must(root.AddParseTree("b", tree["b"]))
    23  	b := new(bytes.Buffer)
    24  	err = added.ExecuteTemplate(b, "a", "1>0")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	if got, want := b.String(), ` 1&gt;0 <a href=" 1%3e0 "></a>`; got != want {
    29  		t.Errorf("got %q want %q", got, want)
    30  	}
    31  }
    32  
    33  func TestClone(t *testing.T) {
    34  	// The {{.}} will be executed with data "<i>*/" in different contexts.
    35  	// In the t0 template, it will be in a text context.
    36  	// In the t1 template, it will be in a URL context.
    37  	// In the t2 template, it will be in a JavaScript context.
    38  	// In the t3 template, it will be in a CSS context.
    39  	const tmpl = `{{define "a"}}{{template "lhs"}}{{.}}{{template "rhs"}}{{end}}`
    40  	b := new(bytes.Buffer)
    41  
    42  	// Create an incomplete template t0.
    43  	t0 := Must(New("t0").Parse(tmpl))
    44  
    45  	// Clone t0 as t1.
    46  	t1 := Must(t0.Clone())
    47  	Must(t1.Parse(`{{define "lhs"}} <a href=" {{end}}`))
    48  	Must(t1.Parse(`{{define "rhs"}} "></a> {{end}}`))
    49  
    50  	// Execute t1.
    51  	b.Reset()
    52  	if err := t1.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if got, want := b.String(), ` <a href=" %3ci%3e*/ "></a> `; got != want {
    56  		t.Errorf("t1: got %q want %q", got, want)
    57  	}
    58  
    59  	// Clone t0 as t2.
    60  	t2 := Must(t0.Clone())
    61  	Must(t2.Parse(`{{define "lhs"}} <p onclick="javascript: {{end}}`))
    62  	Must(t2.Parse(`{{define "rhs"}} "></p> {{end}}`))
    63  
    64  	// Execute t2.
    65  	b.Reset()
    66  	if err := t2.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	if got, want := b.String(), ` <p onclick="javascript: &#34;\u003ci\u003e*/&#34; "></p> `; got != want {
    70  		t.Errorf("t2: got %q want %q", got, want)
    71  	}
    72  
    73  	// Clone t0 as t3, but do not execute t3 yet.
    74  	t3 := Must(t0.Clone())
    75  	Must(t3.Parse(`{{define "lhs"}} <style> {{end}}`))
    76  	Must(t3.Parse(`{{define "rhs"}} </style> {{end}}`))
    77  
    78  	// Complete t0.
    79  	Must(t0.Parse(`{{define "lhs"}} ( {{end}}`))
    80  	Must(t0.Parse(`{{define "rhs"}} ) {{end}}`))
    81  
    82  	// Clone t0 as t4. Redefining the "lhs" template should not fail.
    83  	t4 := Must(t0.Clone())
    84  	if _, err := t4.Parse(`{{define "lhs"}} OK {{end}}`); err != nil {
    85  		t.Errorf(`redefine "lhs": got err %v want nil`, err)
    86  	}
    87  	// Cloning t1 should fail as it has been executed.
    88  	if _, err := t1.Clone(); err == nil {
    89  		t.Error("cloning t1: got nil err want non-nil")
    90  	}
    91  	// Redefining the "lhs" template in t1 should fail as it has been executed.
    92  	if _, err := t1.Parse(`{{define "lhs"}} OK {{end}}`); err == nil {
    93  		t.Error(`redefine "lhs": got nil err want non-nil`)
    94  	}
    95  
    96  	// Execute t0.
    97  	b.Reset()
    98  	if err := t0.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	if got, want := b.String(), ` ( &lt;i&gt;*/ ) `; got != want {
   102  		t.Errorf("t0: got %q want %q", got, want)
   103  	}
   104  
   105  	// Clone t0. This should fail, as t0 has already executed.
   106  	if _, err := t0.Clone(); err == nil {
   107  		t.Error(`t0.Clone(): got nil err want non-nil`)
   108  	}
   109  
   110  	// Similarly, cloning sub-templates should fail.
   111  	if _, err := t0.Lookup("a").Clone(); err == nil {
   112  		t.Error(`t0.Lookup("a").Clone(): got nil err want non-nil`)
   113  	}
   114  	if _, err := t0.Lookup("lhs").Clone(); err == nil {
   115  		t.Error(`t0.Lookup("lhs").Clone(): got nil err want non-nil`)
   116  	}
   117  
   118  	// Execute t3.
   119  	b.Reset()
   120  	if err := t3.ExecuteTemplate(b, "a", "<i>*/"); err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	if got, want := b.String(), ` <style> ZgotmplZ </style> `; got != want {
   124  		t.Errorf("t3: got %q want %q", got, want)
   125  	}
   126  }
   127  
   128  func TestTemplates(t *testing.T) {
   129  	names := []string{"t0", "a", "lhs", "rhs"}
   130  	// Some template definitions borrowed from TestClone.
   131  	const tmpl = `
   132  		{{define "a"}}{{template "lhs"}}{{.}}{{template "rhs"}}{{end}}
   133  		{{define "lhs"}} <a href=" {{end}}
   134  		{{define "rhs"}} "></a> {{end}}`
   135  	t0 := Must(New("t0").Parse(tmpl))
   136  	templates := t0.Templates()
   137  	if len(templates) != len(names) {
   138  		t.Errorf("expected %d templates; got %d", len(names), len(templates))
   139  	}
   140  	for _, name := range names {
   141  		found := false
   142  		for _, tmpl := range templates {
   143  			if name == tmpl.text.Name() {
   144  				found = true
   145  				break
   146  			}
   147  		}
   148  		if !found {
   149  			t.Error("could not find template", name)
   150  		}
   151  	}
   152  }
   153  
   154  // This used to crash; https://golang.org/issue/3281
   155  func TestCloneCrash(t *testing.T) {
   156  	t1 := New("all")
   157  	Must(t1.New("t1").Parse(`{{define "foo"}}foo{{end}}`))
   158  	t1.Clone()
   159  }
   160  
   161  // Ensure that this guarantee from the docs is upheld:
   162  // "Further calls to Parse in the copy will add templates
   163  // to the copy but not to the original."
   164  func TestCloneThenParse(t *testing.T) {
   165  	t0 := Must(New("t0").Parse(`{{define "a"}}{{template "embedded"}}{{end}}`))
   166  	t1 := Must(t0.Clone())
   167  	Must(t1.Parse(`{{define "embedded"}}t1{{end}}`))
   168  	if len(t0.Templates())+1 != len(t1.Templates()) {
   169  		t.Error("adding a template to a clone added it to the original")
   170  	}
   171  	// double check that the embedded template isn't available in the original
   172  	err := t0.ExecuteTemplate(ioutil.Discard, "a", nil)
   173  	if err == nil {
   174  		t.Error("expected 'no such template' error")
   175  	}
   176  }
   177  
   178  // https://golang.org/issue/5980
   179  func TestFuncMapWorksAfterClone(t *testing.T) {
   180  	funcs := FuncMap{"customFunc": func() (string, error) {
   181  		return "", errors.New("issue5980")
   182  	}}
   183  
   184  	// get the expected error output (no clone)
   185  	uncloned := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
   186  	wantErr := uncloned.Execute(ioutil.Discard, nil)
   187  
   188  	// toClone must be the same as uncloned. It has to be recreated from scratch,
   189  	// since cloning cannot occur after execution.
   190  	toClone := Must(New("").Funcs(funcs).Parse("{{customFunc}}"))
   191  	cloned := Must(toClone.Clone())
   192  	gotErr := cloned.Execute(ioutil.Discard, nil)
   193  
   194  	if wantErr.Error() != gotErr.Error() {
   195  		t.Errorf("clone error message mismatch want %q got %q", wantErr, gotErr)
   196  	}
   197  }
   198  
   199  // https://golang.org/issue/16101
   200  func TestTemplateCloneExecuteRace(t *testing.T) {
   201  	const (
   202  		input   = `<title>{{block "a" .}}a{{end}}</title><body>{{block "b" .}}b{{end}}<body>`
   203  		overlay = `{{define "b"}}A{{end}}`
   204  	)
   205  	outer := Must(New("outer").Parse(input))
   206  	tmpl := Must(Must(outer.Clone()).Parse(overlay))
   207  
   208  	var wg sync.WaitGroup
   209  	for i := 0; i < 10; i++ {
   210  		wg.Add(1)
   211  		go func() {
   212  			defer wg.Done()
   213  			for i := 0; i < 100; i++ {
   214  				if err := tmpl.Execute(ioutil.Discard, "data"); err != nil {
   215  					panic(err)
   216  				}
   217  			}
   218  		}()
   219  	}
   220  	wg.Wait()
   221  }
   222  
   223  func TestTemplateCloneLookup(t *testing.T) {
   224  	// Template.escape makes an assumption that the template associated
   225  	// with t.Name() is t. Check that this holds.
   226  	tmpl := Must(New("x").Parse("a"))
   227  	tmpl = Must(tmpl.Clone())
   228  	if tmpl.Lookup(tmpl.Name()) != tmpl {
   229  		t.Error("after Clone, tmpl.Lookup(tmpl.Name()) != tmpl")
   230  	}
   231  }
   232  
   233  func TestCloneGrowth(t *testing.T) {
   234  	tmpl := Must(New("root").Parse(`<title>{{block "B". }}Arg{{end}}</title>`))
   235  	tmpl = Must(tmpl.Clone())
   236  	Must(tmpl.Parse(`{{define "B"}}Text{{end}}`))
   237  	for i := 0; i < 10; i++ {
   238  		tmpl.Execute(ioutil.Discard, nil)
   239  	}
   240  	if len(tmpl.DefinedTemplates()) > 200 {
   241  		t.Fatalf("too many templates: %v", len(tmpl.DefinedTemplates()))
   242  	}
   243  }