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>
impl<M: Model> ProgramSimulator<M>
Sourcepub fn init(&mut self) -> Option<Cmd>
pub fn init(&mut self) -> Option<Cmd>
Initialize the model, calling init() and capturing any returned command.
Sourcepub fn step(&mut self) -> Option<Cmd>
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.
Sourcepub fn run_until_empty(&mut self) -> usize
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.
Sourcepub fn run_until_quit(&mut self, max_steps: usize) -> usize
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.
Sourcepub fn into_model(self) -> M
pub fn into_model(self) -> M
Consume the simulator and return the final model.
Sourcepub fn stats(&self) -> &SimulationStats
pub fn stats(&self) -> &SimulationStats
Get the simulation statistics.
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Check if the model has been initialized.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Get the number of pending messages.
Sourcepub fn sim_key(&mut self, c: char)
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();Sourcepub fn sim_key_type(&mut self, key_type: KeyType)
pub fn sim_key_type(&mut self, key_type: KeyType)
Simulate a special key press (Enter, Escape, Arrow keys, etc.).
Sourcepub fn sim_mouse(
&mut self,
x: u16,
y: u16,
button: MouseButton,
action: MouseAction,
)
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)
Sourcepub fn sim_resize(&mut self, width: u16, height: u16)
pub fn sim_resize(&mut self, width: u16, height: u16)
Simulate a window resize event.