placeholders

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2022 License: MIT Imports: 5 Imported by: 0

README

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ignore added in v0.0.5

func Ignore() cmp.Option

Ignore returns a cmp.Ignore option that ignores all placeholders.

func IsPlaceholder

func IsPlaceholder(obj any) bool

IsPlaceholder returns true iff obj is a reference to an object allocated with placeholders.Make.

func Make

func Make[TPtr ~*T, T any](t TestingCleanup) TPtr

Make allocates a new zero-valued object of type T and returns an object of type TPtr that references the allocated object. TPtr is either *T or has *T as its underlying type (i.e, defined as "type Foo *Bar", where TPtr = Foo and T = Bar). The returned reference or any other reference to the allocated object is considered a placeholder reference: when cmp.Equal or cmp.Diff is invoked with the placeholders.Ignore option, a placeholder reference is considered to be equal to any other object, regardless of types or values.

Only pointers to the allocated object are considered placeholders and not the object itself (see examples below).

The second type parameter (T) can always be inferred from the first one (TPtr) and does not need to be explicitly specified.

The only parameter (t) can be of type *testing.T, *testing.B, or any other type with a function Cleanup with a similar semantic. It ensures that the resources allocated to keep track of the placeholders are eventually reclaimed (e.g., when t is of type *testing.T, they are reclaimed upon the completion of the test).

Example:

helloString := "hello"

// true
cmp.Equal(placeholders.Make[*string](t), &helloString, placeholders.Ignore())

placeholder := placeholders.Make[*string](t)
anotherRef := &(*placeholder)

// true, any reference to the allocated object is a placeholder
cmp.Equal(anotherRef, &helloString, placeholders.Ignore())

// false, the allocated object itself is not a placeholder
cmp.Equal(*placeholder, "hello", placeholders.Ignore())

type Foo struct {SPtr *string; S string}

// true, it works with struct fields and embedded types as well!
cmp.Equal(Foo{placeholders.Make[*string](t), "world"}, Foo{&helloString, "world"}, placeholders.Ignore())

// false, non-placeholder fields differ
cmp.Equal(Foo{placeholders.Make[*string](t), "earthlings"}, Foo{&helloString, "world"}, placeholders.Ignore())

Types

type TestingCleanup added in v0.0.4

type TestingCleanup interface {
	// Cleanup is used to reclaim the resources allocated to keep track of the
	// placeholders.
	Cleanup(f func())
}

TestingCleanup is an interface implemented by *testing.T and *testing.B.

Directories

Path Synopsis
examples
gomock/foo/mock
Package mock_foo is a generated GoMock package.
Package mock_foo is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL