mirror of
https://github.com/danog/liquid.git
synced 2025-01-23 05:01:13 +01:00
Fix generic equality with nil
This commit is contained in:
parent
f8b55032f4
commit
229059c169
@ -7,6 +7,9 @@ import (
|
|||||||
|
|
||||||
// Equal returns a bool indicating whether a == b after conversion.
|
// Equal returns a bool indicating whether a == b after conversion.
|
||||||
func Equal(a, b interface{}) bool {
|
func Equal(a, b interface{}) bool {
|
||||||
|
if a == nil || b == nil {
|
||||||
|
return a == b
|
||||||
|
}
|
||||||
return genericCompare(reflect.ValueOf(a), reflect.ValueOf(b)) == 0
|
return genericCompare(reflect.ValueOf(a), reflect.ValueOf(b)) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +74,6 @@ func genericCompare(a, b reflect.Value) int {
|
|||||||
return genericSameTypeCompare(a.Interface(), b.Interface())
|
return genericSameTypeCompare(a.Interface(), b.Interface())
|
||||||
}
|
}
|
||||||
ak, bk := a.Kind(), b.Kind()
|
ak, bk := a.Kind(), b.Kind()
|
||||||
// _ = ak.Convert
|
|
||||||
switch a.Kind() {
|
switch a.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
if b.Kind() == reflect.Bool {
|
if b.Kind() == reflect.Bool {
|
||||||
|
@ -7,7 +7,23 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
var comparisonTests = []struct {
|
var eqTests = []struct {
|
||||||
|
a, b interface{}
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{0, 1, false},
|
||||||
|
{1, 1, true},
|
||||||
|
{1.0, 1.0, true},
|
||||||
|
{"a", "b", false},
|
||||||
|
{"a", "a", true},
|
||||||
|
{nil, nil, true},
|
||||||
|
{nil, 1, false},
|
||||||
|
{1, nil, false},
|
||||||
|
{false, false, true},
|
||||||
|
{false, true, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
var lessTests = []struct {
|
||||||
a, b interface{}
|
a, b interface{}
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
@ -25,8 +41,17 @@ var comparisonTests = []struct {
|
|||||||
{false, true, true},
|
{false, true, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEqual(t *testing.T) {
|
||||||
|
for i, test := range eqTests {
|
||||||
|
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||||
|
value := Equal(test.a, test.b)
|
||||||
|
require.Equalf(t, test.expected, value, "%v < %v", test.a, test.b)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLess(t *testing.T) {
|
func TestLess(t *testing.T) {
|
||||||
for i, test := range comparisonTests {
|
for i, test := range lessTests {
|
||||||
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||||
value := Less(test.a, test.b)
|
value := Less(test.a, test.b)
|
||||||
require.Equalf(t, test.expected, value, "%v < %v", test.a, test.b)
|
require.Equalf(t, test.expected, value, "%v < %v", test.a, test.b)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user