Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EqualsWithNaN to testutil #3

Merged
merged 4 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![core module docs](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/efficientgo/core)

Go module with set of core packages **every** Go project needs. Minimal API, battle-tested, strictly versioned and with only one transient dependency--[davecgh/go-spew](https://github.com/davecgh/go-spew).
Go module with set of core packages **every** Go project needs. Minimal API, battle-tested, strictly versioned and with only two transient dependencies-- [davecgh/go-spew](https://github.com/davecgh/go-spew) and [google/go-cmp](https://github.com/google/go-cmp).

Maintained by experienced Go developers, including author of the [Efficient Go book](https://www.oreilly.com/library/view/efficient-go/9781098105709/).

Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/efficientgo/core

go 1.17

require github.com/davecgh/go-spew v1.1.1
require (
github.com/davecgh/go-spew v1.1.1
github.com/google/go-cmp v0.5.9
saswatamcode marked this conversation as resolved.
Show resolved Hide resolved
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
28 changes: 28 additions & 0 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/efficientgo/core/errors"
"github.com/efficientgo/core/testutil/internal"
"github.com/google/go-cmp/cmp"
)

// Assert fails the test if the condition is false.
Expand Down Expand Up @@ -76,6 +77,33 @@ func Equals(tb testing.TB, exp, act interface{}, v ...interface{}) {
tb.Fatal(sprintfWithLimit("\033[31m%s:%d:"+msg+"\n\n\texp: %#v\n\n\tgot: %#v%s\033[39m\n\n", filepath.Base(file), line, exp, act, diff(exp, act)))
}

type goCmp struct {
opts cmp.Options
}

// WithGoCmp allows specifying options and using https://github.com/google/go-cmp
// for equality comparisons. The compatibility guarantee of this function's arguments
// are the same as go-cmp i.e none due to v0.x.
func WithGoCmp(opts ...cmp.Option) goCmp {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also mention in the comment that compatibility guarantee for this function arguments is same as go-cmp, so... zero as it is at version 0.x. It's fine, but let's just mention it in comment

return goCmp{opts: opts}
}

// Equals uses go-cmp for comparing equality between two structs, and can be used with
// various options defined in go-cmp/cmp and go-cmp/cmp/cmpopts.
func (o goCmp) Equals(tb testing.TB, exp, act interface{}, v ...interface{}) {
tb.Helper()
if cmp.Equal(exp, act, o.opts) {
return
}
_, file, line, _ := runtime.Caller(1)

var msg string
if len(v) > 0 {
msg = fmt.Sprintf(v[0].(string), v[1:]...)
}
tb.Fatal(sprintfWithLimit("\033[31m%s:%d:"+msg+"\n\n\texp: %#v\n\n\tgot: %#v%s\033[39m\n\n", filepath.Base(file), line, exp, act, diff(exp, act)))
}

// FaultOrPanicToErr returns error if panic of fault was triggered during execution of function.
func FaultOrPanicToErr(f func()) (err error) {
// Set this go routine to panic on segfault to allow asserting on those.
Expand Down
58 changes: 57 additions & 1 deletion testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

package testutil

import "testing"
import (
"math"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestContains(t *testing.T) {
tests := map[string]struct {
Expand Down Expand Up @@ -68,3 +74,53 @@ func TestContains(t *testing.T) {
})
}
}

type child struct {
Val float64
}

type parent struct {
C child
}

type unexp struct {
val float64
}

func TestEqualsWithNaN(t *testing.T) {
for _, tc := range []struct {
name string
a interface{}
b interface{}
opts cmp.Options
}{
{
a: math.NaN(),
b: math.NaN(),
name: "Simple NaN value comparison",
opts: cmp.Options{cmpopts.EquateNaNs()},
},
{
a: child{Val: math.NaN()},
b: child{Val: math.NaN()},
name: "NaN value as struct member comparison",
opts: cmp.Options{cmpopts.EquateNaNs()},
},
{
a: parent{C: child{Val: math.NaN()}},
b: parent{C: child{Val: math.NaN()}},
name: "NaN value in nested struct comparison",
opts: cmp.Options{cmpopts.EquateNaNs()},
},
{
a: unexp{val: math.NaN()},
b: unexp{val: math.NaN()},
name: "NaN value as unexported struct member comparison",
opts: cmp.Options{cmp.AllowUnexported(unexp{}), cmpopts.EquateNaNs()},
},
} {
t.Run(tc.name, func(t *testing.T) {
WithGoCmp(tc.opts).Equals(t, tc.a, tc.b)
})
}
}