pub struct MockGameAPI {
pub registry: Registry,
/* private fields */
}Expand description
A mock implementation of GameAPI for unit-testing plugins natively.
Stores all state in-memory (no WASM, no ECS). Useful for testing plugin logic without spinning up the full engine.
§Example
use openstranded_plugin_api::test_utils::MockGameAPI;
use openstranded_plugin_api::{GameAPI, Value, Service, ServiceError};
struct MyService;
impl Service for MyService {
fn call(&self, _method: &str, _args: &[Value]) -> Result<Value, ServiceError> {
Ok(Value::String("ok".into()))
}
}
let mut api = MockGameAPI::new();
api.register_service("my", Box::new(MyService)).unwrap();
assert!(api.has_service("my"));
// Check emitted logs
assert!(api.logs().is_empty());Fields§
§registry: RegistryIn-memory registry with test data.
Implementations§
Source§impl MockGameAPI
impl MockGameAPI
Sourcepub fn with_registry_file(domain: &str, filename: &str, data: &[u8]) -> Self
pub fn with_registry_file(domain: &str, filename: &str, data: &[u8]) -> Self
Create a mock API pre-populated with a single registry file.
Convenience for tests that need registry data.
§Example
use openstranded_plugin_api::test_utils::MockGameAPI;
use openstranded_plugin_api::GameAPI;
let api = MockGameAPI::with_registry_file(
"items", "items_edible.ron",
b"[{ \"id\": 1, \"name\": \"Wood\" }]"
);
let entries = api.registry_domain("items").unwrap();
assert_eq!(entries.len(), 1);Sourcepub fn logs(&self) -> Vec<(LogLevel, String)>
pub fn logs(&self) -> Vec<(LogLevel, String)>
Get all accumulated log messages (for assertions in tests).
Sourcepub fn has_log(&self, level: LogLevel, msg_substring: &str) -> bool
pub fn has_log(&self, level: LogLevel, msg_substring: &str) -> bool
Check whether a specific log message exists.
Sourcepub fn service_domains(&self) -> Vec<&str>
pub fn service_domains(&self) -> Vec<&str>
Get all registered service domains.
Trait Implementations§
Source§impl Default for MockGameAPI
impl Default for MockGameAPI
Source§impl GameAPI for MockGameAPI
impl GameAPI for MockGameAPI
Source§fn registry_domain(
&self,
name: &str,
) -> Result<Vec<RegistryEntry>, ServiceError>
fn registry_domain( &self, name: &str, ) -> Result<Vec<RegistryEntry>, ServiceError>
Get all file entries for a domain.
Source§fn registry_file(
&self,
domain: &str,
filename: &str,
) -> Result<Vec<u8>, ServiceError>
fn registry_file( &self, domain: &str, filename: &str, ) -> Result<Vec<u8>, ServiceError>
Get raw bytes of a specific file within a domain.
Source§fn register_service(
&mut self,
domain: &str,
service: Box<dyn Service>,
) -> Result<(), ServiceError>
fn register_service( &mut self, domain: &str, service: Box<dyn Service>, ) -> Result<(), ServiceError>
Register a service under the given domain name.
Source§fn get_service(&self, domain: &str) -> Result<&dyn Service, ServiceError>
fn get_service(&self, domain: &str) -> Result<&dyn Service, ServiceError>
Get a reference to a registered service.
Source§fn has_service(&self, domain: &str) -> bool
fn has_service(&self, domain: &str) -> bool
Check whether a service domain is registered.
Auto Trait Implementations§
impl !Freeze for MockGameAPI
impl !RefUnwindSafe for MockGameAPI
impl !Sync for MockGameAPI
impl !UnwindSafe for MockGameAPI
impl Send for MockGameAPI
impl Unpin for MockGameAPI
impl UnsafeUnpin for MockGameAPI
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more