Skip to content

Placeholders for struct fields for efficient testing with go-cmp.

License

Notifications You must be signed in to change notification settings

xosmig/placeholders

Repository files navigation

Go Reference Go Report Card Test codecov

placeholders

Placeholders for struct fields for efficient testing with go-cmp.
The package is non-intrusive, i.e., the original structs do not need to be modified.

For more detailed documentation, see: placeholders.go
For more examples, see: placeholders_test.go

Example of integration with go-mock:

file examples/gomock/gomock_example_test.go

func TestGomockExample(t *testing.T) {
	ctrl := gomock.NewController(t)

	mock := mock_foo.NewMockFooProcessor(ctrl)

	// Matches a Foo object with any first argument and second argument equal to foo.NewBaz("world").
	mock.EXPECT().Process(matchers.DiffEq(foo.NewFoo(placeholders.Make[*foo.Bar](t), foo.NewBaz("world")))).
		Return("greetings!")

	// Matches a Foo object the first argument equal to foo.NewBar("goodbye") and any second argument.
	mock.EXPECT().Process(matchers.DiffEq(foo.NewFoo(foo.NewBar("goodbye"), placeholders.Make[foo.BazPtrWrapper](t)))).
		Return("farewell!")

	assert.Equal(t, mock.Process(foo.NewFoo(foo.NewBar("hello"), foo.NewBaz("world"))), "greetings!")

	assert.Equal(t, mock.Process(foo.NewFoo(foo.NewBar("goodbye"), foo.NewBaz("world"))), "farewell!")
}

file examples/gomock/foo/foo.go

type Foo struct {
	Bar *Bar
	Baz BazPtrWrapper
}

func NewFoo(bar *Bar, baz BazPtrWrapper) *Foo {
	return &Foo{bar, baz}
}

type Bar struct {
	S string
}

func NewBar(s string) *Bar {
	return &Bar{s}
}

type Baz struct {
	S string
}

func NewBaz(s string) *Baz {
	return &Baz{s}
}

type BazPtrWrapper *Baz

//go:generate mockgen -destination ./mock/foo_processor.mock.go . FooProcessor
type FooProcessor interface {
	Process(foo *Foo) string
}

About

Placeholders for struct fields for efficient testing with go-cmp.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages