117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
package moq
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestMoq(t *testing.T) {
|
|
m, err := New("testdata/example", "")
|
|
if err != nil {
|
|
t.Errorf("moq.New: %s", err)
|
|
}
|
|
var buf bytes.Buffer
|
|
err = m.Mock(&buf, "PersonStore")
|
|
if err != nil {
|
|
t.Errorf("m.Mock: %s", err)
|
|
}
|
|
s := buf.String()
|
|
// assertions of things that should be mentioned
|
|
var strs = []string{
|
|
"package example",
|
|
"type PersonStoreMock struct",
|
|
"CreateFunc func(ctx context.Context, person *Person, confirm bool) error",
|
|
"GetFunc func(ctx context.Context, id string) (*Person, error)",
|
|
"func (mock *PersonStoreMock) Create(ctx context.Context, person *Person, confirm bool) error",
|
|
"func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)",
|
|
"panic(\"moq: PersonStoreMock.CreateFunc is nil but was just called\")",
|
|
"panic(\"moq: PersonStoreMock.GetFunc is nil but was just called\")",
|
|
}
|
|
for _, str := range strs {
|
|
if !strings.Contains(s, str) {
|
|
t.Errorf("expected but missing: \"%s\"", str)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMoqExplicitPackage(t *testing.T) {
|
|
m, err := New("testdata/example", "different")
|
|
if err != nil {
|
|
t.Errorf("moq.New: %s", err)
|
|
}
|
|
var buf bytes.Buffer
|
|
err = m.Mock(&buf, "PersonStore")
|
|
if err != nil {
|
|
t.Errorf("m.Mock: %s", err)
|
|
}
|
|
s := buf.String()
|
|
// assertions of things that should be mentioned
|
|
var strs = []string{
|
|
"package different",
|
|
"type PersonStoreMock struct",
|
|
"CreateFunc func(ctx context.Context, person *example.Person, confirm bool) error",
|
|
"GetFunc func(ctx context.Context, id string) (*example.Person, error)",
|
|
"func (mock *PersonStoreMock) Create(ctx context.Context, person *example.Person, confirm bool) error",
|
|
"func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*example.Person, error)",
|
|
}
|
|
for _, str := range strs {
|
|
if !strings.Contains(s, str) {
|
|
t.Errorf("expected but missing: \"%s\"", str)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestVeradicArguments tests to ensure variadic work as
|
|
// expected.
|
|
// see https://github.com/matryer/moq/issues/5
|
|
func TestVariadicArguments(t *testing.T) {
|
|
m, err := New("testdata/variadic", "")
|
|
if err != nil {
|
|
t.Errorf("moq.New: %s", err)
|
|
}
|
|
var buf bytes.Buffer
|
|
err = m.Mock(&buf, "Greeter")
|
|
if err != nil {
|
|
t.Errorf("m.Mock: %s", err)
|
|
}
|
|
s := buf.String()
|
|
// assertions of things that should be mentioned
|
|
var strs = []string{
|
|
"package variadic",
|
|
"type GreeterMock struct",
|
|
"GreetFunc func(ctx context.Context, names ...string) string",
|
|
"return mock.GreetFunc(ctx, names...)",
|
|
}
|
|
for _, str := range strs {
|
|
if !strings.Contains(s, str) {
|
|
t.Errorf("expected but missing: \"%s\"", str)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNothingToReturn(t *testing.T) {
|
|
m, err := New("testdata/example", "")
|
|
if err != nil {
|
|
t.Errorf("moq.New: %s", err)
|
|
}
|
|
var buf bytes.Buffer
|
|
err = m.Mock(&buf, "PersonStore")
|
|
if err != nil {
|
|
t.Errorf("m.Mock: %s", err)
|
|
}
|
|
s := buf.String()
|
|
if strings.Contains(s, `return mock.ClearCacheFunc(id)`) {
|
|
t.Errorf("should not have return for items that have no return arguments")
|
|
}
|
|
// assertions of things that should be mentioned
|
|
var strs = []string{
|
|
"mock.ClearCacheFunc(id)",
|
|
}
|
|
for _, str := range strs {
|
|
if !strings.Contains(s, str) {
|
|
t.Errorf("expected but missing: \"%s\"", str)
|
|
}
|
|
}
|
|
}
|