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

@@ -769,7 +769,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,
@@ -861,7 +861,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)
@@ -1002,7 +1002,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)
@@ -1196,33 +1196,39 @@ var cmpTests = []cmpTest{
{"eq .Iface1 .Iface1", "true", true},
{"eq .Iface1 .Iface2", "false", true},
{"eq .Iface2 .Iface2", "true", 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, Iface2 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)
@@ -1254,7 +1260,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>" // NOTE: not in html/template, get empty string
err = tmpl.Execute(&b, data)
if err != nil {
@@ -1423,7 +1429,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)
}
@@ -1529,7 +1535,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)
@@ -1585,7 +1591,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
@@ -1680,7 +1686,7 @@ func TestIssue31810(t *testing.T) {
t.Skip("broken in html/template")
// 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 {