2017-03-13 18:55:02 -04:00
|
|
|
// 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 (
|
|
|
|
"testing"
|
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2017-04-30 15:52:56 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-03-13 18:55:02 -04:00
|
|
|
)
|
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
type templateFinder int
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
func (templateFinder) Lookup(name string) *tpl.TemplateAdapter {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
func (templateFinder) GetFuncs() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"print": fmt.Sprint,
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
func TestApply(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
ns := New(&deps.Deps{Tmpl: new(templateFinder)})
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
strings := []interface{}{"a\n", "b\n"}
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
result, err := ns.Apply(strings, "print", "a", "b", "c")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []interface{}{"abc", "abc"}, result, "testing variadic")
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
_, err = ns.Apply(strings, "apply", ".")
|
|
|
|
require.Error(t, err)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
var nilErr *error
|
|
|
|
_, err = ns.Apply(nilErr, "chomp", ".")
|
2017-04-30 15:52:56 -04:00
|
|
|
require.Error(t, err)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
_, err = ns.Apply(strings, "dobedobedo", ".")
|
2017-04-30 15:52:56 -04:00
|
|
|
require.Error(t, err)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
_, err = ns.Apply(strings, "foo.Chomp", "c\n")
|
|
|
|
if err == nil {
|
2017-04-30 15:52:56 -04:00
|
|
|
t.Errorf("apply with unknown func should fail")
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|