github.com/whatlly/hugo@v0.47.1/tpl/collections/apply_test.go (about)

     1  // Copyright 2017 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  	"testing"
    18  
    19  	"fmt"
    20  
    21  	"github.com/gohugoio/hugo/deps"
    22  	"github.com/gohugoio/hugo/tpl"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  type templateFinder int
    27  
    28  func (templateFinder) Lookup(name string) (tpl.Template, bool) {
    29  	return nil, false
    30  }
    31  
    32  func (templateFinder) GetFuncs() map[string]interface{} {
    33  	return map[string]interface{}{
    34  		"print": fmt.Sprint,
    35  	}
    36  }
    37  
    38  func TestApply(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	ns := New(&deps.Deps{Tmpl: new(templateFinder)})
    42  
    43  	strings := []interface{}{"a\n", "b\n"}
    44  
    45  	result, err := ns.Apply(strings, "print", "a", "b", "c")
    46  	require.NoError(t, err)
    47  	require.Equal(t, []interface{}{"abc", "abc"}, result, "testing variadic")
    48  
    49  	_, err = ns.Apply(strings, "apply", ".")
    50  	require.Error(t, err)
    51  
    52  	var nilErr *error
    53  	_, err = ns.Apply(nilErr, "chomp", ".")
    54  	require.Error(t, err)
    55  
    56  	_, err = ns.Apply(strings, "dobedobedo", ".")
    57  	require.Error(t, err)
    58  
    59  	_, err = ns.Apply(strings, "foo.Chomp", "c\n")
    60  	if err == nil {
    61  		t.Errorf("apply with unknown func should fail")
    62  	}
    63  
    64  }