Skip to content

Commit

Permalink
Add CmpIgnoreUnexported method
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Jan 17, 2025
1 parent 8655240 commit 4094282
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func getLove() (bool, error) {

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

### [For panic](https://github.com/bayashi/actually/wiki/All-assertion-methods#assertion-for-panic)

Expand Down
30 changes: 29 additions & 1 deletion assert_cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/protobuf/testing/protocmp"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func (a *testingA) CmpProto(t *testing.T, testNames ...string) *testingA {

// 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.
// then you can use CmpIgnoreUnexported method.
func (a *testingA) CmpAllowUnexported(t *testing.T, testNames ...string) *testingA {
invalidCallForSame(a)
a.name = a.naming(testNames...)
Expand All @@ -74,6 +75,33 @@ func (a *testingA) CmpAllowUnexported(t *testing.T, testNames ...string) *testin
return a
}

// CmpIgnoreUnexported method gets the differences between two objects by go-cmp.Diff with cmpopts.IgnoreUnexported option.
// It ignores unexported methods to compare structs instead panic. If you would like to compare also unexported methods,
// then you can use CmpAllowUnexported method.
func (a *testingA) CmpIgnoreUnexported(t *testing.T, testNames ...string) *testingA {
invalidCallForSame(a)
a.name = a.naming(testNames...)
a.t = t
a.t.Helper()

if !isStructType(a.got) {
w := reportForSame(a).Message(notice_Label, notice_Cmp_ShouldStruct)
return a.fail(w, reason_GotShouldStruct)
}
if !isStructType(a.expect) {
w := reportForSame(a).Message(notice_Label, notice_Cmp_ShouldStruct)
return a.fail(w, reason_ExpectShouldStruct)
}

a.CmpOpt(cmpopts.IgnoreUnexported(a.got, a.expect))

if diff := cmp.Diff(a.expect, a.got, a.cmpOpts.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.
/*
Expand Down
38 changes: 38 additions & 0 deletions assert_cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,41 @@ func TestCmpAllowUnexported_Fail(t *testing.T) {
Got(x).Expect(nil).CmpAllowUnexported(t)
}, "`Expect` value should be type of struct")
}

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

y := x
y.id = 2
Got(x).Expect(y).CmpIgnoreUnexported(t)
}

func TestCmpIgnoreUnexported_Fail(t *testing.T) {
x := struct {
id int
Name string
}{
id: 1,
Name: "aiko",
}
y := x
y.Name = "eiko"
stubConfirm(t, func() {
Got(x).Expect(y).CmpIgnoreUnexported(t)
}, "Not same value")

stubConfirm(t, func() {
Got(nil).Expect(y).CmpIgnoreUnexported(t)
}, "`Got` value should be type of struct")

stubConfirm(t, func() {
Got(x).Expect(nil).CmpIgnoreUnexported(t)
}, "`Expect` value should be type of struct")
}

0 comments on commit 4094282

Please sign in to comment.