Skip to content

Commit

Permalink
re-write lua tests to use common integration test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
makspll committed Jan 9, 2025
1 parent 9c8a032 commit 25c3bc7
Show file tree
Hide file tree
Showing 30 changed files with 382 additions and 345 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"rust-analyzer.rustc.source": "discover",
"rust-analyzer.linkedProjects": [
"./crates/bevy_api_gen/Cargo.toml",
// "./crates/bevy_api_gen/Cargo.toml",
"Cargo.toml",
],
"rust-analyzer.check.invocationStrategy": "once",
Expand Down
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ bevy_mod_scripting_functions = { workspace = true }
[workspace.dependencies]
bevy = { version = "0.15.0", default-features = false }
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.9.0-alpha.2" }
bevy_mod_scripting_functions = { path = "crates/bevy_mod_scripting_functions", version = "0.9.0-alpha.2" }
test_utils = { path = "crates/test_utils" }
bevy_mod_scripting_functions = { path = "crates/bevy_mod_scripting_functions", version = "0.9.0-alpha.2", default-features = false }
mlua = { version = "0.10" }
rhai = { version = "1.20.1" }

# test utilities
script_integration_test_harness = { path = "crates/script_integration_test_harness" }
test_utils = { path = "crates/test_utils" }

[dev-dependencies]
bevy = { workspace = true, default-features = true }
clap = { version = "4.1", features = ["derive"] }
Expand All @@ -82,6 +85,7 @@ members = [
"crates/test_utils",
"crates/bevy_mod_scripting_functions",
"crates/xtask",
"crates/script_integration_test_harness",
]
resolver = "2"
exclude = ["crates/bevy_api_gen", "crates/macro_tests"]
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_mod_scripting_core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ pub type ContextPreHandlingInitializer<P> =

#[derive(Resource)]
pub struct ContextLoadingSettings<P: IntoScriptPluginParams> {
/// Defines the strategy used to load and reload contexts
pub loader: Option<ContextBuilder<P>>,
/// Defines the strategy used to assign contexts to scripts
pub assigner: Option<ContextAssigner<P>>,
/// Initializers run once after creating a context but before executing it for the first time
pub context_initializers: Vec<ContextInitializer<P>>,
/// Initializers run every time before executing or loading a script
pub context_pre_handling_initializers: Vec<ContextPreHandlingInitializer<P>>,
}

Expand Down
22 changes: 12 additions & 10 deletions crates/bevy_mod_scripting_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#![allow(clippy::arc_with_non_send_sync)]

use std::sync::atomic::AtomicBool;

use crate::event::ScriptErrorEvent;
use asset::{
AssetPathToLanguageMapper, Language, ScriptAsset, ScriptAssetLoader, ScriptAssetSettings,
Expand Down Expand Up @@ -49,13 +45,13 @@ pub trait IntoScriptPluginParams: 'static {
type C: Context;
type R: Runtime;

fn build_runtime() -> Self::R;

// fn supported_language() -> Language;
}

