github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/tpl/collections/apply_test.go (about)

     1  // Copyright 2019 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 collections
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"io"
    20  	"reflect"
    21  	"testing"
    22  
    23  	qt "github.com/frankban/quicktest"
    24  	"github.com/gohugoio/hugo/config"
    25  	"github.com/gohugoio/hugo/deps"
    26  	"github.com/gohugoio/hugo/langs"
    27  	"github.com/gohugoio/hugo/output"
    28  	"github.com/gohugoio/hugo/tpl"
    29  )
    30  
    31  type templateFinder int
    32  
    33  func (templateFinder) Lookup(name string) (tpl.Template, bool) {
    34  	return nil, false
    35  }
    36  
    37  func (templateFinder) HasTemplate(name string) bool {
    38  	return false
    39  }
    40  
    41  func (templateFinder) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
    42  	return nil, false, false
    43  }
    44  
    45  func (templateFinder) LookupVariants(name string) []tpl.Template {
    46  	return nil
    47  }
    48  
    49  func (templateFinder) LookupLayout(d output.LayoutDescriptor, f output.Format) (tpl.Template, bool, error) {
    50  	return nil, false, nil
    51  }
    52  
    53  func (templateFinder) Execute(t tpl.Template, wr io.Writer, data any) error {
    54  	return nil
    55  }
    56  
    57  func (templateFinder) ExecuteWithContext(ctx context.Context, t tpl.Template, wr io.Writer, data any) error {
    58  	return nil
    59  }
    60  
    61  func (templateFinder) GetFunc(name string) (reflect.Value, bool) {
    62  	if name == "dobedobedo" {
    63  		return reflect.Value{}, false
    64  	}
    65  
    66  	return reflect.ValueOf(fmt.Sprint), true
    67  }
    68  
    69  func TestApply(t *testing.T) {
    70  	t.Parallel()
    71  	c := qt.New(t)
    72  	d := &deps.Deps{Language: langs.NewDefaultLanguage(config.New())}
    73  	d.SetTmpl(new(templateFinder))
    74  	ns := New(d)
    75  
    76  	strings := []any{"a\n", "b\n"}
    77  
    78  	ctx := context.Background()
    79  
    80  	result, err := ns.Apply(ctx, strings, "print", "a", "b", "c")
    81  	c.Assert(err, qt.IsNil)
    82  	c.Assert(result, qt.DeepEquals, []any{"abc", "abc"})
    83  
    84  	_, err = ns.Apply(ctx, strings, "apply", ".")
    85  	c.Assert(err, qt.Not(qt.IsNil))
    86  
    87  	var nilErr *error
    88  	_, err = ns.Apply(ctx, nilErr, "chomp", ".")
    89  	c.Assert(err, qt.Not(qt.IsNil))
    90  
    91  	_, err = ns.Apply(ctx, strings, "dobedobedo", ".")
    92  	c.Assert(err, qt.Not(qt.IsNil))
    93  
    94  	_, err = ns.Apply(ctx, strings, "foo.Chomp", "c\n")
    95  	if err == nil {
    96  		t.Errorf("apply with unknown func should fail")
    97  	}
    98  }