mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Merge branch 'gohugoio:master' into fix/js.Build-sourcemap-comment
This commit is contained in:
commit
4a8df5446b
2 changed files with 30 additions and 8 deletions
|
@ -231,8 +231,8 @@ func (n *Namespace) checkComparisonArgCount(min int, others ...any) bool {
|
||||||
// Conditional can be used as a ternary operator.
|
// Conditional can be used as a ternary operator.
|
||||||
//
|
//
|
||||||
// It returns v1 if cond is true, else v2.
|
// It returns v1 if cond is true, else v2.
|
||||||
func (n *Namespace) Conditional(cond bool, v1, v2 any) any {
|
func (n *Namespace) Conditional(cond any, v1, v2 any) any {
|
||||||
if cond {
|
if hreflect.IsTruthful(cond) {
|
||||||
return v1
|
return v1
|
||||||
}
|
}
|
||||||
return v2
|
return v2
|
||||||
|
|
|
@ -21,10 +21,9 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/htesting/hqt"
|
|
||||||
|
|
||||||
qt "github.com/frankban/quicktest"
|
qt "github.com/frankban/quicktest"
|
||||||
"github.com/gohugoio/hugo/common/hugo"
|
"github.com/gohugoio/hugo/common/hugo"
|
||||||
|
"github.com/gohugoio/hugo/htesting/hqt"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -447,12 +446,35 @@ func TestTimeUnix(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConditional(t *testing.T) {
|
func TestConditional(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
c := qt.New(t)
|
c := qt.New(t)
|
||||||
n := New(time.UTC, false)
|
ns := New(time.UTC, false)
|
||||||
a, b := "a", "b"
|
|
||||||
|
|
||||||
c.Assert(n.Conditional(true, a, b), qt.Equals, a)
|
type args struct {
|
||||||
c.Assert(n.Conditional(false, a, b), qt.Equals, b)
|
cond any
|
||||||
|
v1 any
|
||||||
|
v2 any
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want any
|
||||||
|
}{
|
||||||
|
{"a", args{cond: true, v1: "true", v2: "false"}, "true"},
|
||||||
|
{"b", args{cond: false, v1: "true", v2: "false"}, "false"},
|
||||||
|
{"c", args{cond: 1, v1: "true", v2: "false"}, "true"},
|
||||||
|
{"d", args{cond: 0, v1: "true", v2: "false"}, "false"},
|
||||||
|
{"e", args{cond: "foo", v1: "true", v2: "false"}, "true"},
|
||||||
|
{"f", args{cond: "", v1: "true", v2: "false"}, "false"},
|
||||||
|
{"g", args{cond: []int{6, 7}, v1: "true", v2: "false"}, "true"},
|
||||||
|
{"h", args{cond: []int{}, v1: "true", v2: "false"}, "false"},
|
||||||
|
{"i", args{cond: nil, v1: "true", v2: "false"}, "false"},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
c.Assert(ns.Conditional(tt.args.cond, tt.args.v1, tt.args.v2), qt.Equals, tt.want)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue 9462
|
// Issue 9462
|
||||||
|
|
Loading…
Reference in a new issue