mock

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseErrorReadCloser

type CloseErrorReadCloser struct {
	// contains filtered or unexported fields
}

CloseErrorReadCloser is a mock io.ReadCloser where only the Close() method returns an error. This is useful for testing scenarios where reads succeed but closing fails.

func NewCloseErrorReadCloser

func NewCloseErrorReadCloser(r io.Reader, err error) *CloseErrorReadCloser

NewCloseErrorReadCloser creates a new ReadCloser that reads successfully but returns an error when Close() is called. This simulates partial failure scenarios.

func (*CloseErrorReadCloser) Close

func (c *CloseErrorReadCloser) Close() error

Close always returns the configured error, simulating a close failure while allowing reads to succeed.

func (*CloseErrorReadCloser) Read

func (c *CloseErrorReadCloser) Read(p []byte) (int, error)

Read implements the io.Reader interface by delegating to the underlying reader. This method always succeeds, allowing testing of close error scenarios.

type CloseErrorWriteCloser

type CloseErrorWriteCloser struct {
	// contains filtered or unexported fields
}

CloseErrorWriteCloser is a mock io.WriteCloser where only the Close() method returns an error. This is useful for testing scenarios where writes succeed but closing fails.

func NewCloseErrorWriteCloser

func NewCloseErrorWriteCloser(w io.Writer, err error) *CloseErrorWriteCloser

NewCloseErrorWriteCloser creates a new WriteCloser that writes successfully but returns an error when Close() is called. This simulates partial failure scenarios.

func (*CloseErrorWriteCloser) Close

func (c *CloseErrorWriteCloser) Close() error

Close always returns the configured error, simulating a close failure while allowing writes to succeed.

func (*CloseErrorWriteCloser) Write

func (c *CloseErrorWriteCloser) Write(p []byte) (n int, err error)

Write implements the io.Writer interface by delegating to the underlying writer. This method always succeeds, allowing testing of close error scenarios.

type ErrorFile

type ErrorFile struct {
	// contains filtered or unexported fields
}

ErrorFile is a mock file implementation that always returns errors. This is useful for testing error handling paths in code that operates on files.

func NewErrorFile

func NewErrorFile(err error) *ErrorFile

NewErrorFile creates a new error file that will return the specified error for all file operations. This is commonly used to test error scenarios.

func (*ErrorFile) Close

func (e *ErrorFile) Close() error

Close always returns the configured error, simulating a file close failure.

func (*ErrorFile) Read

func (e *ErrorFile) Read(p []byte) (int, error)

Read always returns the configured error, simulating a file read failure.

func (*ErrorFile) ReadDir

func (e *ErrorFile) ReadDir(count int) ([]fs.DirEntry, error)

ReadDir always returns the configured error, simulating a directory read failure.

func (*ErrorFile) Seek

func (e *ErrorFile) Seek(offset int64, whence int) (int64, error)

Seek always returns the configured error, simulating a file seek failure.

func (*ErrorFile) Stat

func (e *ErrorFile) Stat() (os.FileInfo, error)

Stat always returns the configured error, simulating a file stat failure.

func (*ErrorFile) Write

func (e *ErrorFile) Write(p []byte) (n int, err error)

Write always returns the configured error, simulating a file write failure.

type ErrorHasher

type ErrorHasher struct {
	// contains filtered or unexported fields
}

ErrorHasher is a mock implementation of hash.Hash that can return errors on Write operations. This is useful for testing error handling in code that uses hash.Hash interfaces.

func NewErrorHasher

func NewErrorHasher(writeErr error) *ErrorHasher

NewErrorHasher creates a new ErrorHasher that will return the specified error when Write() is called. This is useful for testing hash write error scenarios.

func (*ErrorHasher) BlockSize

func (h *ErrorHasher) BlockSize() int

BlockSize implements the hash.Hash interface and returns a mock block size.

func (*ErrorHasher) Reset

func (h *ErrorHasher) Reset()

Reset implements the hash.Hash interface but does nothing in this mock.

func (*ErrorHasher) Size

func (h *ErrorHasher) Size() int

Size implements the hash.Hash interface and returns a mock hash size.

func (*ErrorHasher) Sum

func (h *ErrorHasher) Sum(b []byte) []byte

Sum implements the hash.Hash interface and returns a mock hash value. This always succeeds and returns a unique mock hash for testing.

func (*ErrorHasher) Write

func (h *ErrorHasher) Write(p []byte) (n int, err error)

Write implements the hash.Hash interface and returns the configured error. This simulates a hash write failure for testing purposes.

type ErrorReadWriteCloser

type ErrorReadWriteCloser struct {
	Err error // The error to return for all read, write, and close operations
}

ErrorReadWriteCloser is a mock that implements io.Reader, io.Writer, and io.Closer interfaces, always returning the specified error for all operations. This is useful for testing scenarios where all I/O operations fail, such as network failures or corrupted streams.

func NewErrorReadWriteCloser

func NewErrorReadWriteCloser(err error) *ErrorReadWriteCloser

NewErrorReadWriteCloser creates a new ErrorReadWriteCloser that will return the specified error for all read, write, and close operations. This mock is particularly useful for testing error handling in streaming operations where all I/O methods need to fail consistently.

func (*ErrorReadWriteCloser) Close

func (e *ErrorReadWriteCloser) Close() error

Close always returns the configured error, simulating a close failure. This method implements the io.Closer interface for consistent error testing.

