Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions test/src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ enum Action_<P: Program> {
CountDown,
}

enum Initializer<'a, P: Program + 'a> {
Boot,
Preset(&'a program::Preset<P::State, P::Message>),
State(P::State),
}

impl<P: Program + 'static> Emulator<P> {
/// Creates a new [`Emulator`] of the [`Program`] with the given [`Mode`] and [`Size`].
///
Expand All @@ -73,7 +79,21 @@ impl<P: Program + 'static> Emulator<P> {
mode: Mode,
size: Size,
) -> Emulator<P> {
Self::with_preset(sender, program, mode, size, None)
Self::initialize(sender, program, mode, size, Initializer::Boot)
}

/// Creates a new [`Emulator`] analogously to [`new`](Self::new), but it also takes
/// an existing [`Program::State`] as the initial state.
///
/// When the [`Emulator`] has finished booting, an [`Event::Ready`] will be produced.
pub fn with_state(
sender: mpsc::Sender<Event<P>>,
program: &P,
mode: Mode,
size: Size,
state: P::State,
) -> Emulator<P> {
Self::initialize(sender, program, mode, size, Initializer::State(state))
}

/// Creates a new [`Emulator`] analogously to [`new`](Self::new), but it also takes a
Expand All @@ -86,6 +106,26 @@ impl<P: Program + 'static> Emulator<P> {
mode: Mode,
size: Size,
preset: Option<&program::Preset<P::State, P::Message>>,
) -> Emulator<P> {
Self::initialize(
sender,
program,
mode,
size,
preset.map(Initializer::Preset).unwrap_or(Initializer::Boot),
)
}

/// Creates a new [`Emulator`] analogously to [`new`](Self::new), but it also takes a
/// [`program::Preset`] that will be used as the initial state.
///
/// When the [`Emulator`] has finished booting, an [`Event::Ready`] will be produced.
fn initialize(
sender: mpsc::Sender<Event<P>>,
program: &P,
mode: Mode,
size: Size,
initializer: Initializer<'_, P>,
) -> Emulator<P> {
use renderer::Headless;

Expand All @@ -104,12 +144,10 @@ impl<P: Program + 'static> Emulator<P> {

let runtime = Runtime::new(executor, sender);

let (state, task) = runtime.enter(|| {
if let Some(preset) = preset {
preset.boot()
} else {
program.boot()
}
let (state, task) = runtime.enter(|| match initializer {
Initializer::Boot => program.boot(),
Initializer::Preset(preset) => preset.boot(),
Initializer::State(state) => (state, Task::none()),
});

let mut emulator = Self {
Expand Down
89 changes: 64 additions & 25 deletions tester/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ enum Event {
Play,
Import,
Export,
Reset,
Imported(Result<Ice, ice::ParseError>),
Edit,
Edited(text_editor::Action),
Expand Down Expand Up @@ -232,17 +233,29 @@ impl<P: Program + 'static> Tester<P> {
}
Event::Record => {
self.edit = None;
self.instructions.clear();

let (sender, receiver) = mpsc::channel(1);

let emulator = Emulator::with_preset(
sender,
program,
self.mode,
self.viewport,
self.preset(program),
);
let emulator = if let State::Asserting { state, .. } =
std::mem::replace(&mut self.state, State::Empty)
{
Emulator::with_state(
sender,
program,
self.mode,
self.viewport,
state,
)
} else {
self.instructions.clear();
Emulator::with_preset(
sender,
program,
self.mode,
self.viewport,
self.preset(program),
)
};

self.state = State::Recording { emulator };

Expand Down Expand Up @@ -342,6 +355,19 @@ impl<P: Program + 'static> Tester<P> {
})
.discard()
}
Event::Reset => {
self.edit = None;
self.instructions.clear();

let (state, _) = self
.preset(program)
.map(program::Preset::boot)
.unwrap_or_else(|| program.boot());

self.state = State::Idle { state };

Task::none()
}
Event::Imported(Ok(ice)) => {
self.viewport = ice.viewport;
self.mode = ice.mode;
Expand Down Expand Up @@ -488,25 +514,33 @@ impl<P: Program + 'static> Tester<P> {
let mut interaction = Some(interaction);

while let Some(new_interaction) = interaction.take() {
if let Some(Instruction::Interact(last_interaction)) =
self.instructions.pop()
{
let (merged_interaction, new_interaction) =
last_interaction.merge(new_interaction);

if let Some(new_interaction) = new_interaction {
self.instructions.push(Instruction::Interact(
merged_interaction,
));

self.instructions
.push(Instruction::Interact(new_interaction));
if let Some(last_instruction) = self.instructions.pop() {
if let Instruction::Interact(last_interaction) =
last_instruction
{
let (merged_interaction, new_interaction) =
last_interaction.merge(new_interaction);

if let Some(new_interaction) = new_interaction {
self.instructions.push(Instruction::Interact(
merged_interaction,
));

self.instructions.push(Instruction::Interact(
new_interaction,
));
} else {
interaction = Some(merged_interaction);
}
} else {
interaction = Some(merged_interaction);
self.instructions.extend([
last_instruction,
Instruction::Interact(new_interaction),
]);
}
} else {
self.instructions
.push(Instruction::Interact(new_interaction));
.push(Instruction::Interact(new_interaction))
}
}

Expand Down Expand Up @@ -807,6 +841,10 @@ impl<P: Program + 'static> Tester<P> {
.style(button::danger)
};

let reset = control(icon::cancel())
.on_press(Event::Reset)
.style(button::danger);

let import = control(icon::folder())
.on_press_maybe((!self.is_busy()).then_some(Event::Import))
.style(button::secondary);
Expand All @@ -819,8 +857,9 @@ impl<P: Program + 'static> Tester<P> {
)
.style(button::success);

let controls =
row![import, export, play, record].height(30).spacing(10);
let controls = row![import, export, play, record, reset]
.height(30)
.spacing(10);

column![instructions, controls].spacing(10).align_x(Center)
};
Expand Down