tpl: Remove eq argument limitation

Fixes #6237
This commit is contained in:
Vazrupe (HyeonGyu Lee) 2019-09-03 21:20:20 +09:00 committed by Bjørn Erik Pedersen
parent f4e1cb8d05
commit 5e66094775
2 changed files with 50 additions and 13 deletions

View file

@ -90,17 +90,13 @@ func (*Namespace) Default(dflt interface{}, given ...interface{}) (interface{},
return dflt, nil return dflt, nil
} }
// Eq returns the boolean truth of arg1 == arg2. // Eq returns the boolean truth of arg1 == arg2 || arg1 == arg3 || arg1 == arg4.
func (ns *Namespace) Eq(x, y interface{}) bool { func (n *Namespace) Eq(first interface{}, others ...interface{}) bool {
if ns.caseInsensitive { if n.caseInsensitive {
panic("caseInsensitive not implemented for Eq") panic("caseInsensitive not implemented for Eq")
} }
if e, ok := x.(compare.Eqer); ok { if len(others) == 0 {
return e.Eq(y) panic("missing arguments for comparison")
}
if e, ok := y.(compare.Eqer); ok {
return e.Eq(x)
} }
normalize := func(v interface{}) interface{} { normalize := func(v interface{}) interface{} {
@ -119,9 +115,24 @@ func (ns *Namespace) Eq(x, y interface{}) bool {
return v return v
} }
} }
x = normalize(x)
y = normalize(y) normFirst := normalize(first)
return reflect.DeepEqual(x, y) 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. // Ne returns the boolean truth of arg1 != arg2.

View file

@ -145,6 +145,10 @@ func TestCompare(t *testing.T) {
n := New(false) n := New(false)
twoEq := func(a, b interface{}) bool {
return n.Eq(a, b)
}
for _, test := range []struct { for _, test := range []struct {
tstCompareType tstCompareType
funcUnderTest func(a, b interface{}) bool funcUnderTest func(a, b interface{}) bool
@ -153,7 +157,7 @@ func TestCompare(t *testing.T) {
{tstLt, n.Lt}, {tstLt, n.Lt},
{tstGe, n.Ge}, {tstGe, n.Ge},
{tstLe, n.Le}, {tstLe, n.Le},
{tstEq, n.Eq}, {tstEq, twoEq},
{tstNe, n.Ne}, {tstNe, n.Ne},
} { } {
doTestCompare(t, test.tstCompareType, test.funcUnderTest) 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) { func TestCase(t *testing.T) {
c := qt.New(t) c := qt.New(t)
n := New(true) n := New(true)