Expand description
Unit-test support for plugins: run a Plugin against an in-memory
mock host.
use eov_plugin_api::prelude::*;
use eov_plugin_api::testing::TestHarness;
struct Counter {
host: Host,
}
impl Plugin for Counter {
fn create(host: Host, _init: PluginInit) -> Result<Self, String> {
Ok(Self { host })
}
fn on_event(&self, event: HostEvent) -> Result<(), String> {
if let HostEvent::FileOpened { file } = event {
self.host.info(format!("opened {}", file.filename));
}
Ok(())
}
}
let harness = TestHarness::<Counter>::new().unwrap();
harness
.send(HostEvent::FileOpened {
file: FileInfo {
file_id: 1,
filename: "a.svs".into(),
..Default::default()
},
})
.unwrap();
assert_eq!(harness.calls_to("log").len(), 1);The mock answers every method the real host knows with a plausible
result, keeps layers, surfaces, settings and contributions in memory so
tests can inspect them, and records every call. Unlike eov, it does not
turn host.warn/host.error into notifications: find those among the
recorded log calls by their level.
§Languages in tests
The language and catalog tr! translates with belong to
the whole plugin library, not to one harness: every
TestHarness::new or TestHarness::with_init sets them from
PluginInit::language (test_init gives English), and
HostEvent::LanguageChanged switches them for every test of the
binary. Tests run in parallel, so a test that changes the language can
change the texts another test compares, and creating a harness can put
it back to English in the middle of that test. Run the tests that
change languages in an integration-test binary of their own
(tests/language.rs), one at a time behind a lock, as eov’s own plugins
do; or hold the same lock while every harness is created and used.
The thread-local-language feature of this crate, on its
dev-dependency (eov-plugin-api = { version = "1.1", features = ["thread-local-language"] } under [dev-dependencies]), gives each
thread its own language and catalog instead. A harness then sets them
for the thread that creates it, and LanguageChanged for the thread
that sends it; texts a plugin makes on threads of its own are in
English, since new threads start in English. It suits plugins that make
their texts on the thread that delivers events.
A harness finds the plugin’s catalogs as eov does: through the [i18n]
table of plugin.toml in PluginInit::plugin_root. test_init’s
root is an empty temporary folder, so a language set on it changes only
i18n::current (the decimal separator,
isolation), and tr! stays English without a word. To test
translations, put plugin.toml and
<catalogs>/<tag>/LC_MESSAGES/<domain>.po into plugin_root before
creating the harness, as the example plugin’s
texts_follow_the_language_of_the_interface test does, or set a catalog
with i18n::set_catalog after creating it
(creating another harness sets the language and catalog again).
Structs§
- Mock
Host - The mock host object handed to the plugin.
- Mock
Layer - A layer as the mock host stores it.
- Mock
State - Everything the mock host knows. Tests may read and modify it freely.
- Mock
Surface - A surface as the mock host stores it.
- Recorded
Call - One recorded host call.
- Test
Harness - Drives a plugin through the same FFI adapter the host uses.
- Test
Slide - A slide opened with
TestHarness::open_slide.
Functions§
- test_
init - Default init for tests: unique, writable temporary directories, and no
language, so the plugin’s texts are in English (see the
module documentation on tests that switch
languages). Its
plugin_rootholds noplugin.tomland no catalogs.