tpl/collections: Make delimit return a string

Closes #10876
Closes #11502
This commit is contained in:
Bjørn Erik Pedersen 2023-10-28 11:10:27 +02:00
parent 705e3cd5f5
commit e54139c85b
3 changed files with 9 additions and 16 deletions

View file

@ -356,8 +356,8 @@ func normalizeExpected(ext, str string) string {
} }
func testAllMarkdownEnginesForPages(t *testing.T, func testAllMarkdownEnginesForPages(t *testing.T,
assertFunc func(t *testing.T, ext string, pages page.Pages), settings map[string]any, pageSources ...string) { assertFunc func(t *testing.T, ext string, pages page.Pages), settings map[string]any, pageSources ...string,
) {
engines := []struct { engines := []struct {
ext string ext string
shouldExecute func() bool shouldExecute func() bool
@ -643,7 +643,6 @@ Simple Page With Some Date`
} }
func TestPageRawContent(t *testing.T) { func TestPageRawContent(t *testing.T) {
files := ` files := `
-- hugo.toml -- -- hugo.toml --
-- content/basic.md -- -- content/basic.md --
@ -668,7 +667,6 @@ title: "empty"
b.AssertFileContent("public/basic/index.html", "|**basic**|") b.AssertFileContent("public/basic/index.html", "|**basic**|")
b.AssertFileContent("public/empty/index.html", "! title") b.AssertFileContent("public/empty/index.html", "! title")
} }
func TestPageWithShortCodeInSummary(t *testing.T) { func TestPageWithShortCodeInSummary(t *testing.T) {
@ -1954,5 +1952,4 @@ func TestRenderWithoutArgument(t *testing.T) {
).BuildE() ).BuildE()
b.Assert(err, qt.IsNotNil) b.Assert(err, qt.IsNotNil)
} }

View file

@ -17,16 +17,14 @@ package collections
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"html/template"
"math/rand" "math/rand"
"net/url" "net/url"
"reflect" "reflect"
"strings" "strings"
"time" "time"
"errors"
"github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/common/collections"
"github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/common/maps"
@ -101,7 +99,7 @@ func (ns *Namespace) After(n any, l any) (any, error) {
// Delimit takes a given list l and returns a string delimited by sep. // Delimit takes a given list l and returns a string delimited by sep.
// If last is passed to the function, it will be used as the final delimiter. // If last is passed to the function, it will be used as the final delimiter.
func (ns *Namespace) Delimit(ctx context.Context, l, sep any, last ...any) (template.HTML, error) { func (ns *Namespace) Delimit(ctx context.Context, l, sep any, last ...any) (string, error) {
d, err := cast.ToStringE(sep) d, err := cast.ToStringE(sep)
if err != nil { if err != nil {
return "", err return "", err
@ -154,7 +152,7 @@ func (ns *Namespace) Delimit(ctx context.Context, l, sep any, last ...any) (temp
return "", fmt.Errorf("can't iterate over %v", l) return "", fmt.Errorf("can't iterate over %v", l)
} }
return template.HTML(str), nil return str, nil
} }
// Dictionary creates a new map from the given parameters by // Dictionary creates a new map from the given parameters by

View file

@ -71,8 +71,7 @@ func TestAfter(t *testing.T) {
} }
} }
type tstGrouper struct { type tstGrouper struct{}
}
type tstGroupers []*tstGrouper type tstGroupers []*tstGrouper
@ -81,8 +80,7 @@ func (g tstGrouper) Group(key any, items any) (any, error) {
return fmt.Sprintf("%v(%d)", key, ilen), nil return fmt.Sprintf("%v(%d)", key, ilen), nil
} }
type tstGrouper2 struct { type tstGrouper2 struct{}
}
func (g *tstGrouper2) Group(key any, items any) (any, error) { func (g *tstGrouper2) Group(key any, items any) (any, error) {
ilen := reflect.ValueOf(items).Len() ilen := reflect.ValueOf(items).Len()
@ -134,7 +132,7 @@ func TestDelimit(t *testing.T) {
seq any seq any
delimiter any delimiter any
last any last any
expect template.HTML expect string
}{ }{
{[]string{"class1", "class2", "class3"}, " ", nil, "class1 class2 class3"}, {[]string{"class1", "class2", "class3"}, " ", nil, "class1 class2 class3"},
{[]int{1, 2, 3, 4, 5}, ",", nil, "1,2,3,4,5"}, {[]int{1, 2, 3, 4, 5}, ",", nil, "1,2,3,4,5"},
@ -163,7 +161,7 @@ func TestDelimit(t *testing.T) {
} { } {
errMsg := qt.Commentf("[%d] %v", i, test) errMsg := qt.Commentf("[%d] %v", i, test)
var result template.HTML var result string
var err error var err error
if test.last == nil { if test.last == nil {