Documentation ¶
Overview ¶
Package helpers (github.com/launchdarkly/go-test-helpers) contains miscellaneous convenience functions for use in test code.
Index ¶
- func AsPointer[V any](v V) *V
- func AssertChannelClosed[V any](t assert.TestingT, ch <-chan V, timeout time.Duration, ...) bool
- func AssertChannelNotClosed[V any](t assert.TestingT, ch <-chan V, timeout time.Duration, ...) bool
- func AssertNoMoreValues[V any](t assert.TestingT, ch <-chan V, timeout time.Duration, ...) bool
- func FilePathExists(path string) bool
- func ReadWithTimeout(r io.Reader, n int, timeout time.Duration) []byte
- func RequireValue[V any](t require.TestingT, ch <-chan V, timeout time.Duration, ...) V
- func TryReceive[V any](ch <-chan V, timeout time.Duration) (V, bool, bool)
- func WithCloser(closeableObject io.Closer, action func())
- func WithTempDir(f func(path string))
- func WithTempFile(f func(filePath string))
- func WithTempFileData(data []byte, f func(filePath string))
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertChannelClosed ¶
func AssertChannelClosed[V any]( t assert.TestingT, ch <-chan V, timeout time.Duration, customMessageAndArgs ...any, ) bool
AssertChannelClosed asserts that the channel is closed within the timeout, sending no values.
func AssertChannelNotClosed ¶
func AssertChannelNotClosed[V any]( t assert.TestingT, ch <-chan V, timeout time.Duration, customMessageAndArgs ...any, ) bool
AssertChannelNotClosed asserts that the channel is not closed within the timeout, consuming any values that may be sent during that time.
func AssertNoMoreValues ¶
func AssertNoMoreValues[V any]( t assert.TestingT, ch <-chan V, timeout time.Duration, customMessageAndArgs ...any, ) bool
AssertNoMoreValues asserts that no value is available from the channel within the timeout, but that the channel was not closed.
func FilePathExists ¶
FilePathExists is simply a shortcut for using os.Stat to check for a file's or directory's existence.
func ReadWithTimeout ¶
ReadWithTimeout reads data until it gets the desired number of bytes or times out.
This is an inefficient implementation that should only be used in tests.
func RequireValue ¶
func RequireValue[V any](t require.TestingT, ch <-chan V, timeout time.Duration, customMessageAndArgs ...any) V
RequireValue returns the next value from the channel, or forces an immediate test failure and exit if the timeout expires first.
func TryReceive ¶
TryReceive waits for a value from the channel and returns (value, true, false) if successful; (<empty>, false, false) if the timeout expired first; or (<empty>, false, true) if the channel was closed.
func WithCloser ¶
WithCloser executes a function and ensures that the given object's Close() method is always called afterward.
This is simply a way to get more specific control over an object's lifetime than using defer. A test function may wish to ensure that an object is closed before some subsequent actions are taken, rather than at the end of the entire test.
If closing the object fails, an error is logged.
func WithTempDir ¶
func WithTempDir(f func(path string))
WithTempDir creates a temporary directory, calls the function with its path, then removes it.
func WithTempFile ¶
func WithTempFile(f func(filePath string))
WithTempFile creates a temporary file, passes its name to the given function, then ensures that the file is deleted.
If for any reason it is not possible to create the file, a panic is raised since the test code cannot continue.
If deletion of the file fails (assuming it has not already been deleted) then an error is logged, but there is no panic.
helpers.WithTempFile(func(path string) { DoSomethingWithTempFile(path) }) // the file is deleted at the end of this block
func WithTempFileData ¶
WithTempFileData is identical to WithTempFile except that it prepopulates the file with the specified data.
Types ¶
This section is empty.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package httphelpers contains convenience tools for testing HTTP client code.
|
Package httphelpers contains convenience tools for testing HTTP client code. |
Package jsonhelpers contains general-purpose functions for manipulating JSON.
|
Package jsonhelpers contains general-purpose functions for manipulating JSON. |
Package matchers provides a flexible test assertion API similar to Java's Hamcrest.
|
Package matchers provides a flexible test assertion API similar to Java's Hamcrest. |
Package testbox provides the ability to run test logic that uses a subset of Go's testing.T methods either inside or outside the regular testing environment.
|
Package testbox provides the ability to run test logic that uses a subset of Go's testing.T methods either inside or outside the regular testing environment. |