2022-02-17 10:51:19 -05:00
|
|
|
// Copyright 2022 The Hugo Authors. All rights reserved.
|
2019-12-10 13:56:44 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-02-17 10:51:19 -05:00
|
|
|
"context"
|
2019-12-10 13:56:44 -05:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
qt "github.com/frankban/quicktest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestStruct struct {
|
|
|
|
S string
|
|
|
|
M map[string]string
|
|
|
|
}
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
func (t TestStruct) Hello1(arg string) string {
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TestStruct) Hello2(arg1, arg2 string) string {
|
|
|
|
return arg1 + " " + arg2
|
|
|
|
}
|
|
|
|
|
2022-02-17 10:51:19 -05:00
|
|
|
type execHelper struct{}
|
|
|
|
|
|
|
|
func (e *execHelper) Init(ctx context.Context, tmpl Preparer) {
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:51:19 -05:00
|
|
|
func (e *execHelper) GetFunc(ctx context.Context, tmpl Preparer, name string) (reflect.Value, reflect.Value, bool) {
|
2019-12-10 13:56:44 -05:00
|
|
|
if name == "print" {
|
2022-02-17 10:51:19 -05:00
|
|
|
return zero, zero, false
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
return reflect.ValueOf(func(s string) string {
|
|
|
|
return "hello " + s
|
2022-02-17 10:51:19 -05:00
|
|
|
}), zero, true
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2022-02-17 10:51:19 -05:00
|
|
|
func (e *execHelper) GetMapValue(ctx context.Context, tmpl Preparer, m, key reflect.Value) (reflect.Value, bool) {
|
2019-12-10 13:56:44 -05:00
|
|
|
key = reflect.ValueOf(strings.ToLower(key.String()))
|
|
|
|
return m.MapIndex(key), true
|
|
|
|
}
|
|
|
|
|
2022-02-17 10:51:19 -05:00
|
|
|
func (e *execHelper) GetMethod(ctx context.Context, tmpl Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) {
|
2019-11-27 07:42:36 -05:00
|
|
|
if name != "Hello1" {
|
|
|
|
return zero, zero
|
|
|
|
}
|
|
|
|
m := receiver.MethodByName("Hello2")
|
|
|
|
return m, reflect.ValueOf("v2")
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func TestTemplateExecutor(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
templ, err := New("").Parse(`
|
|
|
|
{{ print "foo" }}
|
|
|
|
{{ printf "hugo" }}
|
|
|
|
Map: {{ .M.A }}
|
2019-11-27 07:42:36 -05:00
|
|
|
Method: {{ .Hello1 "v1" }}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
|
|
|
ex := NewExecuter(&execHelper{})
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
data := TestStruct{S: "sv", M: map[string]string{"a": "av"}}
|
|
|
|
|
2022-02-17 10:51:19 -05:00
|
|
|
c.Assert(ex.ExecuteWithContext(context.Background(), templ, &b, data), qt.IsNil)
|
2019-12-10 13:56:44 -05:00
|
|
|
got := b.String()
|
|
|
|
|
|
|
|
c.Assert(got, qt.Contains, "foo")
|
|
|
|
c.Assert(got, qt.Contains, "hello hugo")
|
|
|
|
c.Assert(got, qt.Contains, "Map: av")
|
2019-11-27 07:42:36 -05:00
|
|
|
c.Assert(got, qt.Contains, "Method: v2 v1")
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|