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