tpl/internal: Sync go_templates

Closes #10411
This commit is contained in:
Bjørn Erik Pedersen
2022-11-14 19:13:09 +01:00
parent 58a98c7758
commit f6ab9553f4
34 changed files with 739 additions and 514 deletions

View File

@@ -695,6 +695,8 @@ var execTests = []execTest{
{"bug18a", "{{eq . '.'}}", "true", '.', true},
{"bug18b", "{{eq . 'e'}}", "true", 'e', true},
{"bug18c", "{{eq . 'P'}}", "true", 'P', true},
{"issue56490", "{{$i := 0}}{{$x := 0}}{{range $i = .AI}}{{end}}{{$i}}", "5", tVal, true},
}
func zeroArgs() string {
@@ -775,7 +777,7 @@ func mapOfThree() any {
}
func testExecute(execTests []execTest, template *Template, t *testing.T) {
b := new(bytes.Buffer)
b := new(strings.Builder)
funcs := FuncMap{
"add": add,
"count": count,
@@ -864,7 +866,7 @@ func TestDelims(t *testing.T) {
if err != nil {
t.Fatalf("delim %q text %q parse err %s", left, text, err)
}
var b = new(bytes.Buffer)
var b = new(strings.Builder)
err = tmpl.Execute(b, value)
if err != nil {
t.Fatalf("delim %q exec err %s", left, err)
@@ -1027,7 +1029,7 @@ func TestTree(t *testing.T) {
if err != nil {
t.Fatal("parse error:", err)
}
var b bytes.Buffer
var b strings.Builder
const expect = "[1[2[3[4]][5[6]]][7[8[9]][10[11]]]]"
// First by looking up the template.
err = tmpl.Lookup("tree").Execute(&b, tree)
@@ -1223,33 +1225,39 @@ var cmpTests = []cmpTest{
{"eq .NilIface .Iface1", "false", true},
{"eq .NilIface 0", "false", true},
{"eq 0 .NilIface", "false", true},
{"eq .Map .Map", "true", true}, // Uncomparable types but nil is OK.
{"eq .Map nil", "true", true}, // Uncomparable types but nil is OK.
{"eq nil .Map", "true", true}, // Uncomparable types but nil is OK.
{"eq .Map .NonNilMap", "false", true}, // Uncomparable types but nil is OK.
// Errors
{"eq `xy` 1", "", false}, // Different types.
{"eq 2 2.0", "", false}, // Different types.
{"lt true true", "", false}, // Unordered types.
{"lt 1+0i 1+0i", "", false}, // Unordered types.
{"eq .Ptr 1", "", false}, // Incompatible types.
{"eq .Ptr .NegOne", "", false}, // Incompatible types.
{"eq .Map .Map", "", false}, // Uncomparable types.
{"eq .Map .V1", "", false}, // Uncomparable types.
{"eq `xy` 1", "", false}, // Different types.
{"eq 2 2.0", "", false}, // Different types.
{"lt true true", "", false}, // Unordered types.
{"lt 1+0i 1+0i", "", false}, // Unordered types.
{"eq .Ptr 1", "", false}, // Incompatible types.
{"eq .Ptr .NegOne", "", false}, // Incompatible types.
{"eq .Map .V1", "", false}, // Uncomparable types.
{"eq .NonNilMap .NonNilMap", "", false}, // Uncomparable types.
}
func TestComparison(t *testing.T) {
b := new(bytes.Buffer)
b := new(strings.Builder)
var cmpStruct = struct {
Uthree, Ufour uint
NegOne, Three int
Ptr, NilPtr *int
NonNilMap map[int]int
Map map[int]int
V1, V2 V
Iface1, NilIface fmt.Stringer
}{
Uthree: 3,
Ufour: 4,
NegOne: -1,
Three: 3,
Ptr: new(int),
Iface1: b,
Uthree: 3,
Ufour: 4,
NegOne: -1,
Three: 3,
Ptr: new(int),
NonNilMap: make(map[int]int),
Iface1: b,
}
for _, test := range cmpTests {
text := fmt.Sprintf("{{if %s}}true{{else}}false{{end}}", test.expr)
@@ -1281,7 +1289,7 @@ func TestMissingMapKey(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var b bytes.Buffer
var b strings.Builder
// By default, just get "<no value>"
err = tmpl.Execute(&b, data)
if err != nil {
@@ -1451,7 +1459,7 @@ func TestBlock(t *testing.T) {
t.Fatal(err)
}
var buf bytes.Buffer
var buf strings.Builder
if err := tmpl.Execute(&buf, "hello"); err != nil {
t.Fatal(err)
}
@@ -1557,7 +1565,7 @@ func TestAddrOfIndex(t *testing.T) {
}
for _, text := range texts {
tmpl := Must(New("tmpl").Parse(text))
var buf bytes.Buffer
var buf strings.Builder
err := tmpl.Execute(&buf, reflect.ValueOf([]V{{1}}))
if err != nil {
t.Fatalf("%s: Execute: %v", text, err)
@@ -1613,7 +1621,7 @@ func TestInterfaceValues(t *testing.T) {
for _, tt := range tests {
tmpl := Must(New("tmpl").Parse(tt.text))
var buf bytes.Buffer
var buf strings.Builder
err := tmpl.Execute(&buf, map[string]any{
"PlusOne": func(n int) int {
return n + 1
@@ -1706,7 +1714,7 @@ func TestExecutePanicDuringCall(t *testing.T) {
// Issue 31810. Check that a parenthesized first argument behaves properly.
func TestIssue31810(t *testing.T) {
// A simple value with no arguments is fine.
var b bytes.Buffer
var b strings.Builder
const text = "{{ (.) }}"
tmpl, err := New("").Parse(text)
if err != nil {