Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsPlaceholder ¶
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. |