Skip to content

Commit 25c3bc7

Browse files
committed
re-write lua tests to use common integration test framework
1 parent 9c8a032 commit 25c3bc7

File tree

30 files changed

+382
-345
lines changed

30 files changed

+382
-345
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"rust-analyzer.rustc.source": "discover",
1212
"rust-analyzer.linkedProjects": [
13-
"./crates/bevy_api_gen/Cargo.toml",
13+
// "./crates/bevy_api_gen/Cargo.toml",
1414
"Cargo.toml",
1515
],
1616
"rust-analyzer.check.invocationStrategy": "once",

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ bevy_mod_scripting_functions = { workspace = true }
6060
[workspace.dependencies]
6161
bevy = { version = "0.15.0", default-features = false }
6262
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.9.0-alpha.2" }
63-
bevy_mod_scripting_functions = { path = "crates/bevy_mod_scripting_functions", version = "0.9.0-alpha.2" }
64-
test_utils = { path = "crates/test_utils" }
63+
bevy_mod_scripting_functions = { path = "crates/bevy_mod_scripting_functions", version = "0.9.0-alpha.2", default-features = false }
6564
mlua = { version = "0.10" }
6665
rhai = { version = "1.20.1" }
6766

67+
# test utilities
68+
script_integration_test_harness = { path = "crates/script_integration_test_harness" }
69+
test_utils = { path = "crates/test_utils" }
70+
6871
[dev-dependencies]
6972
bevy = { workspace = true, default-features = true }
7073
clap = { version = "4.1", features = ["derive"] }
@@ -82,6 +85,7 @@ members = [
8285
"crates/test_utils",
8386
"crates/bevy_mod_scripting_functions",
8487
"crates/xtask",
88+
"crates/script_integration_test_harness",
8589
]
8690
resolver = "2"
8791
exclude = ["crates/bevy_api_gen", "crates/macro_tests"]

crates/bevy_mod_scripting_core/src/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ pub type ContextPreHandlingInitializer<P> =
6666

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

crates/bevy_mod_scripting_core/src/lib.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#![allow(clippy::arc_with_non_send_sync)]
2-
3-
use std::sync::atomic::AtomicBool;
4-
51
use crate::event::ScriptErrorEvent;
62
use asset::{
73
AssetPathToLanguageMapper, Language, ScriptAsset, ScriptAssetLoader, ScriptAssetSettings,
@@ -49,13 +45,13 @@ pub trait IntoScriptPluginParams: 'static {
4945
type C: Context;
5046
type R: Runtime;
5147

48+
fn build_runtime() -> Self::R;
49+
5250
// fn supported_language() -> Language;
5351
}
5452

