Skip to main content

ProgramSimulator

Struct ProgramSimulator 

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

A simulator for testing Model implementations without a terminal.

§Example

use bubbletea::{Model, Message, Cmd, simulator::ProgramSimulator};

struct Counter { count: i32 }

impl Model for Counter {
    fn init(&self) -> Option<Cmd> { None }
    fn update(&mut self, msg: Message) -> Option<Cmd> {
        if let Some(n) = msg.downcast::<i32>() {
            self.count += n;
        }
        None
    }
    fn view(&self) -> String {
        format!("Count: {}", self.count)
    }
}

let mut sim = ProgramSimulator::new(Counter { count: 0 });
sim.send(Message::new(5));
sim.send(Message::new(3));
sim.step();
sim.step();

assert_eq!(sim.model().count, 8);

Implementations§

Source§

impl<M: Model> ProgramSimulator<M>

Source

pub fn new(model: M) -> Self

Create a new simulator with the given model.

Source

pub fn init(&mut self) -> Option<Cmd>

Initialize the model, calling init() and capturing any returned command.

Source

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

Queue a message for processing.

Source

pub fn step(&mut self) -> Option<Cmd>

Process one message from the queue, calling update and view.

Returns the command returned by update, if any.

Source

pub fn run_until_empty(&mut self) -> usize

Process all pending messages until the queue is empty or quit is requested.

Returns the number of messages processed. Has a built-in safety limit of 1000 iterations to prevent infinite loops.

Source

pub fn run_until_quit(&mut self, max_steps: usize) -> usize

Run until quit is received or max_steps is reached.

Returns the number of steps processed.

Source

pub fn model(&self) -> &M

Get a reference to the current model state.

Source

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

Get a mutable reference to the current model state.

Source

pub fn into_model(self) -> M

Consume the simulator and return the final model.

Source

pub fn stats(&self) -> &SimulationStats

Get the simulation statistics.

Source

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

Get all captured view outputs.

Source

pub fn last_view(&self) -> Option<&str>

Get the most recent view output.

Source

pub fn is_quit(&self) -> bool

Check if quit has been requested.

Source

pub fn is_initialized(&self) -> bool

Check if the model has been initialized.

Source

pub fn pending_count(&self) -> usize

Get the number of pending messages.

Source

pub fn sim_key(&mut self, c: char)

Simulate a key press (single character).

§Example
use bubbletea::simulator::ProgramSimulator;

let mut sim = ProgramSimulator::new(MyModel);
sim.init();
sim.sim_key('a');  // Queue 'a' key press
sim.step();
Source

pub fn sim_key_type(&mut self, key_type: KeyType)

Simulate a special key press (Enter, Escape, Arrow keys, etc.).

Source

pub fn sim_mouse( &mut self, x: u16, y: u16, button: MouseButton, action: MouseAction, )

Simulate a mouse event.

§Arguments
  • x - Column position (0-indexed)
  • y - Row position (0-indexed)
  • button - Which button (Left, Right, Middle, etc.)
  • action - What happened (Press, Release, Motion)
Source

pub fn sim_resize(&mut self, width: u16, height: u16)

Simulate a window resize event.

Source

pub fn sim_paste(&mut self, text: &str)

Simulate a paste operation (bracketed paste).

Auto Trait Implementations§

§

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

§

impl<M> !RefUnwindSafe for ProgramSimulator<M>

§

impl<M> Send for ProgramSimulator<M>

§

impl<M> !Sync for ProgramSimulator<M>

§

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