standard-plugin-sdk 0.1.1

Write Standard Code plugins in Rust: wasm components against standard:plugin@2.0.0
Documentation
//! 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    "));
}