5553
/// Bevy plugin enabling scripting within the bevy mod scripting framework
5654
pub struct ScriptingPlugin<P: IntoScriptPluginParams> {
57-
/// Callback for initiating the runtime
58-
pub runtime_builder: fn() -> P::R,
5955
/// Settings for the runtime
6056
pub runtime_settings: Option<RuntimeSettings<P>>,
6157
/// The handler used for executing callbacks in scripts
@@ -78,7 +74,6 @@ where
7874
{
7975
fn default() -> Self {
8076
Self {
81-
runtime_builder: P::R::default,
8277
runtime_settings: Default::default(),
8378
callback_handler: Default::default(),
8479
context_builder: Default::default(),
@@ -94,7 +89,7 @@ impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
9489
fn build(&self, app: &mut bevy::prelude::App) {
9590
app.insert_resource(self.runtime_settings.as_ref().cloned().unwrap_or_default())
9691
.insert_non_send_resource::<RuntimeContainer<P>>(RuntimeContainer {
97-
runtime: (self.runtime_builder)(),
92+
runtime: P::build_runtime(),
9893
})
9994
.init_non_send_resource::<ScriptContexts<P>>()
10095
.insert_resource::<CallbackSettings<P>>(CallbackSettings {
@@ -149,12 +144,15 @@ impl<P: IntoScriptPluginParams> ScriptingPlugin<P> {
149144

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

154-
if INITIALIZED.fetch_or(true, std::sync::atomic::Ordering::Relaxed) {
150+
if app.world().contains_resource::<BMSInitialized>() {
155151
return;
156152
}
157153

154+
app.insert_resource(BMSInitialized);
155+
158156
app.add_event::<ScriptErrorEvent>()
159157
.add_event::<ScriptCallbackEvent>()
160158
.init_resource::<AppReflectAllocator>()
@@ -337,6 +335,10 @@ mod test {
337335
type C = C;
338336
type R = R;
339337
const LANGUAGE: Language = Language::Unknown;
338+
339+
fn build_runtime() -> Self::R {
340+
R
341+
}
340342
}
341343

342344
app.add_plugins(AssetPlugin::default());

crates/bevy_mod_scripting_core/src/systems.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ mod test {
335335
type R = TestRuntime;
336336

337337
const LANGUAGE: crate::asset::Language = crate::asset::Language::Unknown;
338+
339+
fn build_runtime() -> Self::R {
340+
TestRuntime {
341+
invocations: vec![],
342+
}
343+
}
338344
}
339345

340346
struct TestRuntime {

crates/bevy_mod_scripting_functions/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ::bevy::prelude::*;
2+
use test_functions::register_test_functions;
23
#[cfg(feature = "bevy_bindings")]
34
pub mod bevy_bindings;
45
pub mod core;
@@ -17,5 +18,11 @@ impl Plugin for ScriptFunctionsPlugin {
1718
fn build(&self, app: &mut App) {
1819
register_bevy_bindings(app);
1920
register_core_functions(app);
21+
#[cfg(feature = "test_functions")]
22+
register_test_functions(app);
23+
24+
// TODO: if bevy ever does this itself we should remove this
25+
app.world_mut().register_component::<Parent>();
26+
app.world_mut().register_component::<Children>();
2027
}
2128
}

crates/bevy_mod_scripting_functions/src/test_functions.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use std::sync::Arc;
22

33
use crate::NamespaceBuilder;
44
use bevy::{
5+
app::App,
56
prelude::{Entity, World},
67
reflect::{Reflect, TypeRegistration},
78
};
89
use bevy_mod_scripting_core::{
910
bindings::{
10-
access_map::ReflectAccessId,
1111
function::{
1212
script_function::{CallerContext, DynamicScriptFunctionMut},
1313
CallScriptFunction,
@@ -19,7 +19,8 @@ use bevy_mod_scripting_core::{
1919
};
2020
use test_utils::test_data::EnumerateTestComponents;
2121

22-
pub fn register_test_functions(world: &mut World) {
22+
pub fn register_test_functions(world: &mut App) {
23+
let world = world.world_mut();
2324
NamespaceBuilder::<World>::new_unregistered(world)
2425
.register("_get_mock_type", |s: WorldCallbackAccess| {
2526
let world = s.try_read().unwrap();
@@ -79,27 +80,5 @@ pub fn register_test_functions(world: &mut World) {
7980
))
8081
}
8182
},
82-
)
83-
.register(
84-
"_set_write_access",
85-
|s: WorldCallbackAccess, ref_: ReflectReference| {
86-
let world = s.try_read().unwrap();
87-
88-
world
89-
.claim_write_access(ReflectAccessId::for_reference(ref_.base.base_id).unwrap());
90-
},
91-
)
92-
.register(
93-
"_set_read_access",
94-
|s: WorldCallbackAccess, ref_: ReflectReference| {
95-
let world = s.try_read().unwrap();
96-
97-
world.claim_read_access(ReflectAccessId::for_reference(ref_.base.base_id).unwrap());
98-
},
99-
)
100-
.register("_claim_global_access", |s: WorldCallbackAccess| {
101-
let world = s.try_read().unwrap();
102-
103-
world.claim_global_access();
104-
});
83+
);
10584
}

crates/languages/bevy_mod_scripting_lua/Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,21 @@ path = "src/lib.rs"
3939
[dependencies]
4040
bevy = { workspace = true, default-features = false }
4141
bevy_mod_scripting_core = { workspace = true, features = ["mlua_impls"] }
42-
bevy_mod_scripting_functions = { workspace = true, features = [
43-
], default-features = false }
42+
bevy_mod_scripting_functions = { workspace = true, features = [] }
4443
mlua = { workspace = true, features = ["vendored", "send", "macros"] }
4544
parking_lot = "0.12.1"
4645
uuid = "1.1"
4746
smol_str = "0.2.2"
4847
smallvec = "1.13"
4948

5049
[dev-dependencies]
51-
test_utils = { workspace = true }
50+
# test_utils = { workspace = true }
51+
script_integration_test_harness = { workspace = true }
52+
bevy_mod_scripting_functions = { workspace = true, features = [
53+
"core_functions",
54+
"bevy_bindings",
55+
"test_functions",
56+
] }
5257
libtest-mimic = "0.8"
5358
regex = "1.11"
5459

crates/languages/bevy_mod_scripting_lua/src/bindings/script_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl FromLua for LuaScriptValue {
5757
}
5858
ScriptValue::List(vec)
5959
}
60-
// Value::Function(function) => todo!(),
60+
Value::Function(_) => todo!("Function FromLua is not implemented yet"),
6161
// Value::Thread(thread) => todo!(),
6262
Value::UserData(ud) => {
6363
let ud = ud.borrow::<LuaReflectReference>().map_err(|e| {

crates/languages/bevy_mod_scripting_lua/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ impl IntoScriptPluginParams for LuaScriptingPlugin {
2626
type C = Lua;
2727
type R = ();
2828
const LANGUAGE: Language = Language::Lua;
29+
30+
fn build_runtime() -> Self::R {}
2931
}
3032

3133
pub struct LuaScriptingPlugin {
@@ -37,7 +39,6 @@ impl Default for LuaScriptingPlugin {
3739
LuaScriptingPlugin {
3840
scripting_plugin: ScriptingPlugin {
3941
context_assigner: None,
40-
runtime_builder: Default::default,
4142
runtime_settings: None,
4243
callback_handler: Some(lua_handler),
4344
context_builder: Some(ContextBuilder::<LuaScriptingPlugin> {

crates/languages/bevy_mod_scripting_lua/tests/data/access/aliasing_global_access.lua

Lines changed: 0 additions & 11 deletions
This file was deleted.

crates/languages/bevy_mod_scripting_lua/tests/data/access/aliasing_write.lua

Lines changed: 0 additions & 11 deletions
This file was deleted.

crates/languages/bevy_mod_scripting_lua/tests/data/get_component/component_no_component_data.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local component = world.get_type_by_name("CompWithDefault")
2-
local entity = _get_entity_with_test_component("CompWithDefault")
2+
local entity = world._get_entity_with_test_component("CompWithDefault")
33
local retrieved = world.get_component(entity, component)
44

55
assert(retrieved ~= nil, "Component was not found")

crates/languages/bevy_mod_scripting_lua/tests/data/get_component/component_with_component_data.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local component = world.get_type_by_name("TestComponent")
2-
local entity = _get_entity_with_test_component("TestComponent")
2+
local entity = world._get_entity_with_test_component("TestComponent")
33
local retrieved = world.get_component(entity, component)
44

55
assert(retrieved ~= nil, "Component was not found")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
local type = _get_mock_type()
1+
local type = world._get_mock_type()
22
assert(world.get_resource(type) == nil, "Resource should not exist")
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
local entity = world.spawn()
2-
local type = _get_mock_type()
2+
local type = world._get_mock_type()
33

44
assert(world.has_component(entity, type) == false, "Entity should not have component")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
local entity = _get_entity_with_test_component("CompWithDefault")
1+
local entity = world._get_entity_with_test_component("CompWithDefault")
22
local component = world.get_type_by_name("CompWithDefault")
33
assert(world.has_component(entity, component) == true, "Component was not found")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
local entity = _get_entity_with_test_component("TestComponent")
1+
local entity = world._get_entity_with_test_component("TestComponent")
22
local component = world.get_type_by_name("TestComponent")
33
assert(world.has_component(entity, component) == true, "Component was not found")
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
local type = _get_mock_type()
1+
local type = world._get_mock_type()
22
assert(world.has_resource(type) == false, "Resource should not exist")

crates/languages/bevy_mod_scripting_lua/tests/data/query/query_returns_all_entities_matching.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local entity_a = world.spawn()
22
local entity_b = world.spawn()
33
local entity_c = world.spawn()
4-
local entity_d = _get_entity_with_test_component("CompWithFromWorldAndComponentData")
4+
local entity_d = world._get_entity_with_test_component("CompWithFromWorldAndComponentData")
55

66
local component_with = world.get_type_by_name("CompWithFromWorldAndComponentData")
77
local component_without = world.get_type_by_name("CompWithDefaultAndComponentData")

crates/languages/bevy_mod_scripting_lua/tests/data/remove_component/no_component_data_errors.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
local entity = _get_entity_with_test_component("CompWithDefault")
2+
local entity = world._get_entity_with_test_component("CompWithDefault")
33
local component = world.get_type_by_name("CompWithDefault")
44

55
assert_throws(function ()
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
local entity = _get_entity_with_test_component("TestComponent")
2+
local entity = world._get_entity_with_test_component("TestComponent")
33
local component = world.get_type_by_name("TestComponent")
44
world.remove_component(entity, component)
55
assert(world.has_component(entity, component) == false, "Component was not removed")

crates/languages/bevy_mod_scripting_lua/tests/data/remove_resource/no_resource_data_errors.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
local type = _get_mock_type()
2+
local type = world._get_mock_type()
33

44
assert_throws(function ()
55
world.remove_resource(type)

0 commit comments

Comments
 (0)