| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package template |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"reflect" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
|
| 24 |
qt "github.com/frankban/quicktest" |
| 25 |
"github.com/gohugoio/hugo/common/hmaps" |
| 26 |
"github.com/gohugoio/hugo/common/hreflect" |
| 27 |
) |
| 28 |
|
| 29 |
type TestStruct struct { |
| 30 |
S string |
| 31 |
M map[string]string |
| 32 |
} |
| 33 |
|
| 34 |
func (t TestStruct) Hello1(arg string) string { |
| 35 |
return arg |
| 36 |
} |
| 37 |
|
| 38 |
func (t TestStruct) Hello2(arg1, arg2 string) string { |
| 39 |
return arg1 + " " + arg2 |
| 40 |
} |
| 41 |
|
| 42 |
type execHelper struct{} |
| 43 |
|
| 44 |
func (e *execHelper) Init(ctx context.Context, tmpl Preparer) { |
| 45 |
} |
| 46 |
|
| 47 |
func (e *execHelper) GetFunc(ctx context.Context, tmpl Preparer, name string) (reflect.Value, reflect.Value, bool) { |
| 48 |
if name == "print" { |
| 49 |
return zero, zero, false |
| 50 |
} |
| 51 |
return reflect.ValueOf(func(s string) string { |
| 52 |
return "hello " + s |
| 53 |
}), zero, true |
| 54 |
} |
| 55 |
|
| 56 |
func (e *execHelper) GetMapValue(ctx context.Context, tmpl Preparer, m, key reflect.Value) (reflect.Value, bool) { |
| 57 |
key = reflect.ValueOf(strings.ToLower(key.String())) |
| 58 |
return m.MapIndex(key), true |
| 59 |
} |
| 60 |
|
| 61 |
func (e *execHelper) GetMethod(ctx context.Context, tmpl Preparer, receiver reflect.Value, name string) (reflect.Value, reflect.Value) { |
| 62 |
if name != "Hello1" { |
| 63 |
return zero, zero |
| 64 |
} |
| 65 |
m := hreflect.GetMethodByName(receiver, "Hello2") |
| 66 |
return m, reflect.ValueOf("v2") |
| 67 |
} |
| 68 |
|
| 69 |
func (e *execHelper) OnCalled(ctx context.Context, tmpl Preparer, name string, args []reflect.Value, returnValue reflect.Value) { |
| 70 |
} |
| 71 |
|
| 72 |
func TestTemplateExecutor(t *testing.T) { |
| 73 |
c := qt.New(t) |
| 74 |
|
| 75 |
templ, err := New("").Parse(` |
| 76 |
{{ print "foo" }} |
| 77 |
{{ printf "hugo" }} |
| 78 |
Map: {{ .M.A }} |
| 79 |
Method: {{ .Hello1 "v1" }} |
| 80 |
|
| 81 |
`) |
| 82 |
|
| 83 |
c.Assert(err, qt.IsNil) |
| 84 |
|
| 85 |
ex := NewExecuter(&execHelper{}) |
| 86 |
|
| 87 |
var b bytes.Buffer |
| 88 |
data := TestStruct{S: "sv", M: map[string]string{"a": "av"}} |
| 89 |
|
| 90 |
c.Assert(ex.ExecuteWithContext(context.Background(), templ, &b, data), qt.IsNil) |
| 91 |
got := b.String() |
| 92 |
|
| 93 |
c.Assert(got, qt.Contains, "foo") |
| 94 |
c.Assert(got, qt.Contains, "hello hugo") |
| 95 |
c.Assert(got, qt.Contains, "Map: av") |
| 96 |
c.Assert(got, qt.Contains, "Method: v2 v1") |
| 97 |
} |
| 98 |
|
| 99 |
// See issue 15385. |
| 100 |
func TestSafeCallFastPaths(t *testing.T) { |
| 101 |
c := qt.New(t) |
| 102 |
|
| 103 |
vals := func(args ...any) []reflect.Value { |
| 104 |
var argv []reflect.Value |
| 105 |
for _, a := range args { |
| 106 |
argv = append(argv, reflect.ValueOf(a)) |
| 107 |
} |
| 108 |
return argv |
| 109 |
} |
| 110 |
ctx := context.WithValue(context.Background(), "k", "v") |
| 111 |
errBoom := errors.New("boom") |
| 112 |
m := map[string]any{"a": 1} |
| 113 |
params := hmaps.Params{"a": 1} |
| 114 |
|
| 115 |
for _, test := range []struct { |
| 116 |
name string |
| 117 |
fn any |
| 118 |
args []reflect.Value |
| 119 |
expect any |
| 120 |
err string |
| 121 |
allocs float64 |
| 122 |
}{ |
| 123 |
{"variadic string", func(args ...any) string { return args[0].(string) }, vals("a", 1), "a", "", 2}, |
| 124 |
{"string", func() string { return "s" }, nil, "s", "", 1}, |
| 125 |
{"ctx variadic any error", func(c context.Context, args ...any) (any, error) { return c.Value("k"), nil }, vals(ctx, "x"), "v", "", 1}, |
| 126 |
{"ctx variadic nil", func(c context.Context, args ...any) (any, error) { return nil, nil }, vals(ctx), nil, "", 0}, |
| 127 |
{"ctx variadic error", func(c context.Context, args ...any) (any, error) { return nil, errBoom }, vals(ctx), nil, "boom", 0}, |
| 128 |
{"any variadic bool", func(a any, args ...any) bool { return a == args[0] }, vals(1, 1), true, "", 1}, |
| 129 |
{"params", func() hmaps.Params { return params }, nil, params, "", 0}, |
| 130 |
{"any any bool error", func(a, b any) (bool, error) { return a == b, nil }, vals("a", "b"), false, "", 0}, |
| 131 |
{"any string error", func(a any) (string, error) { return "", errBoom }, vals("a"), "", "boom", 0}, |
| 132 |
{"ctx string variadic", func(c context.Context, s string, args ...any) (any, error) { return args[0], nil }, vals(ctx, "s", "1"), "1", "", 1}, |
| 133 |
{"reflect value bool", func(v reflect.Value) bool { return v.Int() == 1 }, []reflect.Value{reflect.ValueOf(reflect.ValueOf(1))}, true, "", 0}, |
| 134 |
{"map", func() map[string]any { return m }, nil, m, "", 0}, |
| 135 |
{"variadic map error", func(args ...any) (map[string]any, error) { return m, nil }, vals(1), m, "", 1}, |
| 136 |
{"any", func() any { return 42 }, nil, 42, "", 0}, |
| 137 |
{"bool", func() bool { return true }, nil, true, "", 0}, |
| 138 |
{"string variadic", func(s string, args ...any) string { return s }, vals("s", "1"), "s", "", 2}, |
| 139 |
{"any variadic any error", func(a any, args ...any) (any, error) { return args[0], nil }, vals(1, "b"), "b", "", 1}, |
| 140 |
{"panic", func() string { panic("bad") }, nil, nil, "bad", -1}, |
| 141 |
{"slow path", func(a, b string) string { return a + b }, vals("a", "b"), "ab", "", -1}, |
| 142 |
} { |
| 143 |
c.Run(test.name, func(c *qt.C) { |
| 144 |
fn := reflect.ValueOf(test.fn) |
| 145 |
v, err := safeCall(fn, test.args) |
| 146 |
if test.err != "" { |
| 147 |
c.Assert(err, qt.ErrorMatches, test.err) |
| 148 |
} else { |
| 149 |
c.Assert(err, qt.IsNil) |
| 150 |
c.Assert(v.IsValid(), qt.IsTrue) |
| 151 |
if test.expect == nil { |
| 152 |
c.Assert(v.Interface(), qt.IsNil) |
| 153 |
} else { |
| 154 |
c.Assert(v.Interface(), qt.DeepEquals, test.expect) |
| 155 |
} |
| 156 |
} |
| 157 |
if test.allocs >= 0 { |
| 158 |
allocs := testing.AllocsPerRun(100, func() { safeCall(fn, test.args) }) |
| 159 |
c.Assert(allocs, qt.Equals, test.allocs) |
| 160 |
} |
| 161 |
}) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func BenchmarkSafeCall(b *testing.B) { |
| 166 |
for _, bench := range []struct { |
| 167 |
name string |
| 168 |
fn any |
| 169 |
args []reflect.Value |
| 170 |
}{ |
| 171 |
{"variadic string", func(args ...any) string { return "" }, []reflect.Value{reflect.ValueOf("a")}}, |
| 172 |
{"string", func() string { return "" }, nil}, |
| 173 |
{"ctx variadic any error", func(context.Context, ...any) (any, error) { return nil, nil }, []reflect.Value{reflect.ValueOf(context.Background()), reflect.ValueOf("a")}}, |
| 174 |
{"slow path", func(a, b string) string { return a + b }, []reflect.Value{reflect.ValueOf("a"), reflect.ValueOf("b")}}, |
| 175 |
} { |
| 176 |
fn := reflect.ValueOf(bench.fn) |
| 177 |
b.Run(bench.name, func(b *testing.B) { |
| 178 |
for b.Loop() { |
| 179 |
safeCall(fn, bench.args) |
| 180 |
} |
| 181 |
}) |
| 182 |
} |
| 183 |
} |