Beispiel #1
0
func TestAddParseTree(t *testing.T) {
	// Create some templates.
	root, err := new(Set).Parse(cloneText1)
	if err != nil {
		t.Fatal(err)
	}
	_, err = root.Parse(cloneText2)
	if err != nil {
		t.Fatal(err)
	}
	// Add a new parse tree.
	tree, err := parse.Parse("cloneText3", cloneText3, "", "", nil, builtins)
	if err != nil {
		t.Fatal(err)
	}
	err = root.tree.Add(tree["c"])
	if err != nil {
		t.Fatal(err)
	}
	// Execute.
	var b bytes.Buffer
	err = root.Execute(&b, "a", 0)
	if err != nil {
		t.Fatal(err)
	}
	if b.String() != "broot" {
		t.Errorf("expected %q got %q", "broot", b.String())
	}
}
Beispiel #2
0
// parse parses the given text and adds the resulting templates to the set.
// The name is only used for debugging purposes: when parsing files or glob,
// it can show which file caused an error.
//
// Parsing templates after the set executed results in an error.
func (s *Set) parse(text, name string) (*Set, error) {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	if s.compiled {
		return nil, fmt.Errorf(
			"template: new templates can't be added after execution")
	}
	s.init()
	if tree, err := parse.Parse(name, text, s.leftDelim, s.rightDelim,
		builtins, s.parseFuncs); err != nil {
		return nil, err
	} else if err = s.tree.AddTree(tree); err != nil {
		return nil, err
	}
	return s, nil
}
Beispiel #3
0
func TestensurePipelineContains(t *testing.T) {
	tests := []struct {
		input, output string
		ids           []string
	}{
		{
			"{{.X}}",
			".X",
			[]string{},
		},
		{
			"{{.X | html}}",
			".X | html",
			[]string{},
		},
		{
			"{{.X}}",
			".X | html",
			[]string{"html"},
		},
		{
			"{{.X | html}}",
			".X | html | urlquery",
			[]string{"urlquery"},
		},
		{
			"{{.X | html | urlquery}}",
			".X | html | urlquery",
			[]string{"urlquery"},
		},
		{
			"{{.X | html | urlquery}}",
			".X | html | urlquery",
			[]string{"html", "urlquery"},
		},
		{
			"{{.X | html | urlquery}}",
			".X | html | urlquery",
			[]string{"html"},
		},
		{
			"{{.X | urlquery}}",
			".X | html | urlquery",
			[]string{"html", "urlquery"},
		},
		{
			"{{.X | html | print}}",
			".X | urlquery | html | print",
			[]string{"urlquery", "html"},
		},
		{
			"{{($).X | html | print}}",
			"($).X | urlquery | html | print",
			[]string{"urlquery", "html"},
		},
	}
	for i, test := range tests {
		text := fmt.Sprintf(`{{define "t"}}%s{{end}}`, test.input)
		// fake funcs just for the test.
		funcs := map[string]interface{}{
			"html":     true,
			"print":    true,
			"urlquery": true,
		}
		tree, err := parse.Parse("", text, "", "", funcs, FuncMap)
		if err != nil {
			t.Errorf("#%d: parsing error: %v", i, err)
			continue
		}
		action, ok := (tree["t"].List.Nodes[0].(*parse.ActionNode))
		if !ok {
			t.Errorf("#%d: First node is not an action: %s", i, text)
			continue
		}
		pipe := action.Pipe
		ensurePipelineContains(pipe, test.ids)
		got := pipe.String()
		if got != test.output {
			t.Errorf("#%d: %s, %v: want\n\t%s\ngot\n\t%s", i, text, test.ids, test.output, got)
		}
	}
}