From 4e9edb2df67c8bc83d974f01947b4b1ad096f7d8 Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Fri, 23 Apr 2021 02:37:45 +0900 Subject: [PATCH 1/2] :heart: return empty if not get diff --- diffmatcher.go | 3 +++ diffmatcher_test.go | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/diffmatcher.go b/diffmatcher.go index 234501f..948040e 100644 --- a/diffmatcher.go +++ b/diffmatcher.go @@ -31,5 +31,8 @@ func (d *diffMatcher) Matches(x interface{}) bool { } func (d *diffMatcher) String() string { + if d.diff == "" { + return "" + } return fmt.Sprintf("diff(-got +want) is %s", d.diff) } diff --git a/diffmatcher_test.go b/diffmatcher_test.go index 4795f41..eae5cdb 100644 --- a/diffmatcher_test.go +++ b/diffmatcher_test.go @@ -1,7 +1,6 @@ package cmpmock import ( - "fmt" "testing" "time" @@ -12,7 +11,6 @@ func Test_DiffEq(t *testing.T) { type Foo struct{ CreateAt time.Time } t1 := time.Now() t2 := t1.Add(100 * time.Millisecond) - defaultString := "diff(-got +want) is %s" type args struct { want interface{} opts cmp.Options @@ -42,7 +40,7 @@ func Test_DiffEq(t *testing.T) { if got := sut.Matches(x); got != tt.wantMatch { t.Errorf("Matches() = %v, want %v", got, tt.wantMatch) } - if got := sut.String(); got != fmt.Sprintf(defaultString, tt.wantDiff) { + if got := sut.String(); got != tt.wantDiff { t.Errorf("String() = %q, want %q", got, tt.wantDiff) } }) From 515235a00e3ca74889695cd947551b267f7d9b28 Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Fri, 23 Apr 2021 02:55:29 +0900 Subject: [PATCH 2/2] :sparkles: update README --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index bb4006b..25f181c 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,46 @@ [![test](https://github.com/budougumi0617/cmpmock/workflows/test/badge.svg)](https://github.com/budougumi0617/cmpmock/actions?query=workflow%3Atest) [![reviewdog](https://github.com/budougumi0617/cmpmock/workflows/reviewdog/badge.svg)](https://github.com/budougumi0617/cmpmock/actions?query=workflow%3Areviewdog) +Readable & Flexible matcher for https://github.com/golang/mock + ## Description + +### Readable ouput + +Default output +``` +expected call at /Users/budougumi0617/go/src/github.com/budougumi0617/cmpmock/_example/repo_test.go:26 doesn't match the argument at index 1. +Got: &{John Due Tokyo 2021-04-23 02:46:58.145696 +0900 JST m=+0.000595005} +Want: is equal to &{John Due Tokyo 2021-04-23 02:46:48.145646 +0900 JST m=-9.999455563} +``` + +use `cmpmock.DiffEq` +``` +expected call at /Users/budougumi0617/go/src/github.com/budougumi0617/cmpmock/_example/repo_test.go:27 doesn't match the argument at index 1. +Got: &{John Due Tokyo 2021-04-23 02:46:33.290458 +0900 JST m=+0.001035665} +Want: diff(-got +want) is &_example.User{ + Name: "John Due", + Address: "Tokyo", +- CreateAt: s"2021-04-23 02:46:33.290458 +0900 JST m=+0.001035665", ++ CreateAt: s"2021-04-23 02:46:23.290383 +0900 JST m=-9.999039004", +} +``` + ## Usage +```go +wantUser := &_example.User{ + Name: name, + Address: address, + CreateAt: time.Now().Add(-10 * time.Second), +} + +mrepo := mock_example.NewMockUserRepo(ctrl) +mrepo.EXPECT().Save(ctx, cmpmock.DiffEq(wantUser)).Return(nil) +``` + ## Installation ```bash