89 lines
2.4 KiB
Go
89 lines
2.4 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)",
|
|
}
|
|
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",
|
|
}
|
|
for _, str := range strs {
|
|
if !strings.Contains(s, str) {
|
|
t.Errorf("expected but missing: \"%s\"", str)
|
|
}
|
|
}
|
|
}
|