-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Cmp
, CmpAllowUnexported
and CmpOpt
methods
- Loading branch information
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters