mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
@@ -13,6 +13,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"iter"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -412,6 +413,9 @@ var execTests = []execTest{
|
||||
{"Interface Call", `{{stringer .S}}`, "foozle", map[string]any{"S": bytes.NewBufferString("foozle")}, true},
|
||||
{".ErrFunc", "{{call .ErrFunc}}", "bla", tVal, true},
|
||||
{"call nil", "{{call nil}}", "", tVal, false},
|
||||
{"empty call", "{{call}}", "", tVal, false},
|
||||
{"empty call after pipe valid", "{{.ErrFunc | call}}", "bla", tVal, true},
|
||||
{"empty call after pipe invalid", "{{1 | call}}", "", tVal, false},
|
||||
|
||||
// Erroneous function calls (check args).
|
||||
{".BinaryFuncTooFew", "{{call .BinaryFunc `1`}}", "", tVal, false},
|
||||
@@ -618,6 +622,30 @@ var execTests = []execTest{
|
||||
{"declare in range", "{{range $x := .PSI}}<{{$foo:=$x}}{{$x}}>{{end}}", "<21><22><23>", tVal, true},
|
||||
{"range count", `{{range $i, $x := count 5}}[{{$i}}]{{$x}}{{end}}`, "[0]a[1]b[2]c[3]d[4]e", tVal, true},
|
||||
{"range nil count", `{{range $i, $x := count 0}}{{else}}empty{{end}}`, "empty", tVal, true},
|
||||
{"range iter.Seq[int]", `{{range $i := .}}{{$i}}{{end}}`, "01", fVal1(2), true},
|
||||
{"i = range iter.Seq[int]", `{{$i := 0}}{{range $i = .}}{{$i}}{{end}}`, "01", fVal1(2), true},
|
||||
{"range iter.Seq[int] over two var", `{{range $i, $c := .}}{{$c}}{{end}}`, "", fVal1(2), false},
|
||||
{"i, c := range iter.Seq2[int,int]", `{{range $i, $c := .}}{{$i}}{{$c}}{{end}}`, "0112", fVal2(2), true},
|
||||
{"i, c = range iter.Seq2[int,int]", `{{$i := 0}}{{$c := 0}}{{range $i, $c = .}}{{$i}}{{$c}}{{end}}`, "0112", fVal2(2), true},
|
||||
{"i = range iter.Seq2[int,int]", `{{$i := 0}}{{range $i = .}}{{$i}}{{end}}`, "01", fVal2(2), true},
|
||||
{"i := range iter.Seq2[int,int]", `{{range $i := .}}{{$i}}{{end}}`, "01", fVal2(2), true},
|
||||
{"i,c,x range iter.Seq2[int,int]", `{{$i := 0}}{{$c := 0}}{{$x := 0}}{{range $i, $c = .}}{{$i}}{{$c}}{{end}}`, "0112", fVal2(2), true},
|
||||
{"i,x range iter.Seq[int]", `{{$i := 0}}{{$x := 0}}{{range $i = .}}{{$i}}{{end}}`, "01", fVal1(2), true},
|
||||
{"range iter.Seq[int] else", `{{range $i := .}}{{$i}}{{else}}empty{{end}}`, "empty", fVal1(0), true},
|
||||
{"range iter.Seq2[int,int] else", `{{range $i := .}}{{$i}}{{else}}empty{{end}}`, "empty", fVal2(0), true},
|
||||
{"range int8", rangeTestInt, rangeTestData[int8](), int8(5), true},
|
||||
{"range int16", rangeTestInt, rangeTestData[int16](), int16(5), true},
|
||||
{"range int32", rangeTestInt, rangeTestData[int32](), int32(5), true},
|
||||
{"range int64", rangeTestInt, rangeTestData[int64](), int64(5), true},
|
||||
{"range int", rangeTestInt, rangeTestData[int](), int(5), true},
|
||||
{"range uint8", rangeTestInt, rangeTestData[uint8](), uint8(5), true},
|
||||
{"range uint16", rangeTestInt, rangeTestData[uint16](), uint16(5), true},
|
||||
{"range uint32", rangeTestInt, rangeTestData[uint32](), uint32(5), true},
|
||||
{"range uint64", rangeTestInt, rangeTestData[uint64](), uint64(5), true},
|
||||
{"range uint", rangeTestInt, rangeTestData[uint](), uint(5), true},
|
||||
{"range uintptr", rangeTestInt, rangeTestData[uintptr](), uintptr(5), true},
|
||||
{"range uintptr(0)", `{{range $v := .}}{{print $v}}{{else}}empty{{end}}`, "empty", uintptr(0), true},
|
||||
{"range 5", `{{range $v := 5}}{{printf "%T%d" $v $v}}{{end}}`, rangeTestData[int](), nil, true},
|
||||
|
||||
// Cute examples.
|
||||
{"or as if true", `{{or .SI "slice is empty"}}`, "[3 4 5]", tVal, true},
|
||||
@@ -722,6 +750,37 @@ var execTests = []execTest{
|
||||
{"issue60801", "{{$k := 0}}{{$v := 0}}{{range $k, $v = .AI}}{{$k}}={{$v}} {{end}}", "0=3 1=4 2=5 ", tVal, true},
|
||||
}
|
||||
|
||||
func fVal1(i int) iter.Seq[int] {
|
||||
return func(yield func(int) bool) {
|
||||
for v := range i {
|
||||
if !yield(v) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fVal2(i int) iter.Seq2[int, int] {
|
||||
return func(yield func(int, int) bool) {
|
||||
for v := range i {
|
||||
if !yield(v, v+1) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const rangeTestInt = `{{range $v := .}}{{printf "%T%d" $v $v}}{{end}}`
|
||||
|
||||
func rangeTestData[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr]() string {
|
||||
I := T(5)
|
||||
var buf strings.Builder
|
||||
for i := T(0); i < I; i++ {
|
||||
fmt.Fprintf(&buf, "%T%d", i, i)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func zeroArgs() string {
|
||||
return "zeroArgs"
|
||||
}
|
||||
|
Reference in New Issue
Block a user