use std::sync::Arc;
use std::time::Duration;
use crate::context::InvocationContext;
use crate::plugin::{Plugin, PluginConfig};
use crate::plugin_manager::PluginManager;
fn manager(plugins: Vec<Plugin>) -> Arc<PluginManager> {
Arc::new(PluginManager::new(plugins, Duration::ZERO).expect("NewPluginManager()"))
}
fn test_plugin() -> Plugin {
Plugin::new(PluginConfig {
name: "test-plugin".into(),
..Default::default()
})
}
#[test]
fn test_plugin_manager_has_plugins() {
let with_plugin = manager(vec![test_plugin()]);
let empty = manager(vec![]);
let cases: [(&str, Option<Arc<PluginManager>>, bool); 3] = [
("nil manager", None, false),
("empty manager", Some(empty), false),
("manager with a plugin", Some(with_plugin), true),
];
for (name, pm, want) in cases {
let got = pm.as_ref().is_some_and(|m| m.has_plugins());
assert_eq!(got, want, "{name}: HasPlugins()");
}
}
#[test]
fn test_from_context() {
let pm = manager(vec![]);
let ctx = InvocationContext::builder()
.plugin_manager(pm.clone())
.build_invocation();
let got = ctx.plugin_manager().expect("FromContext() = nil");
assert!(
Arc::ptr_eq(&got, &pm),
"FromContext() did not return the manager stored by ToContext"
);
assert!(
InvocationContext::builder()
.build_invocation()
.plugin_manager()
.is_none(),
"FromContext() on an empty context = non-nil, want nil"
);
}
#[test]
fn test_take_inherited_plugins() {
let pm = manager(vec![test_plugin()]);
let ctx = InvocationContext::builder()
.plugin_manager(pm.clone())
.build_invocation();
let (_, got) = ctx.take_inherited_plugins();
assert!(
got.is_none(),
"TakeInheritedPlugins() without a mark = non-nil, want nil"
);
let (cleared, got) = ctx.with_inherited_plugins().take_inherited_plugins();
assert!(
got.is_some_and(|g| Arc::ptr_eq(&g, &pm)),
"TakeInheritedPlugins() did not return the marked manager"
);
let (_, again) = cleared.take_inherited_plugins();
assert!(
again.is_none(),
"TakeInheritedPlugins() on a cleared context = non-nil, want nil"
);
}