Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MockSerial ¶
MockSerial simulates a serial device for testing. It provides configurable behavior including data streams, delays, and error injection.
func NewMockSerial ¶
func NewMockSerial(opts ...MockSerialOption) *MockSerial
NewMockSerial creates a new mock serial device with the given options.
Example ¶
ExampleNewMockSerial demonstrates creating a mock serial device.
package main
import (
"fmt"
"github.com/cthonicasoftware/astrolabe-cli/internal/testutil"
)
func main() {
// Create a mock serial device with pre-configured data
mock := testutil.NewMockSerial(
testutil.WithFrames([][]byte{
[]byte("Hello\n"),
[]byte("World\n"),
}),
)
fmt.Printf("Mock created with %d frames\n", mock.FramesRemaining())
}
Output: Mock created with 2 frames
func (*MockSerial) AddFrame ¶
func (m *MockSerial) AddFrame(frame []byte)
AddFrame appends a frame to the mock's frame list (useful for dynamic testing).
Example ¶
ExampleMockSerial_AddFrame demonstrates adding frames dynamically.
package main
import (
"fmt"
"github.com/cthonicasoftware/astrolabe-cli/internal/testutil"
)
func main() {
mock := testutil.NewMockSerial(
testutil.WithFrames([][]byte{[]byte("Initial\n")}),
)
fmt.Printf("Initial frames: %d\n", mock.FramesRemaining())
// Add more frames
mock.AddFrame([]byte("Second\n"))
mock.AddFrame([]byte("Third\n"))
fmt.Printf("After adding: %d\n", mock.FramesRemaining())
}
Output: Initial frames: 1 After adding: 3
func (*MockSerial) Close ¶
func (m *MockSerial) Close() error
Close simulates closing the serial port.
func (*MockSerial) Frames ¶
func (m *MockSerial) Frames() <-chan []byte
Frames returns the channel for reading frames from the mock serial port.
func (*MockSerial) FramesRemaining ¶
func (m *MockSerial) FramesRemaining() int
FramesRemaining returns the number of frames yet to be emitted.
func (*MockSerial) Meta ¶
func (m *MockSerial) Meta() core.SourceMeta
Meta returns the source metadata for the mock device.
Example ¶
ExampleMockSerial_Meta demonstrates getting source metadata.
package main
import (
"fmt"
"github.com/cthonicasoftware/astrolabe-cli/internal/sources"
"github.com/cthonicasoftware/astrolabe-cli/internal/testutil"
)
func main() {
mock := testutil.NewMockSerial(
testutil.WithConfig(sources.Config{
Port: "/dev/ttyUSB0",
Baud: 115200,
}),
)
meta := mock.Meta()
fmt.Printf("Kind: %s, Port: %s, Baud: %d\n", meta.Kind, meta.Port, meta.Baud)
}
Output: Kind: serial, Port: /dev/ttyUSB0, Baud: 115200
func (*MockSerial) Open ¶
func (m *MockSerial) Open(ctx context.Context) error
Open simulates opening the serial port and starts the read loop.
func (*MockSerial) Reset ¶
func (m *MockSerial) Reset()
Reset allows the mock to be reused for another test.
type MockSerialOption ¶
type MockSerialOption func(*MockSerial)
MockSerialOption configures a MockSerial device.
func WithConfig ¶
func WithConfig(cfg sources.Config) MockSerialOption
WithConfig sets the serial configuration.
Example ¶
ExampleWithConfig demonstrates configuring serial parameters.
package main
import (
"fmt"
"github.com/cthonicasoftware/astrolabe-cli/internal/sources"
"github.com/cthonicasoftware/astrolabe-cli/internal/testutil"
)
func main() {
// Configure mock with specific serial settings
cfg := sources.Config{
Port: "/dev/ttyUSB0",
Baud: 9600,
Parity: "E",
DataBits: 7,
StopBits: "2",
FlowControl: "hardware",
}
mock := testutil.NewMockSerial(testutil.WithConfig(cfg))
meta := mock.Meta()
fmt.Printf("Port: %s, Baud: %d\n", meta.Port, meta.Baud)
}
Output: Port: /dev/ttyUSB0, Baud: 9600
func WithFrameDelay ¶
func WithFrameDelay(delay time.Duration) MockSerialOption
WithFrameDelay sets the delay between frame emissions.
func WithFrames ¶
func WithFrames(frames [][]byte) MockSerialOption
WithFrames sets the sequence of byte frames to emit.
func WithReadError ¶
func WithReadError(err error) MockSerialOption
WithReadError configures an error to return after all frames are exhausted.