use std::sync::Arc;
use crate::{ModuleContext, services::ClientServiceRegistry};
use super::{MockModuleRegistry, MockPlatformCapabilities, MockServerHandle, MockThemeProvider};
pub struct TestModuleContext {
caps: MockPlatformCapabilities,
server: Arc<MockServerHandle>,
theme: MockThemeProvider,
services: Option<ClientServiceRegistry>,
registry: Option<MockModuleRegistry>,
}
impl TestModuleContext {
#[must_use]
pub fn builder() -> TestModuleContextBuilder {
TestModuleContextBuilder::default()
}
#[must_use]
pub fn as_context(&self) -> ModuleContext<'_> {
ModuleContext {
capabilities: &self.caps,
server: Arc::clone(&self.server) as Arc<dyn crate::ServerHandle>,
theme: &self.theme,
services: self.services.as_ref(),
module_registry: self
.registry
.as_ref()
.map(|r| r as &dyn crate::ClientModuleRegistry),
}
}
#[must_use]
pub const fn capabilities(&self) -> &MockPlatformCapabilities {
&self.caps
}
#[must_use]
pub fn server(&self) -> &MockServerHandle {
&self.server
}
#[must_use]
pub const fn theme(&self) -> &MockThemeProvider {
&self.theme
}
}
#[derive(Default)]
pub struct TestModuleContextBuilder {
caps: MockPlatformCapabilities,
server: MockServerHandle,
theme: MockThemeProvider,
services: Option<ClientServiceRegistry>,
registry: Option<MockModuleRegistry>,
}
#[cfg_attr(coverage_nightly, coverage(off))]
impl TestModuleContextBuilder {
#[must_use]
pub const fn grid_size(mut self, width: u16, height: u16) -> Self {
self.caps = self.caps.with_grid_size(width, height);
self
}
#[must_use]
pub const fn dark_mode(mut self, dark: bool) -> Self {
self.caps = self.caps.with_dark_mode(dark);
self
}
#[must_use]
pub fn server_options(
mut self,
options: std::collections::HashMap<String, crate::OptionValue>,
) -> Self {
self.server = self.server.with_options(options);
self
}
#[must_use]
pub fn highlight(mut self, group: &str, style: crate::Style) -> Self {
self.theme = self.theme.with_highlight(group, style);
self
}
#[must_use]
pub fn running_modules(mut self, kinds: &[&str]) -> Self {
self.registry = Some(MockModuleRegistry::new().with_running(kinds));
self
}
#[must_use]
pub fn services(mut self, registry: ClientServiceRegistry) -> Self {
self.services = Some(registry);
self
}
#[must_use]
pub fn build(self) -> TestModuleContext {
TestModuleContext {
caps: self.caps,
server: Arc::new(self.server),
theme: self.theme,
services: self.services,
registry: self.registry,
}
}
}