go-test / deep

Golang deep variable equality test that returns human-readable differences
MIT License
743 stars 54 forks source link

Functions are handled differently from reflect.DeepEqual #46

Closed countcb closed 1 year ago

countcb commented 3 years ago

deep.Equal does behave differently from reflect.DeepEqual when comparing functions.

The documentation of reflect.DeepEqual states for functions:

Func values are deeply equal if both are nil; otherwise they are not deeply equal.

Example Code:

func testFunc(i int) int {
    return i
}
func TestEqualForFunctions() {
    type TestStruct struct {
        Function func(int) int
    }
    t1 := TestStruct{
        Function: testFunc,
    }
    t2 := TestStruct{
        Function: testFunc,
    }
    fmt.Println("reflect", reflect.DeepEqual(t1, t2))
    fmt.Println("deep", deep.Equal(t1, t2))

    t1.Function = nil
    fmt.Println("reflect", reflect.DeepEqual(t1, t2))
    fmt.Println("deep", deep.Equal(t1, t2))

    t1.Function = nil
    t2.Function = nil
    fmt.Println("reflect", reflect.DeepEqual(t1, t2))
    fmt.Println("deep", deep.Equal(t1, t2))
}

Output:

reflect false 
deep []  // should state that both functions are != nil
reflect false
deep [] // should state that function 1 is not nil but function 2 is nil
reflect true
deep []
countcb commented 3 years ago

If this gets added/changed then maybe it would be a good idea to add a field deep.CompareFunctions

similar to deep.CompareUnexportedFields

daniel-nichter commented 1 year ago

Fixed in https://github.com/go-test/deep/pull/55. Will be released soon.

daniel-nichter commented 1 year ago

Released: https://github.com/go-test/deep/releases/tag/v1.0.9