Skip to main content

MockGameAPI

Struct MockGameAPI 

Source
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

Source

pub fn new() -> Self

Create an empty mock API.

Source

pub fn with_registry_file(domain: &str, filename: &str, data: &[u8]) -> Self

Create a mock API pre-populated with a single registry file.

Source

pub fn set_config(&mut self, key: &str, value: Value)

Set a config value for testing (dotted key path).

Source

pub fn set_keybind(&mut self, action: &str, key: &str)

Set a keybinding for testing.

Source

pub fn add_content_file(&mut self, path: &str, data: Vec<u8>)

Add a content file by path.

Source

pub fn logs(&self) -> Vec<(LogLevel, String)>

Get all accumulated log messages (for assertions in tests).

Source

pub fn has_log(&self, level: LogLevel, msg_substring: &str) -> bool

Check whether a specific log message exists.

Source

pub fn service_domains(&self) -> Vec<&str>

Get all registered service domains.

Source

pub fn events(&self) -> Vec<(String, Value)>

Get all emitted events (for test assertions).

Source

pub fn has_event(&self, name: &str) -> bool

Check whether a specific event was emitted.

Trait Implementations§

Source§

impl Default for MockGameAPI

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl GameAPI for MockGameAPI

Source§

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>

Get raw bytes of a specific file within a domain.
Source§

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>

Register a service under the given domain name.
Source§

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>

Call a method on another plugin’s service (WASM-friendly). Read more
Source§

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>

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>

Buffer a component write. Applied atomically at the flush point.
Source§

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>

Buffer a spawn request. Returns the new entity ID after flush.
Source§

fn despawn_entity(&mut self, _entity: u64) -> Result<(), ServiceError>

Buffer a despawn request.
Source§

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>

Read the full keybinds table as a Map<String, String>.
Source§

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>

Load a previously saved data blob by domain name. Read more
Source§

fn emit_event(&self, name: &str, data: &Value) -> Result<(), ServiceError>

Emit a named event with attached data. Read more
Source§

fn log(&self, level: LogLevel, message: &str)

Log a message at the given level.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.