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