1
0
mirror of https://github.com/danog/liquid.git synced 2025-01-22 22:51:13 +01:00

Fix generic equality with nil

This commit is contained in:
Oliver Steele 2017-06-27 22:54:27 -04:00
parent f8b55032f4
commit 229059c169
2 changed files with 30 additions and 3 deletions

View File

@ -7,6 +7,9 @@ import (
// Equal returns a bool indicating whether a == b after conversion.
func Equal(a, b interface{}) bool {
if a == nil || b == nil {
return a == b
}
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())
}
ak, bk := a.Kind(), b.Kind()
// _ = ak.Convert
switch a.Kind() {
case reflect.Bool:
if b.Kind() == reflect.Bool {

View File

@ -7,7 +7,23 @@ import (
"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{}
expected bool
}{
@ -25,8 +41,17 @@ var comparisonTests = []struct {
{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) {
for i, test := range comparisonTests {
for i, test := range lessTests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
value := Less(test.a, test.b)
require.Equalf(t, test.expected, value, "%v < %v", test.a, test.b)