Skip to content

Commit

Permalink
Merge pull request #1 from budougumi0617/fix-string
Browse files Browse the repository at this point in the history
❤️ return empty if not get diff
  • Loading branch information
budougumi0617 authored Apr 22, 2021
2 parents b185424 + 515235a commit f0445f5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
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

0 comments on commit f0445f5

Please sign in to comment.