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 Cmp, CmpAllowUnexported and CmpOpt methods #135

Merged
merged 1 commit into from
May 2, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func getLove() (bool, error) {
### [For 2 objects](https://github.com/bayashi/actually/wiki/All-assertion-methods#assertion-for-2-objects)

* Same, SameConvertibleNumber, SamePointer, SameType
* Cmp, CmpAllowUnexported, (CmpOpt)
* NotSame, NotSameConvertibleNumber, NotSamePointer, NotSameType

### [For panic](https://github.com/bayashi/actually/wiki/All-assertion-methods#assertion-for-panic)
Expand Down
3 changes: 3 additions & 0 deletions actually.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package actually
import (
"fmt"
"testing"

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

// testingA is a context of the test
Expand All @@ -18,6 +20,7 @@ type testingA struct {
name string
failed bool
debugInfo []map[string][]any
cmpOpts []cmp.Option
}

// Got sets the value you actually got. Got() creates *testingA and returns it.
Expand Down
60 changes: 60 additions & 0 deletions assert_cmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package actually

import (
"testing"

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

// Cmp method gets the differences between two objects by go-cmp.Diff.
// https://pkg.go.dev/github.com/google/go-cmp/cmp#Diff
/*
actually.Got(obj1).Expect(obj2).Cmp(t)
*/
// If you need to set cmp.Option, then you shoud use `CmpOpt(cmp.Option)` method before calling Cmp.
// Cmp method is just a wrapper of go-cmp.Diff. So, it's same that unexported fields are not compared by default;
// they result in panics unless suppressed by using an Ignore option. It may panic if it cannot compare the values.
func (a *testingA) Cmp(t *testing.T, testNames ...string) *testingA {
invalidCallForSame(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()

if diff := cmp.Diff(a.expect, a.got, a.cmpOpts...); diff != "" {
return a.fail(reportForSame(a).Message("Diff details", diff), reason_NotSame)
}

return a
}

// CmpAllowUnexported method gets the differences between two objects by go-cmp.Diff with cmp.AllowUnexported option.
// It accepts unexported methods to compare instead panic. If you would like to ignore unexported methods,
// then you can use cmpopts.IgnoreUnexported or some cmpopt's options to ignore.
func (a *testingA) CmpAllowUnexported(t *testing.T, testNames ...string) *testingA {
invalidCallForSame(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()

a.CmpOpt(cmp.AllowUnexported(a.got))

if diff := cmp.Diff(a.expect, a.got, a.cmpOpts...); diff != "" {
return a.fail(reportForSame(a).Message("Diff details", diff), reason_NotSame)
}

return a
}

// CmpOpt method sets/adds options for Cmp* methods.
// There is no method to reset cmpOpts. Just set all opts at one time, or add opts.
/*
actually.Got(obj1).Expect(obj2).CmpOpt(cmpopts.IgnoreFields(Foo{}, "Field")).Cmp(t)
*/
// ref:
// * https://pkg.go.dev/github.com/google/go-cmp/cmp#Option
// * https://pkg.go.dev/github.com/google/go-cmp/cmp/cmpopts
func (a *testingA) CmpOpt(cmpOpts ...cmp.Option) *testingA {
a.cmpOpts = append(a.cmpOpts, cmpOpts...)

return a
}
41 changes: 41 additions & 0 deletions assert_cmp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package actually

import (
"testing"
)

func TestCmp(t *testing.T) {
Got(123).Expect(123).Cmp(t)
}

func TestCmp_Fail(t *testing.T) {
stubConfirm(t, func() {
Got(123).Expect(456).Cmp(t)
}, "Not same value")
}

func TestCmpAllowUnexported(t *testing.T) {
x := struct {
id int
Name string
}{
id: 1,
Name: "aiko",
}
Got(x).Expect(x).CmpAllowUnexported(t)
}

func TestCmpAllowUnexported_Fail(t *testing.T) {
x := struct {
id int
Name string
}{
id: 1,
Name: "aiko",
}
y := x
y.id = 2
stubConfirm(t, func() {
Got(x).Expect(y).CmpAllowUnexported(t)
}, "Not same value")
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ require github.com/pmezard/go-difflib v1.0.0 // indirect

require github.com/bayashi/witness v0.0.19

require github.com/davecgh/go-spew v1.1.1 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ github.com/bayashi/witness v0.0.19 h1:UC8jpJMqAHMgseA6p4SyY8PZZWl46vaSI8xW7En+5M
github.com/bayashi/witness v0.0.19/go.mod h1:xxXU08y35qzkp0kDRGVYuvHB5JVxFKe6vF16puu7gEo=
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.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=