File: tmplfunc_test.go

package info (click to toggle)
golang-rsc-tmplfunc 0.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 204 kB
  • sloc: makefile: 2
file content (111 lines) | stat: -rw-r--r-- 2,947 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tmplfunc

import (
	"bytes"
	"fmt"
	"strings"
	"testing"

	htmltemplate "html/template"
	texttemplate "text/template"
)

var tests = []struct {
	in  string
	out string
}{
	{`{{define "hello"}}hello {{.}}{{end}}{{template "hello" "world"}}`, "hello world"},
	{`{{define "hello"}}hello {{.}}{{end}}{{hello "world"}}`, "hello world"},
	{`{{define "hello who"}}hello {{.who}}{{end}}{{hello "world"}}`, "hello world"},
	{`{{define "hello who"}}hello {{.who}}{{end}}{{hello}}`,
		"EXEC: template: :1:45: executing \"\" at <hello>: error calling hello: too few arguments in call to template hello",
	},
	{`{{define "hello who?"}}hello {{.who}}{{end}}{{hello}}`, "hello"},
	{`{{define "hello who?"}}hello {{.who}}{{end}}{{hello "world"}}`, "hello world"},
	{`{{define "hello who..."}}hello {{.who}}{{end}}{{hello}}`, "hello []"},
	{`{{define "hello who..."}}hello {{.who}}{{end}}{{hello "world"}}`, "hello [world]"},
}

func TestText(t *testing.T) {
	for i, tt := range tests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			tmpl := texttemplate.New("")
			err := Parse(tmpl, tt.in)
			var out string
			if err != nil {
				out = "PARSE: " + err.Error()
			} else {
				var buf bytes.Buffer
				err := tmpl.Execute(&buf, nil)
				if err != nil {
					out = "EXEC: " + err.Error()
				} else {
					out = strings.ReplaceAll(buf.String(), "<no value>", "") // text generates these but html does not
					out = strings.TrimSpace(out)
				}
			}
			if out != tt.out {
				t.Errorf("have: %s\nwant: %s", out, tt.out)
			}
		})
	}
}

func TestHTML(t *testing.T) {
	for i, tt := range tests {
		t.Run(fmt.Sprint(i), func(t *testing.T) {
			tmpl := htmltemplate.New("")
			err := Parse(tmpl, tt.in)
			var out string
			if err != nil {
				out = "PARSE: " + err.Error()
			} else {
				var buf bytes.Buffer
				err := tmpl.Execute(&buf, nil)
				if err != nil {
					out = "EXEC: " + err.Error()
				} else {
					out = strings.TrimSpace(buf.String())
				}
			}
			if out != tt.out {
				t.Errorf("have: %s\nwant: %s", out, tt.out)
			}
		})
	}
}

func TestGlob(t *testing.T) {
	tmpl := texttemplate.New("")
	MustParseGlob(tmpl, "testdata/*.tmpl")
	texttemplate.Must(tmpl.Parse("{{x .}}"))

	var buf bytes.Buffer
	must(tmpl.Execute(&buf, []int{1, 2, 3}))
	out := strings.TrimSpace(buf.String())
	if out != "y" {
		t.Fatalf("out = %q, want %q", out, "y")
	}
}

func TestFuncs(t *testing.T) {
	tmpl := htmltemplate.New("")
	MustParseGlob(tmpl, "testdata/*.tmpl")
	htmltemplate.Must(tmpl.Parse("{{x .}}"))

	tmpl2 := htmltemplate.Must(tmpl.Clone())
	if err := Funcs(tmpl2); err != nil {
		t.Fatal(err)
	}
	tmpl2.Execute(new(bytes.Buffer), nil)

	if _, err := tmpl.Clone(); err != nil {
		// Happens if you forget to call Funcs above:
		//	cannot Clone "" after it has executed
		t.Fatal(err)
	}
}