Expand description
Run a plugin under cargo test, against an in-process host.
On targets other than wasm, every SDK call goes to the thread’s
MockHost instead of the WIT imports. The mock checks grants exactly
as the viewer’s host does (both use standard-plugin-manifest), keeps
values, records what the plugin emitted and committed, and lays out
surfaces. Harness drives a UiPlugin the way a viewer does:
activate, resize, frames, events.
ⓘ
//! Testing a plugin with `standard_plugin::testing`: the harness drives the
//! plugin as a viewer would, on an in-process host that checks grants like
//! the real one. This file is the `testing` module's documentation example.
// The in-process host exists on non-wasm targets only.
#![cfg(not(target_arch = "wasm32"))]
use standard_plugin::prelude::*;
use standard_plugin::surface::Model;
use standard_plugin::testing::{Harness, MockHost};
/// Shows a counter the plugin keeps in a durable value.
struct Counter {
card: Surface<Cells>,
count: u32,
}
impl UiPlugin for Counter {
fn activate(cx: &mut Context) -> Self {
let count = cx.values().get("counter.count").ok().flatten().unwrap_or(0);
Self {
card: Surface::new("card"),
count,
}
}
fn frame(&mut self, _frame: &Frame) {
self.card
.text(0, 0, &format!("count {}", self.count), Style::DEFAULT);
let _ = self.card.commit();
}
}
#[test]
fn the_harness_lays_out_frames_and_records_commits() {
let mut host = MockHost::new("counter").surface("card", Model::Cells);
host.values.insert("counter.count".into(), "41".into());
let mut harness = Harness::<Counter>::activate(host, "{}");
harness.resize("card", Geometry::cells(12, 1, (0, 0)));
harness.frame(0, 16, standard_plugin::Power::Mains);
let commits = harness.commits("card");
assert_eq!(commits.len(), 1);
assert!(commits[0].full, "the first commit covers the whole card");
harness.plugin(|counter| assert_eq!(counter.card.row_text(0), "count 41 "));
}Structs§
- Daemon
Harness - Drives a
DaemonPluginasstandarddwould: activate,driveat the instants it asks for, events and calls. - Harness
- Drives a
UiPluginas a viewer does, on this thread’sMockHost. - Mock
Call - One call as the host saw it.
- Mock
Commit - One commit as the host saw it.
- Mock
Host - The in-process host. Build one,
installit (or hand it to aHarness), and inspect it after the plugin ran. - Mock
Pane Launch - One pane the plugin opened (
daemon::panes::create), as the host saw it. - Mock
Process - One child the plugin spawned, as the host saw it.
- Mock
Watch - One watch the plugin holds.
- Mock
WebSocket - A WebSocket the plugin opened on the test host.
- Process
Script - What a scripted program does when the plugin spawns it: the output
it writes (delivered as
Event::ProcessOutputfor a piped stream) and its exit status (Event::ProcessExited, and whatwaitanswers).
Enums§
- Call
Kind - How a plugin made a call.