hugo/tpl/collections/apply_test.go
Cameron Moore de7c32a1a8 tpl: Add template function namespaces
This commit moves almost all of the template functions into separate
packages under tpl/ and adds a namespace framework.  All changes should
be backward compatible for end users, as all existing function names in
the template funcMap are left intact.

Seq and DoArithmatic have been moved out of the helpers package and into
template namespaces.

Most of the tests involved have been refactored, and many new tests have
been written.  There's still work to do, but this is a big improvement.

I got a little overzealous and added some new functions along the way:

- strings.Contains
- strings.ContainsAny
- strings.HasSuffix
- strings.TrimPrefix
- strings.TrimSuffix

Documentation is forthcoming.

Fixes #3042
2017-04-30 10:56:38 +02:00

92 lines
2.5 KiB
Go

// Copyright 2017 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package collections
import (
"fmt"
"html/template"
"testing"
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/strings"
"github.com/stretchr/testify/assert"
)
func TestApply(t *testing.T) {
t.Parallel()
hstrings := strings.New(&deps.Deps{})
ns := New(&deps.Deps{})
ns.Funcs(template.FuncMap{
"apply": ns.Apply,
"chomp": hstrings.Chomp,
"strings": hstrings,
"print": fmt.Sprint,
})
strings := []interface{}{"a\n", "b\n"}
noStringers := []interface{}{tstNoStringer{}, tstNoStringer{}}
result, _ := ns.Apply(strings, "chomp", ".")
assert.Equal(t, []interface{}{template.HTML("a"), template.HTML("b")}, result)
result, _ = ns.Apply(strings, "chomp", "c\n")
assert.Equal(t, []interface{}{template.HTML("c"), template.HTML("c")}, result)
result, _ = ns.Apply(strings, "strings.Chomp", "c\n")
assert.Equal(t, []interface{}{template.HTML("c"), template.HTML("c")}, result)
result, _ = ns.Apply(strings, "print", "a", "b", "c")
assert.Equal(t, []interface{}{"abc", "abc"}, result, "testing variadic")
result, _ = ns.Apply(nil, "chomp", ".")
assert.Equal(t, []interface{}{}, result)
_, err := ns.Apply(strings, "apply", ".")
if err == nil {
t.Errorf("apply with apply should fail")
}
var nilErr *error
_, err = ns.Apply(nilErr, "chomp", ".")
if err == nil {
t.Errorf("apply with nil in seq should fail")
}
_, err = ns.Apply(strings, "dobedobedo", ".")
if err == nil {
t.Errorf("apply with unknown func should fail")
}
_, err = ns.Apply(noStringers, "chomp", ".")
if err == nil {
t.Errorf("apply when func fails should fail")
}
_, err = ns.Apply(tstNoStringer{}, "chomp", ".")
if err == nil {
t.Errorf("apply with non-sequence should fail")
}
_, err = ns.Apply(strings, "foo.Chomp", "c\n")
if err == nil {
t.Errorf("apply with unknown namespace should fail")
}
_, err = ns.Apply(strings, "strings.Foo", "c\n")
if err == nil {
t.Errorf("apply with unknown namespace method should fail")
}
}