Skip to main content

Module testing

Module testing 

Source
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§

DaemonHarness
Drives a DaemonPlugin as standardd would: activate, drive at the instants it asks for, events and calls.
Harness
Drives a UiPlugin as a viewer does, on this thread’s MockHost.
MockCall
One call as the host saw it.
MockCommit
One commit as the host saw it.
MockHost
The in-process host. Build one, install it (or hand it to a Harness), and inspect it after the plugin ran.
MockPaneLaunch
One pane the plugin opened (daemon::panes::create), as the host saw it.
MockProcess
One child the plugin spawned, as the host saw it.
MockWatch
One watch the plugin holds.
MockWebSocket
A WebSocket the plugin opened on the test host.
ProcessScript
What a scripted program does when the plugin spawns it: the output it writes (delivered as Event::ProcessOutput for a piped stream) and its exit status (Event::ProcessExited, and what wait answers).

Enums§

CallKind
How a plugin made a call.

Functions§

install
Makes host this thread’s host. A thread without one gets MockHost::new("test-plugin") on its first call.
with_host
Runs f on this thread’s host.