Documentation
¶
Overview ¶
Package tstkit provides utilities for testing.
Index ¶
- Constants
- func ClockDeterministic(start time.Time, tick time.Duration) func() time.Time
- func ClockFixed(tim time.Time) func() time.Time
- func ClockStartingAt(tim time.Time) func() time.Time
- func TikTak(start time.Time) func() time.Time
- type Buffer
- func (buf *Buffer) Kind() string
- func (buf *Buffer) MustWriteString(s string) int
- func (buf *Buffer) Name() string
- func (buf *Buffer) Reset()
- func (buf *Buffer) SkipExamine() *Buffer
- func (buf *Buffer) String() string
- func (buf *Buffer) Write(p []byte) (n int, err error)
- func (buf *Buffer) WriteString(s string) (n int, err error)
Constants ¶
const ( BufferDry = "dry" // BufferDry enforces no data is written. BufferWet = "wet" // BufferWet enforces data is written and examined. BuffDefault = "default" // BuffDefault applies no cleanup checks. )
Buffer kinds define the behavior of a Buffer during test cleanup.
Variables ¶
This section is empty.
Functions ¶
func ClockDeterministic ¶ added in v0.9.0
ClockDeterministic returns the function with the same signature as time.Now and returning time advancing by the given tick with every call no matter how fast or slow you call it.
func ClockFixed ¶ added in v0.9.0
ClockFixed returns the function with the same signature as time.Now which always returns the given time.
func ClockStartingAt ¶ added in v0.9.0
ClockStartingAt returns the function with the same signature as time.Now and returning time as if the current time was set to the given value.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a thread-safe buffer that wraps bytes.Buffer. It tracks write and read operations and supports test cleanup checks based on its kind.
func DryBuffer ¶
DryBuffer creates a thread-safe Buffer with the dry kind BufferDry, which fails the test during cleanup if any data was written to it. An optional name can be provided for use in test log output. The provided tester.T is used to register cleanup checks and report failures.
Example:
buf := DryBuffer(t, "dry-buffer")
buf.WriteString("data") // Will fail test during cleanup.
func NewBuffer ¶
NewBuffer creates a new thread-safe Buffer with the BuffDefault kind. An optional name can be provided. If no name is provided, Buffer.Name returns an empty string.
Example:
buf := NewBuffer("my-buffer") // Named buffer.
buf := NewBuffer() // Unnamed buffer.
func WetBuffer ¶
WetBuffer creates a thread-safe Buffer with the wet kind BufferWet, which fails the test during cleanup if no data was written or if the contents were not read via Buffer.String. An optional name can be provided for use in test log output. The provided tester.T is used to register cleanup checks and report failures.
Example:
buf := WetBuffer(t, "wet-buffer")
buf.WriteString("data")
// Must call buf.SkipExamine() or buf.String() to avoid test failure.
func (*Buffer) MustWriteString ¶
MustWriteString writes the string s to the buffer, incrementing the write counter. It panics if the write operation fails. This is useful in tests where write failures are unexpected and should halt execution.
func (*Buffer) Name ¶
Name returns the buffer's name or an empty string if no name was provided during creation.
func (*Buffer) Reset ¶
func (buf *Buffer) Reset()
Reset clears the buffer's contents and resets the write and read counters. It is thread-safe and prepares the buffer for reuse in tests.
func (*Buffer) SkipExamine ¶
SkipExamine disables the cleanup requirement for the test to examine the buffer. This is useful when the buffer is WetBuffer but we do not want to examine what was written to it. Implements fluent interface.
func (*Buffer) String ¶
String returns the current contents of the buffer as a string, incrementing the read counter. It is thread-safe and implements the fmt.Stringer interface and is intended for inspecting buffer data during tests.
func (*Buffer) Write ¶
Write writes the byte slice p to the buffer, incrementing the write counter. It is thread-safe and implements the io.Writer interface.
func (*Buffer) WriteString ¶
WriteString writes the string s to the buffer, incrementing the write counter. It is thread-safe and implements the io.StringWriter interface.