tpl/collections: Make it a package that stands on its own

See #3042
This commit is contained in:
Bjørn Erik Pedersen
2017-04-30 21:52:56 +02:00
parent a3bf118eaa
commit 8a49c0b3b8
15 changed files with 156 additions and 141 deletions

View File

@@ -14,79 +14,51 @@
package collections
import (
"fmt"
"html/template"
"testing"
"fmt"
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/strings"
"github.com/stretchr/testify/assert"
"github.com/spf13/hugo/tpl"
"github.com/stretchr/testify/require"
)
type templateFinder int
func (templateFinder) Lookup(name string) *tpl.TemplateAdapter {
return nil
}
func (templateFinder) GetFuncs() map[string]interface{} {
return map[string]interface{}{
"print": fmt.Sprint,
}
}
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,
})
ns := New(&deps.Deps{Tmpl: new(templateFinder)})
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, err := ns.Apply(strings, "print", "a", "b", "c")
require.NoError(t, err)
require.Equal(t, []interface{}{"abc", "abc"}, result, "testing variadic")
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")
}
_, err = ns.Apply(strings, "apply", ".")
require.Error(t, err)
var nilErr *error
_, err = ns.Apply(nilErr, "chomp", ".")
if err == nil {
t.Errorf("apply with nil in seq should fail")
}
require.Error(t, err)
_, err = ns.Apply(strings, "dobedobedo", ".")
require.Error(t, err)
_, err = ns.Apply(strings, "foo.Chomp", "c\n")
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")
}
}