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"));

// Check emitted logs
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.

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);
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.

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 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.
Source§

fn has_service(&self, domain: &str) -> bool

Check whether a service domain is registered.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.