Skip to main content

ProgramSimulator

Struct ProgramSimulator 

Source
pub struct ProgramSimulator<M: Model> { /* private fields */ }
Expand description

Deterministic simulator for Model testing.

Runs model logic without any terminal or IO dependencies. Events can be injected, messages sent directly, and frames captured for snapshot testing.

Implementations§

Source§

impl<M: Model> ProgramSimulator<M>

Source

pub fn new(model: M) -> Self

Create a new simulator with the given model.

The model is not initialized until init is called.

Source

pub fn with_registry(model: M, registry: Arc<StateRegistry>) -> Self

Create a new simulator with the given model and persistence registry.

When provided, Cmd::SaveState/Cmd::RestoreState will flush/load through the registry, mirroring runtime behavior.

Source

pub fn init(&mut self)

Initialize the model by calling Model::init() and executing returned commands.

Should be called once before injecting events or capturing frames.

Source

pub fn inject_events(&mut self, events: &[Event])

Inject terminal events into the model.

Each event is converted to a message via From<Event> and dispatched through Model::update(). Commands returned from update are executed.

Source

pub fn inject_event(&mut self, event: Event)

Inject a single terminal event into the model.

The event is converted to a message via From<Event> and dispatched through Model::update(). Commands returned from update are executed.

Source

pub fn send(&mut self, msg: M::Message)

Send a specific message to the model.

The message is dispatched through Model::update() and returned commands are executed.

Source

pub fn capture_frame(&mut self, width: u16, height: u16) -> &Buffer

Capture the current frame at the given dimensions.

Calls Model::view() to render into a fresh buffer and stores the result. Returns a reference to the captured buffer.

Source

pub fn frames(&self) -> &[Buffer]

Get all captured frame buffers.

Source

pub fn last_frame(&self) -> Option<&Buffer>

Get the most recently captured frame buffer, if any.

Source

pub fn frame_count(&self) -> usize

Get the number of captured frames.

Source

pub fn model(&self) -> &M

Get a reference to the model.

Source

pub fn model_mut(&mut self) -> &mut M

Get a mutable reference to the model.

Source

pub fn is_running(&self) -> bool

Check if the simulated program is still running.

Returns false after a Cmd::Quit has been executed.

Source

pub fn tick_rate(&self) -> Option<Duration>

Get the current tick rate (if any).

Source

pub fn logs(&self) -> &[String]

Get all log messages emitted via Cmd::Log.

Source

pub fn command_log(&self) -> &[CmdRecord]

Get the command execution log.

Source

pub fn clear_frames(&mut self)

Clear all captured frames.

Source

pub fn clear_logs(&mut self)

Clear all logs.

Auto Trait Implementations§

§

impl<M> Freeze for ProgramSimulator<M>
where M: Freeze,

§

impl<M> !RefUnwindSafe for ProgramSimulator<M>

§

impl<M> Send for ProgramSimulator<M>
where M: Send,

§

impl<M> Sync for ProgramSimulator<M>
where M: Sync,

§

impl<M> Unpin for ProgramSimulator<M>
where M: Unpin,

§

impl<M> !UnwindSafe for ProgramSimulator<M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more