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

❤️ return empty if not get diff #1

Merged
merged 2 commits into from
Apr 22, 2021
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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions diffmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 1 addition & 3 deletions diffmatcher_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmpmock

import (
"fmt"
"testing"
"time"

Expand All @@ -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
Expand Down Expand Up @@ -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)
}
})
Expand Down