-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3303ef
commit 7ae064a
Showing
7 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,28 @@ | ||
package _example | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type User struct { | ||
Name, Address string | ||
CreateAt time.Time | ||
} | ||
|
||
type UserRepo interface { | ||
Save(context.Context, *User) error | ||
} | ||
|
||
type UserUsecase struct { | ||
Repo UserRepo | ||
} | ||
|
||
func (uu *UserUsecase) Register(ctx context.Context, name, address string) error { | ||
u := &User{ | ||
Name: name, | ||
Address: address, | ||
CreateAt: time.Now(), | ||
} | ||
return uu.Repo.Save(ctx, u) | ||
} |
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,34 @@ | ||
package _example_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/budougumi0617/cmpmock" | ||
"github.com/budougumi0617/cmpmock/_example" | ||
"github.com/budougumi0617/cmpmock/_example/mock_example" | ||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
func TestUserUsecase_Save(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
t.Cleanup(ctrl.Finish) | ||
ctx := context.Background() | ||
name := "John Due" | ||
address := "Tokyo" | ||
wantUser := &_example.User{ | ||
Name: name, | ||
Address: address, | ||
CreateAt: time.Now(), | ||
} | ||
|
||
mrepo := mock_example.NewMockUserRepo(ctrl) | ||
mrepo.EXPECT().Save(ctx, cmpmock.DiffEq(wantUser)).Return(nil) | ||
|
||
sut := _example.UserUsecase{Repo: mrepo} | ||
|
||
if err := sut.Register(ctx, name, address); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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,35 @@ | ||
package cmpmock | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
) | ||
|
||
func DiffEq(v interface{}, opts ...cmp.Option) gomock.Matcher { | ||
var lopts cmp.Options | ||
if len(opts) == 0 { | ||
lopts = append(lopts, cmpopts.EquateApproxTime(1*time.Second)) | ||
} else { | ||
lopts = append(lopts, opts...) | ||
} | ||
return &diffMatcher{want: v, opts: lopts} | ||
} | ||
|
||
type diffMatcher struct { | ||
want interface{} | ||
diff string | ||
opts cmp.Options | ||
} | ||
|
||
func (d *diffMatcher) Matches(x interface{}) bool { | ||
d.diff = cmp.Diff(x, d.want, d.opts...) | ||
return len(d.diff) == 0 | ||
} | ||
|
||
func (d *diffMatcher) String() string { | ||
return fmt.Sprintf("diff(-got +want) is %s", d.diff) | ||
} |
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,50 @@ | ||
package cmpmock | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
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 | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
x Foo | ||
wantMatch bool | ||
wantDiff string | ||
}{ | ||
{ | ||
name: "diff less than a second with default option", | ||
args: args{ | ||
want: Foo{CreateAt: t1}, | ||
opts: nil, | ||
}, | ||
x: Foo{CreateAt: t2}, | ||
wantMatch: true, | ||
wantDiff: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
sut := DiffEq(tt.args.want, tt.args.opts...) | ||
x := Foo{CreateAt: t2} | ||
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) { | ||
t.Errorf("String() = %q, want %q", got, tt.wantDiff) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module github.com/budougumi0617/cmpmock | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/golang/mock v1.5.0 | ||
github.com/google/go-cmp v0.5.5 | ||
) |
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,18 @@ | ||
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g= | ||
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8= | ||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= | ||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |