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