hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit 5e660947757023434dd7a1ec8b8239c0577fd501
parent f4e1cb8d05720ac58f7d89b95b8fe275ac632091
Author: Vazrupe (HyeonGyu Lee) <vazrupe@naver.com>
Date:   Tue,  3 Sep 2019 21:20:20 +0900

tpl: Remove eq argument limitation

Fixes #6237

Diffstat:
Mtpl/compare/compare.go | 35+++++++++++++++++++++++------------
Mtpl/compare/compare_test.go | 28+++++++++++++++++++++++++++-
2 files changed, 50 insertions(+), 13 deletions(-)
diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go
@@ -90,17 +90,13 @@ func (*Namespace) Default(dflt interface{}, given ...interface{}) (interface{}, 
 	return dflt, nil
 }
 
-// Eq returns the boolean truth of arg1 == arg2.
-func (ns *Namespace) Eq(x, y interface{}) bool {
-	if ns.caseInsensitive {
+// Eq returns the boolean truth of arg1 == arg2 || arg1 == arg3 || arg1 == arg4.
+func (n *Namespace) Eq(first interface{}, others ...interface{}) bool {
+	if n.caseInsensitive {
 		panic("caseInsensitive not implemented for Eq")
 	}
-	if e, ok := x.(compare.Eqer); ok {
-		return e.Eq(y)
-	}
-
-	if e, ok := y.(compare.Eqer); ok {
-		return e.Eq(x)
+	if len(others) == 0 {
+		panic("missing arguments for comparison")
 	}
 
 	normalize := func(v interface{}) interface{} {
@@ -119,9 +115,24 @@ func (ns *Namespace) Eq(x, y interface{}) bool {
 			return v
 		}
 	}
-	x = normalize(x)
-	y = normalize(y)
-	return reflect.DeepEqual(x, y)
+
+	normFirst := normalize(first)
+	for _, other := range others {
+		if e, ok := first.(compare.Eqer); ok {
+			return e.Eq(other)
+		}
+
+		if e, ok := other.(compare.Eqer); ok {
+			return e.Eq(first)
+		}
+
+		other = normalize(other)
+		if reflect.DeepEqual(normFirst, other) {
+			return true
+		}
+	}
+
+	return false
 }
 
 // Ne returns the boolean truth of arg1 != arg2.
diff --git a/tpl/compare/compare_test.go b/tpl/compare/compare_test.go
@@ -145,6 +145,10 @@ func TestCompare(t *testing.T) {
 
 	n := New(false)
 
+	twoEq := func(a, b interface{}) bool {
+		return n.Eq(a, b)
+	}
+
 	for _, test := range []struct {
 		tstCompareType
 		funcUnderTest func(a, b interface{}) bool
@@ -153,7 +157,7 @@ func TestCompare(t *testing.T) {
 		{tstLt, n.Lt},
 		{tstGe, n.Ge},
 		{tstLe, n.Le},
-		{tstEq, n.Eq},
+		{tstEq, twoEq},
 		{tstNe, n.Ne},
 	} {
 		doTestCompare(t, test.tstCompareType, test.funcUnderTest)
@@ -237,6 +241,28 @@ func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b inte
 	}
 }
 
+func TestEqualExtend(t *testing.T) {
+	t.Parallel()
+	c := qt.New(t)
+
+	ns := New(false)
+
+	for _, test := range []struct {
+		first  interface{}
+		others []interface{}
+		expect bool
+	}{
+		{1, []interface{}{1, 2}, true},
+		{1, []interface{}{2, 1}, true},
+		{1, []interface{}{2, 3}, false},
+	} {
+
+		result := ns.Eq(test.first, test.others...)
+
+		c.Assert(result, qt.Equals, test.expect)
+	}
+}
+
 func TestCase(t *testing.T) {
 	c := qt.New(t)
 	n := New(true)