pub struct MockGameAPI { /* 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"));
assert!(api.logs().is_empty());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.
Sourcepub fn set_config(&mut self, key: &str, value: Value)
pub fn set_config(&mut self, key: &str, value: Value)
Set a config value for testing (dotted key path).
Sourcepub fn set_keybind(&mut self, action: &str, key: &str)
pub fn set_keybind(&mut self, action: &str, key: &str)
Set a keybinding for testing.
Sourcepub fn add_content_file(&mut self, path: &str, data: Vec<u8>)
pub fn add_content_file(&mut self, path: &str, data: Vec<u8>)
Add a content file by path.
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 read_content_file(&self, path: &str) -> Result<Vec<u8>, ServiceError>
fn read_content_file(&self, path: &str) -> Result<Vec<u8>, ServiceError>
Read any file from the active Content Pack by relative path. Read more
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 (native use only).
Source§fn call_service(
&self,
domain: &str,
method: &str,
args: &[Value],
) -> Result<Value, ServiceError>
fn call_service( &self, domain: &str, method: &str, args: &[Value], ) -> Result<Value, ServiceError>
Call a method on another plugin’s service (WASM-friendly). Read more
Source§fn has_service(&self, domain: &str) -> bool
fn has_service(&self, domain: &str) -> bool
Check whether a service domain is registered.
Source§fn get_component(
&self,
_entity: u64,
_type_name: &str,
) -> Result<Vec<u8>, ServiceError>
fn get_component( &self, _entity: u64, _type_name: &str, ) -> Result<Vec<u8>, ServiceError>
Read a component from an entity as raw serialised bytes.
Source§fn set_component(
&mut self,
_entity: u64,
_type_name: &str,
_data: &[u8],
) -> Result<(), ServiceError>
fn set_component( &mut self, _entity: u64, _type_name: &str, _data: &[u8], ) -> Result<(), ServiceError>
Buffer a component write. Applied atomically at the flush point.
Source§fn query_entities(
&self,
_component_name: &str,
) -> Result<Vec<u64>, ServiceError>
fn query_entities( &self, _component_name: &str, ) -> Result<Vec<u64>, ServiceError>
Find all entities that have a component with the given type name.
Source§fn spawn_entity(&mut self, _archetype: &str) -> Result<u64, ServiceError>
fn spawn_entity(&mut self, _archetype: &str) -> Result<u64, ServiceError>
Buffer a spawn request. Returns the new entity ID after flush.
Source§fn despawn_entity(&mut self, _entity: u64) -> Result<(), ServiceError>
fn despawn_entity(&mut self, _entity: u64) -> Result<(), ServiceError>
Buffer a despawn request.
Source§fn read_config(&self, key: &str) -> Result<Value, ServiceError>
fn read_config(&self, key: &str) -> Result<Value, ServiceError>
Read an engine config value by dotted key path. Read more
Source§fn read_keybinds(&self) -> Result<Value, ServiceError>
fn read_keybinds(&self) -> Result<Value, ServiceError>
Read the full keybinds table as a
Map<String, String>.Source§fn register_save_data(
&mut self,
domain: &str,
data: Vec<u8>,
) -> Result<(), ServiceError>
fn register_save_data( &mut self, domain: &str, data: Vec<u8>, ) -> Result<(), ServiceError>
Register a named blob of save data for the current save slot. Read more
Source§fn load_save_data(&self, domain: &str) -> Result<Option<Vec<u8>>, ServiceError>
fn load_save_data(&self, domain: &str) -> Result<Option<Vec<u8>>, ServiceError>
Load a previously saved data blob by domain name. Read more
Source§fn emit_event(&self, name: &str, data: &Value) -> Result<(), ServiceError>
fn emit_event(&self, name: &str, data: &Value) -> Result<(), ServiceError>
Emit a named event with attached data. Read more
Auto Trait Implementations§
impl !Freeze for MockGameAPI
impl !RefUnwindSafe for MockGameAPI
impl !Sync for MockGameAPI
impl !UnwindSafe for MockGameAPI
impl Forget for MockGameAPI
impl Move 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