func (*ErrorReadWriteCloser) Read

func (e *ErrorReadWriteCloser) Read(p []byte) (int, error)

Read always returns the configured error, simulating a read failure. This method implements the io.Reader interface for consistent error testing.

func (*ErrorReadWriteCloser) Write

func (e *ErrorReadWriteCloser) Write(p []byte) (int, error)

Write always returns the configured error, simulating a write failure. This method implements the io.Writer interface for consistent error testing.

type ErrorWriteAfterN

type ErrorWriteAfterN struct {
	N   int   // Number of successful writes before returning error
	Err error // The error to return after N successful writes
	// contains filtered or unexported fields
}

ErrorWriteAfterN is a mock io.Writer that succeeds for the first N writes and then returns an error for all subsequent writes. This is useful for testing scenarios where a writer works initially but fails after a certain number of operations, such as disk full errors or connection drops.

func NewErrorWriteAfterN

func NewErrorWriteAfterN(n int, err error) *ErrorWriteAfterN

NewErrorWriteAfterN creates a new ErrorWriteAfterN that will allow N successful writes before returning the specified error. This is commonly used to test partial write scenarios and error recovery in streaming operations.

func (*ErrorWriteAfterN) Reset

func (e *ErrorWriteAfterN) Reset()

Reset resets the write counter and total bytes, allowing the mock to be reused.

func (*ErrorWriteAfterN) TotalBytes

func (e *ErrorWriteAfterN) TotalBytes() int

TotalBytes returns the total number of bytes successfully written (for testing).

func (*ErrorWriteAfterN) Write

func (e *ErrorWriteAfterN) Write(p []byte) (int, error)

Write implements the io.Writer interface. It succeeds for the first N calls and returns the configured error for all subsequent calls.

func (*ErrorWriteAfterN) WriteCount

func (e *ErrorWriteAfterN) WriteCount() int

WriteCount returns the number of write operations attempted (for testing).

type ErrorWriteCloser

type ErrorWriteCloser struct {
	// contains filtered or unexported fields
}

ErrorWriteCloser is a mock io.WriteCloser that always returns errors. Useful for testing error handling in code that writes to files or other writers.

func NewErrorWriteCloser

func NewErrorWriteCloser(err error) *ErrorWriteCloser

NewErrorWriteCloser creates a new error WriteCloser that will return the specified error for all write and close operations.

func (*ErrorWriteCloser) Close

func (e *ErrorWriteCloser) Close() error

Close always returns the configured error, simulating a close failure.

func (*ErrorWriteCloser) Write

func (e *ErrorWriteCloser) Write(p []byte) (n int, err error)

Write always returns the configured error, simulating a write failure.

type File

type File struct {
	// contains filtered or unexported fields
}

File is a mock implementation of the fs.File interface for testing purposes. It provides an in-memory file representation that can be used to simulate file operations without touching the actual file system.

func NewFile

func NewFile(data []byte, name string) *File

NewFile creates a new mock file with the specified data and name. This function is commonly used in tests to create file-like objects that can be passed to functions expecting file interfaces.

func (*File) Bytes

func (f *File) Bytes() []byte

Bytes returns the current file content (for testing)

func (*File) Close

func (f *File) Close() error

Close implements the io.Closer interface for mock file operations. Marks the file as closed, preventing further read operations.

func (*File) Read

func (f *File) Read(p []byte) (int, error)

Read implements the io.Reader interface for mock file operations. It reads data from the current position and advances the position accordingly. Returns os.ErrClosed if the file has been closed, or io.EOF when reaching the end.

func (*File) ReadDir

func (f *File) ReadDir(count int) ([]fs.DirEntry, error)

ReadDir implements the fs.ReadDirFile interface. Since this mock represents a regular file, not a directory, it always returns an error.

func (*File) Reset

func (f *File) Reset()

Reset resets the file position to the beginning

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

Seek implements the io.Seeker interface for mock file operations. Allows positioning the file pointer at different locations within the file. Supports seeking from start, current position, or end of file.

func (*File) Stat

func (f *File) Stat() (os.FileInfo, error)

Stat returns file information for the mock file. Creates a fileInfo object with the file's name and size based on data length.

func (*File) Truncate

func (f *File) Truncate(n int)

Truncate truncates the file to the specified size

func (*File) Write

func (f *File) Write(p []byte) (n int, err error)

Write implements the io.Writer interface for mock file operations. It writes data to the current position and advances the position accordingly. If writing beyond the current data length, the file is extended. Returns os.ErrClosed if the file has been closed.

type WriteCloser

type WriteCloser struct {
	// contains filtered or unexported fields
}

WriteCloser is a mock implementation of io.WriteCloser for testing purposes. It wraps an io.Writer and adds close functionality with state tracking.

func NewWriteCloser

func NewWriteCloser(w io.Writer) *WriteCloser

NewWriteCloser creates a new mock WriteCloser that wraps the provided io.Writer. This is useful for testing code that requires both write and close operations.

func (*WriteCloser) Close

func (w *WriteCloser) Close() error

Close implements the io.Closer interface for the mock WriteCloser. Marks the WriteCloser as closed and prevents further write operations.

func (*WriteCloser) Write

func (w *WriteCloser) Write(p []byte) (n int, err error)

Write implements the io.Writer interface by delegating to the underlying writer. Returns os.ErrClosed if the WriteCloser has been closed.

Jump to

Keyboard shortcuts

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