github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/internal/go_templates/texttemplate/hugo_template_test.go (about)

     1  // Copyright 2022 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  	"reflect"
    20  	"strings"
    21  	"testing"
    22  
    23  	qt "github.com/frankban/quicktest"
    24  )
    25  
    26  type TestStruct struct {
    27  	S string
    28  	M map[string]string
    29  }
    30  
    31  func (t TestStruct) Hello1(arg string) string {
    32  	return arg
    33  }
    34  
    35  func (t TestStruct) Hello2(arg1, arg2 string) string {
    36  	return arg1 + " " + arg2
    37  }
    38  
    39  type execHelper struct{}
    40  
    41  func (e *execHelper) Init(ctx context.Context, tmpl Preparer) {
    42  }
    43  
    44  func (e *execHelper) GetFunc(ctx context.Context, tmpl Preparer, name string) (reflect.Value, reflect.Value, bool) {
    45  	if name == "print" {
    46  		return zero, zero, false
    47  	}
    48  	return reflect.ValueOf(func(s string) string {
    49  		return "hello " + s
    50  	}), zero, true
    51  }
    52  
    53  func (e *execHelper) GetMapValue(ctx context.Context, tmpl Preparer, m, key reflect.Value) (reflect.Value, bool) {
    54  	key = reflect.ValueOf(strings.ToLower(key.String()))
    55  	return m.MapIndex(key), true
    56  }
    57  
    58  func (e *execHelper) GetMethod(ctx context.Context, tmpl Preparer, receiver reflect.Value, name string) (method reflect.Value, firstArg reflect.Value) {
    59  	if name != "Hello1" {
    60  		return zero, zero
    61  	}
    62  	m := receiver.MethodByName("Hello2")
    63  	return m, reflect.ValueOf("v2")
    64  }
    65  
    66  func TestTemplateExecutor(t *testing.T) {
    67  	c := qt.New(t)
    68  
    69  	templ, err := New("").Parse(`
    70  {{ print "foo" }}
    71  {{ printf "hugo" }}
    72  Map: {{ .M.A }}
    73  Method: {{ .Hello1 "v1" }}
    74  
    75  `)
    76  
    77  	c.Assert(err, qt.IsNil)
    78  
    79  	ex := NewExecuter(&execHelper{})
    80  
    81  	var b bytes.Buffer
    82  	data := TestStruct{S: "sv", M: map[string]string{"a": "av"}}
    83  
    84  	c.Assert(ex.ExecuteWithContext(context.Background(), templ, &b, data), qt.IsNil)
    85  	got := b.String()
    86  
    87  	c.Assert(got, qt.Contains, "foo")
    88  	c.Assert(got, qt.Contains, "hello hugo")
    89  	c.Assert(got, qt.Contains, "Map: av")
    90  	c.Assert(got, qt.Contains, "Method: v2 v1")
    91  }