testutils

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package testutils provides common testing utilities for reader tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertNoScan

func AssertNoScan(t *testing.T, ch chan readers.Scan, timeout time.Duration)

AssertNoScan verifies that no scan is received on the channel within the timeout. Fails the test if a scan is received.

func AssertScanReceived

func AssertScanReceived(t *testing.T, ch chan readers.Scan, timeout time.Duration) readers.Scan

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

func CreateTempDevicePath(t *testing.T) string

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().

func CreateTestScanChannel

func CreateTestScanChannel(_ *testing.T) chan readers.Scan

CreateTestScanChannel creates a buffered channel for reader scans with capacity of 10.

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.

Jump to

Keyboard shortcuts

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