mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-29 15:10:35 +03:00
parent
3336762939
commit
9262719092
2 changed files with 30 additions and 4 deletions
|
@ -95,10 +95,7 @@ func (n *Namespace) Eq(first interface{}, others ...interface{}) bool {
|
||||||
if n.caseInsensitive {
|
if n.caseInsensitive {
|
||||||
panic("caseInsensitive not implemented for Eq")
|
panic("caseInsensitive not implemented for Eq")
|
||||||
}
|
}
|
||||||
if len(others) == 0 {
|
n.checkComparisonArgCount(1, others...)
|
||||||
panic("missing arguments for comparison")
|
|
||||||
}
|
|
||||||
|
|
||||||
normalize := func(v interface{}) interface{} {
|
normalize := func(v interface{}) interface{} {
|
||||||
if types.IsNil(v) {
|
if types.IsNil(v) {
|
||||||
return nil
|
return nil
|
||||||
|
@ -145,6 +142,7 @@ func (n *Namespace) Eq(first interface{}, others ...interface{}) bool {
|
||||||
|
|
||||||
// Ne returns the boolean truth of arg1 != arg2 && arg1 != arg3 && arg1 != arg4.
|
// Ne returns the boolean truth of arg1 != arg2 && arg1 != arg3 && arg1 != arg4.
|
||||||
func (n *Namespace) Ne(first interface{}, others ...interface{}) bool {
|
func (n *Namespace) Ne(first interface{}, others ...interface{}) bool {
|
||||||
|
n.checkComparisonArgCount(1, others...)
|
||||||
for _, other := range others {
|
for _, other := range others {
|
||||||
if n.Eq(first, other) {
|
if n.Eq(first, other) {
|
||||||
return false
|
return false
|
||||||
|
@ -155,6 +153,7 @@ func (n *Namespace) Ne(first interface{}, others ...interface{}) bool {
|
||||||
|
|
||||||
// Ge returns the boolean truth of arg1 >= arg2 && arg1 >= arg3 && arg1 >= arg4.
|
// Ge returns the boolean truth of arg1 >= arg2 && arg1 >= arg3 && arg1 >= arg4.
|
||||||
func (n *Namespace) Ge(first interface{}, others ...interface{}) bool {
|
func (n *Namespace) Ge(first interface{}, others ...interface{}) bool {
|
||||||
|
n.checkComparisonArgCount(1, others...)
|
||||||
for _, other := range others {
|
for _, other := range others {
|
||||||
left, right := n.compareGet(first, other)
|
left, right := n.compareGet(first, other)
|
||||||
if !(left >= right) {
|
if !(left >= right) {
|
||||||
|
@ -166,6 +165,7 @@ func (n *Namespace) Ge(first interface{}, others ...interface{}) bool {
|
||||||
|
|
||||||
// Gt returns the boolean truth of arg1 > arg2 && arg1 > arg3 && arg1 > arg4.
|
// Gt returns the boolean truth of arg1 > arg2 && arg1 > arg3 && arg1 > arg4.
|
||||||
func (n *Namespace) Gt(first interface{}, others ...interface{}) bool {
|
func (n *Namespace) Gt(first interface{}, others ...interface{}) bool {
|
||||||
|
n.checkComparisonArgCount(1, others...)
|
||||||
for _, other := range others {
|
for _, other := range others {
|
||||||
left, right := n.compareGet(first, other)
|
left, right := n.compareGet(first, other)
|
||||||
if !(left > right) {
|
if !(left > right) {
|
||||||
|
@ -177,6 +177,7 @@ func (n *Namespace) Gt(first interface{}, others ...interface{}) bool {
|
||||||
|
|
||||||
// Le returns the boolean truth of arg1 <= arg2 && arg1 <= arg3 && arg1 <= arg4.
|
// Le returns the boolean truth of arg1 <= arg2 && arg1 <= arg3 && arg1 <= arg4.
|
||||||
func (n *Namespace) Le(first interface{}, others ...interface{}) bool {
|
func (n *Namespace) Le(first interface{}, others ...interface{}) bool {
|
||||||
|
n.checkComparisonArgCount(1, others...)
|
||||||
for _, other := range others {
|
for _, other := range others {
|
||||||
left, right := n.compareGet(first, other)
|
left, right := n.compareGet(first, other)
|
||||||
if !(left <= right) {
|
if !(left <= right) {
|
||||||
|
@ -188,6 +189,7 @@ func (n *Namespace) Le(first interface{}, others ...interface{}) bool {
|
||||||
|
|
||||||
// Lt returns the boolean truth of arg1 < arg2 && arg1 < arg3 && arg1 < arg4.
|
// Lt returns the boolean truth of arg1 < arg2 && arg1 < arg3 && arg1 < arg4.
|
||||||
func (n *Namespace) Lt(first interface{}, others ...interface{}) bool {
|
func (n *Namespace) Lt(first interface{}, others ...interface{}) bool {
|
||||||
|
n.checkComparisonArgCount(1, others...)
|
||||||
for _, other := range others {
|
for _, other := range others {
|
||||||
left, right := n.compareGet(first, other)
|
left, right := n.compareGet(first, other)
|
||||||
if !(left < right) {
|
if !(left < right) {
|
||||||
|
@ -197,6 +199,13 @@ func (n *Namespace) Lt(first interface{}, others ...interface{}) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Namespace) checkComparisonArgCount(min int, others ...interface{}) bool {
|
||||||
|
if len(others) < min {
|
||||||
|
panic("missing arguments for comparison")
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// Conditional can be used as a ternary operator.
|
// Conditional can be used as a ternary operator.
|
||||||
// It returns a if condition, else b.
|
// It returns a if condition, else b.
|
||||||
func (n *Namespace) Conditional(condition bool, a, b interface{}) interface{} {
|
func (n *Namespace) Conditional(condition bool, a, b interface{}) interface{} {
|
||||||
|
|
|
@ -440,3 +440,20 @@ func TestConditional(t *testing.T) {
|
||||||
c.Assert(n.Conditional(true, a, b), qt.Equals, a)
|
c.Assert(n.Conditional(true, a, b), qt.Equals, a)
|
||||||
c.Assert(n.Conditional(false, a, b), qt.Equals, b)
|
c.Assert(n.Conditional(false, a, b), qt.Equals, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 9462
|
||||||
|
func TestComparisonArgCount(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
c := qt.New(t)
|
||||||
|
|
||||||
|
ns := New(false)
|
||||||
|
|
||||||
|
panicMsg := "missing arguments for comparison"
|
||||||
|
|
||||||
|
c.Assert(func() { ns.Eq(1) }, qt.PanicMatches, panicMsg)
|
||||||
|
c.Assert(func() { ns.Ge(1) }, qt.PanicMatches, panicMsg)
|
||||||
|
c.Assert(func() { ns.Gt(1) }, qt.PanicMatches, panicMsg)
|
||||||
|
c.Assert(func() { ns.Le(1) }, qt.PanicMatches, panicMsg)
|
||||||
|
c.Assert(func() { ns.Lt(1) }, qt.PanicMatches, panicMsg)
|
||||||
|
c.Assert(func() { ns.Ne(1) }, qt.PanicMatches, panicMsg)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue