Documentation
¶
Overview ¶
Package testutils provides common testing utilities for reader tests.
Index ¶
- func AssertNoScan(t *testing.T, ch chan readers.Scan, timeout time.Duration)
- func AssertScanReceived(t *testing.T, ch chan readers.Scan, timeout time.Duration) readers.Scan
- func CreateTempDevicePath(t *testing.T) string
- func CreateTestScanChannel(_ *testing.T) chan readers.Scan
- type MockSerialPort
- type SerialPort
- type SerialPortFactory
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertNoScan ¶
AssertNoScan verifies that no scan is received on the channel within the timeout. Fails the test if a scan is received.
func AssertScanReceived ¶
AssertScanReceived waits for a scan to be received on the channel within the timeout. Returns the received scan. Fails the test if no scan is received within timeout.
func CreateTempDevicePath ¶
CreateTempDevicePath creates a temporary file to represent a device path for testing. On Windows systems, it returns a COM port path. On Unix systems, it creates a temporary file and registers cleanup with t.Cleanup().
Types ¶
type MockSerialPort ¶
type MockSerialPort struct {
ReadError error
CloseError error
TimeoutErr error
ReadFunc func(p []byte) (n int, err error)
ReadData []byte
ReadIndex int
Closed bool
// contains filtered or unexported fields
}
MockSerialPort is a mock implementation of serial port for testing. It implements the common serial port interface used by readers.
func NewMockSerialPort ¶
func NewMockSerialPort() *MockSerialPort
NewMockSerialPort creates a new mock serial port for testing.
func (*MockSerialPort) Close ¶
func (m *MockSerialPort) Close() error
Close implements the Close method for serial ports.
func (*MockSerialPort) IsClosed ¶
func (m *MockSerialPort) IsClosed() bool
IsClosed returns true if the port has been closed (thread-safe).
func (*MockSerialPort) Read ¶
func (m *MockSerialPort) Read(p []byte) (n int, err error)
Read implements the Read method for serial ports. It supports custom read functions, error injection, and buffered data reading.
func (*MockSerialPort) SetReadTimeout ¶
func (m *MockSerialPort) SetReadTimeout(_ time.Duration) error
SetReadTimeout implements the SetReadTimeout method for serial ports.
type SerialPort ¶
type SerialPort interface {
Read(p []byte) (n int, err error)
Close() error
SetReadTimeout(t time.Duration) error
}
SerialPort defines the interface for serial port operations. This interface is used by serial-based readers for dependency injection and testing.
func DefaultSerialPortFactory ¶
func DefaultSerialPortFactory(path string, mode *serial.Mode) (SerialPort, error)
DefaultSerialPortFactory is the default factory that opens real serial ports. It wraps the go.bug.st/serial library for production use.
type SerialPortFactory ¶
type SerialPortFactory func(path string, mode *serial.Mode) (SerialPort, error)
SerialPortFactory creates a serial port connection. This factory pattern allows readers to be testable by injecting mock implementations.