/// Bevy plugin enabling scripting within the bevy mod scripting framework
pub struct ScriptingPlugin<P: IntoScriptPluginParams> {
/// Callback for initiating the runtime
pub runtime_builder: fn() -> P::R,
/// Settings for the runtime
pub runtime_settings: Option<RuntimeSettings<P>>,
/// The handler used for executing callbacks in scripts
Expand All @@ -78,7 +74,6 @@ where
{
fn default() -> Self {
Self {
runtime_builder: P::R::default,
runtime_settings: Default::default(),
callback_handler: Default::default(),
context_builder: Default::default(),
Expand All @@ -94,7 +89,7 @@ impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
fn build(&self, app: &mut bevy::prelude::App) {
app.insert_resource(self.runtime_settings.as_ref().cloned().unwrap_or_default())
.insert_non_send_resource::<RuntimeContainer<P>>(RuntimeContainer {
runtime: (self.runtime_builder)(),
runtime: P::build_runtime(),
})
.init_non_send_resource::<ScriptContexts<P>>()
.insert_resource::<CallbackSettings<P>>(CallbackSettings {
Expand Down Expand Up @@ -149,12 +144,15 @@ impl<P: IntoScriptPluginParams> ScriptingPlugin<P> {

// One of registration of things that need to be done only once per app
fn once_per_app_init(app: &mut App) {
static INITIALIZED: AtomicBool = AtomicBool::new(false);
#[derive(Resource)]
struct BMSInitialized;

if INITIALIZED.fetch_or(true, std::sync::atomic::Ordering::Relaxed) {
if app.world().contains_resource::<BMSInitialized>() {
return;
}

app.insert_resource(BMSInitialized);

app.add_event::<ScriptErrorEvent>()
.add_event::<ScriptCallbackEvent>()
.init_resource::<AppReflectAllocator>()
Expand Down Expand Up @@ -337,6 +335,10 @@ mod test {
type C = C;
type R = R;
const LANGUAGE: Language = Language::Unknown;

fn build_runtime() -> Self::R {
R
}
}

app.add_plugins(AssetPlugin::default());
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_mod_scripting_core/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ mod test {
type R = TestRuntime;

const LANGUAGE: crate::asset::Language = crate::asset::Language::Unknown;

fn build_runtime() -> Self::R {
TestRuntime {
invocations: vec![],
}
}
}

struct TestRuntime {
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_mod_scripting_functions/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ::bevy::prelude::*;
use test_functions::register_test_functions;
#[cfg(feature = "bevy_bindings")]
pub mod bevy_bindings;
pub mod core;
Expand All @@ -17,5 +18,11 @@ impl Plugin for ScriptFunctionsPlugin {
fn build(&self, app: &mut App) {
register_bevy_bindings(app);
register_core_functions(app);
#[cfg(feature = "test_functions")]
register_test_functions(app);

// TODO: if bevy ever does this itself we should remove this
app.world_mut().register_component::<Parent>();
app.world_mut().register_component::<Children>();
}
}
29 changes: 4 additions & 25 deletions crates/bevy_mod_scripting_functions/src/test_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::sync::Arc;

use crate::NamespaceBuilder;
use bevy::{
app::App,
prelude::{Entity, World},
reflect::{Reflect, TypeRegistration},
};
use bevy_mod_scripting_core::{
bindings::{
access_map::ReflectAccessId,
function::{
script_function::{CallerContext, DynamicScriptFunctionMut},
CallScriptFunction,
Expand All @@ -19,7 +19,8 @@ use bevy_mod_scripting_core::{
};
use test_utils::test_data::EnumerateTestComponents;

pub fn register_test_functions(world: &mut World) {
pub fn register_test_functions(world: &mut App) {
let world = world.world_mut();
NamespaceBuilder::<World>::new_unregistered(world)
.register("_get_mock_type", |s: WorldCallbackAccess| {
let world = s.try_read().unwrap();
Expand Down Expand Up @@ -79,27 +80,5 @@ pub fn register_test_functions(world: &mut World) {
))
}
},
)
.register(
"_set_write_access",
|s: WorldCallbackAccess, ref_: ReflectReference| {
let world = s.try_read().unwrap();

world
.claim_write_access(ReflectAccessId::for_reference(ref_.base.base_id).unwrap());
},
)
.register(
"_set_read_access",
|s: WorldCallbackAccess, ref_: ReflectReference| {
let world = s.try_read().unwrap();

world.claim_read_access(ReflectAccessId::for_reference(ref_.base.base_id).unwrap());
},
)
.register("_claim_global_access", |s: WorldCallbackAccess| {
let world = s.try_read().unwrap();

world.claim_global_access();
});
);
}
11 changes: 8 additions & 3 deletions crates/languages/bevy_mod_scripting_lua/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,21 @@ path = "src/lib.rs"
[dependencies]
bevy = { workspace = true, default-features = false }
bevy_mod_scripting_core = { workspace = true, features = ["mlua_impls"] }
bevy_mod_scripting_functions = { workspace = true, features = [
], default-features = false }
bevy_mod_scripting_functions = { workspace = true, features = [] }
mlua = { workspace = true, features = ["vendored", "send", "macros"] }
parking_lot = "0.12.1"
uuid = "1.1"
smol_str = "0.2.2"
smallvec = "1.13"

[dev-dependencies]
test_utils = { workspace = true }
# test_utils = { workspace = true }
script_integration_test_harness = { workspace = true }
bevy_mod_scripting_functions = { workspace = true, features = [
"core_functions",
"bevy_bindings",
"test_functions",
] }
libtest-mimic = "0.8"
regex = "1.11"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl FromLua for LuaScriptValue {
}
ScriptValue::List(vec)
}
// Value::Function(function) => todo!(),
Value::Function(_) => todo!("Function FromLua is not implemented yet"),
// Value::Thread(thread) => todo!(),
Value::UserData(ud) => {
let ud = ud.borrow::<LuaReflectReference>().map_err(|e| {
Expand Down
3 changes: 2 additions & 1 deletion crates/languages/bevy_mod_scripting_lua/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ impl IntoScriptPluginParams for LuaScriptingPlugin {
type C = Lua;
type R = ();
const LANGUAGE: Language = Language::Lua;

fn build_runtime() -> Self::R {}
}

pub struct LuaScriptingPlugin {
Expand All @@ -37,7 +39,6 @@ impl Default for LuaScriptingPlugin {
LuaScriptingPlugin {
scripting_plugin: ScriptingPlugin {
context_assigner: None,
runtime_builder: Default::default,
runtime_settings: None,
callback_handler: Some(lua_handler),
context_builder: Some(ContextBuilder::<LuaScriptingPlugin> {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local component = world.get_type_by_name("CompWithDefault")
local entity = _get_entity_with_test_component("CompWithDefault")
local entity = world._get_entity_with_test_component("CompWithDefault")
local retrieved = world.get_component(entity, component)

assert(retrieved ~= nil, "Component was not found")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local component = world.get_type_by_name("TestComponent")
local entity = _get_entity_with_test_component("TestComponent")
local entity = world._get_entity_with_test_component("TestComponent")
local retrieved = world.get_component(entity, component)

assert(retrieved ~= nil, "Component was not found")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
local type = _get_mock_type()
local type = world._get_mock_type()
assert(world.get_resource(type) == nil, "Resource should not exist")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local entity = world.spawn()
local type = _get_mock_type()
local type = world._get_mock_type()

assert(world.has_component(entity, type) == false, "Entity should not have component")
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
local entity = _get_entity_with_test_component("CompWithDefault")
local entity = world._get_entity_with_test_component("CompWithDefault")
local component = world.get_type_by_name("CompWithDefault")
assert(world.has_component(entity, component) == true, "Component was not found")
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
local entity = _get_entity_with_test_component("TestComponent")
local entity = world._get_entity_with_test_component("TestComponent")
local component = world.get_type_by_name("TestComponent")
assert(world.has_component(entity, component) == true, "Component was not found")
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
local type = _get_mock_type()
local type = world._get_mock_type()
assert(world.has_resource(type) == false, "Resource should not exist")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local entity_a = world.spawn()
local entity_b = world.spawn()
local entity_c = world.spawn()
local entity_d = _get_entity_with_test_component("CompWithFromWorldAndComponentData")
local entity_d = world._get_entity_with_test_component("CompWithFromWorldAndComponentData")

local component_with = world.get_type_by_name("CompWithFromWorldAndComponentData")
local component_without = world.get_type_by_name("CompWithDefaultAndComponentData")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

local entity = _get_entity_with_test_component("CompWithDefault")
local entity = world._get_entity_with_test_component("CompWithDefault")
local component = world.get_type_by_name("CompWithDefault")

assert_throws(function ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

local entity = _get_entity_with_test_component("TestComponent")
local entity = world._get_entity_with_test_component("TestComponent")
local component = world.get_type_by_name("TestComponent")
world.remove_component(entity, component)
assert(world.has_component(entity, component) == false, "Component was not removed")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

local type = _get_mock_type()
local type = world._get_mock_type()

assert_throws(function ()
world.remove_resource(type)
Expand Down
Loading

0 comments on commit 25c3bc7

Please sign in to comment.