Skip to content

Commit 4b3ade0

Browse files
committed
test: Tests are running!
1 parent 3a61256 commit 4b3ade0

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

crates/languages/bevy_mod_scripting_rhai/src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
use std::ops::Deref;
44

55
use bevy::{
6+
asset::Handle,
67
app::Plugin,
78
ecs::{entity::Entity, world::World},
89
};
910
use bevy_mod_scripting_core::{
10-
asset::Language,
11+
asset::{Language, ScriptAsset},
1112
bindings::{
1213
function::namespace::Namespace, globals::AppScriptGlobalsRegistry,
1314
script_value::ScriptValue, ThreadWorldContainer, WorldContainer,
@@ -17,7 +18,7 @@ use bevy_mod_scripting_core::{
1718
event::CallbackLabel,
1819
reflection_extensions::PartialReflectExt,
1920
runtime::RuntimeSettings,
20-
script::ScriptId,
21+
script::{DisplayProxy, ScriptId},
2122
IntoScriptPluginParams, ScriptingPlugin,
2223
};
2324
use bindings::{
@@ -180,7 +181,7 @@ impl Plugin for RhaiScriptingPlugin {
180181
// NEW helper function to load content into an existing context without clearing previous definitions.
181182
fn load_rhai_content_into_context(
182183
context: &mut RhaiScriptContext,
183-
script: &ScriptId,
184+
script: &Handle<ScriptAsset>,
184185
content: &[u8],
185186
initializers: &[ContextInitializer<RhaiScriptingPlugin>],
186187
pre_handling_initializers: &[ContextPreHandlingInitializer<RhaiScriptingPlugin>],
@@ -189,7 +190,7 @@ fn load_rhai_content_into_context(
189190
let runtime = runtime.read();
190191

191192
context.ast = runtime.compile(std::str::from_utf8(content)?)?;
192-
context.ast.set_source(script.to_string());
193+
context.ast.set_source(script.display().to_string());
193194

194195
initializers
195196
.iter()
@@ -205,7 +206,7 @@ fn load_rhai_content_into_context(
205206

206207
/// Load a rhai context from a script.
207208
pub fn rhai_context_load(
208-
script: &ScriptId,
209+
script: &Handle<ScriptAsset>,
209210
content: &[u8],
210211
initializers: &[ContextInitializer<RhaiScriptingPlugin>],
211212
pre_handling_initializers: &[ContextPreHandlingInitializer<RhaiScriptingPlugin>],
@@ -229,7 +230,7 @@ pub fn rhai_context_load(
229230

230231
/// Reload a rhai context from a script. New content is appended to the existing context.
231232
pub fn rhai_context_reload(
232-
script: &ScriptId,
233+
script: &Handle<ScriptAsset>,
233234
content: &[u8],
234235
context: &mut RhaiScriptContext,
235236
initializers: &[ContextInitializer<RhaiScriptingPlugin>],
@@ -251,7 +252,7 @@ pub fn rhai_context_reload(
251252
pub fn rhai_callback_handler(
252253
args: Vec<ScriptValue>,
253254
entity: Entity,
254-
script_id: &ScriptId,
255+
script_id: &Handle<ScriptAsset>,
255256
callback: &CallbackLabel,
256257
context: &mut RhaiScriptContext,
257258
pre_handling_initializers: &[ContextPreHandlingInitializer<RhaiScriptingPlugin>],
@@ -271,7 +272,7 @@ pub fn rhai_callback_handler(
271272
bevy::log::trace!(
272273
"Calling callback {} in script {} with args: {:?}",
273274
callback,
274-
script_id,
275+
script_id.display(),
275276
args
276277
);
277278
let runtime = runtime.read();
@@ -288,7 +289,7 @@ pub fn rhai_callback_handler(
288289
if let EvalAltResult::ErrorFunctionNotFound(_, _) = e.unwrap_inner() {
289290
bevy::log::trace!(
290291
"Script {} is not subscribed to callback {} with the provided arguments.",
291-
script_id,
292+
script_id.display(),
292293
callback
293294
);
294295
Ok(ScriptValue::Unit)

crates/testing_crates/script_integration_test_harness/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bevy::{
1717
world::{Command, FromWorld, Mut},
1818
},
1919
log::Level,
20-
prelude::{Entity, World},
20+
prelude::{Entity, World, Commands},
2121
reflect::{Reflect, TypeRegistry},
2222
utils::tracing,
2323
};
@@ -33,7 +33,7 @@ use bevy_mod_scripting_core::{
3333
event::{IntoCallbackLabel, ScriptErrorEvent},
3434
extractors::{HandlerContext, WithWorldGuard},
3535
handler::handle_script_errors,
36-
script::ScriptId,
36+
script::{ScriptComponent, ScriptId},
3737
BMSScriptingInfrastructurePlugin, IntoScriptPluginParams, ScriptingPlugin,
3838
};
3939
use bevy_mod_scripting_functions::ScriptFunctionsPlugin;
@@ -201,14 +201,15 @@ pub fn execute_rhai_integration_test(script_id: &str) -> Result<(), String> {
201201
execute_integration_test(plugin, |_, _| {}, script_id)
202202
}
203203

204-
pub fn execute_integration_test<
204+
pub fn execute_integration_test<'a,
205205
P: IntoScriptPluginParams + Plugin + AsMut<ScriptingPlugin<P>>,
206206
F: FnOnce(&mut World, &mut TypeRegistry),
207207
>(
208208
plugin: P,
209209
init: F,
210-
script_id: &str,
210+
script_id: impl Into<AssetPath<'a>>,
211211
) -> Result<(), String> {
212+
let script_id = script_id.into();
212213
// set "BEVY_ASSET_ROOT" to the global assets folder, i.e. CARGO_MANIFEST_DIR/../../../assets
213214
let mut manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
214215

@@ -234,28 +235,27 @@ pub fn execute_integration_test<
234235
OnTestLast => "on_test_last",
235236
);
236237

237-
let script_id = script_id.to_owned();
238-
let script_id: &'static str = Box::leak(script_id.into_boxed_str());
238+
let script_path = script_id.clone_owned();
239239

240-
let load_system = |server: Res<AssetServer>, mut handle: Local<Handle<ScriptAsset>>| {
241-
*handle = server.load(script_id.to_owned());
240+
// tests can opt in to this via "__RETURN"
241+
let expect_callback_response = script_id.path().to_str().map(|s| s.contains("__RETURN")).unwrap_or(false);
242+
let load_system = move |server: Res<AssetServer>, mut commands: Commands| {
243+
commands.spawn(ScriptComponent::new([server.load(script_path.clone())]));
242244
};
243245

244-
// tests can opt in to this via "__RETURN"
245-
let expect_callback_response = script_id.contains("__RETURN");
246246

247247
app.add_systems(Startup, load_system);
248248
app.add_systems(
249249
Update,
250-
TestCallbackBuilder::<P, OnTest>::build(script_id, expect_callback_response),
250+
TestCallbackBuilder::<P, OnTest>::build(&script_id, expect_callback_response),
251251
);
252252
app.add_systems(
253253
PostUpdate,
254-
TestCallbackBuilder::<P, OnTestPostUpdate>::build(script_id, expect_callback_response),
254+
TestCallbackBuilder::<P, OnTestPostUpdate>::build(&script_id, expect_callback_response),
255255
);
256256
app.add_systems(
257257
Last,
258-
TestCallbackBuilder::<P, OnTestLast>::build(script_id, expect_callback_response),
258+
TestCallbackBuilder::<P, OnTestLast>::build(&script_id, expect_callback_response),
259259
);
260260
app.add_systems(Update, dummy_update_system);
261261
app.add_systems(Startup, dummy_startup_system::<String>);

tests/script_tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use std::path::PathBuf;
55
use libtest_mimic::{Arguments, Failed, Trial};
66
use script_integration_test_harness::{
77
execute_lua_integration_test,
8-
#[cfg(feature = "rhai")]
9-
execute_rhai_integration_test,
108
};
119

1210
use test_utils::{discover_all_tests, Test, TestKind};
@@ -23,10 +21,12 @@ impl TestExecutor for Test {
2321
match self.kind {
2422
TestKind::Lua => execute_lua_integration_test(&self.path.to_string_lossy())?,
2523
TestKind::Rhai => {
26-
#[cfg(feature = "rhai")]
27-
execute_rhai_integration_test(&self.path.to_string_lossy())?
28-
#[cfg(not(feature = "rhai"))]
29-
panic!("no 'rhai' feature")
24+
if cfg!(feature = "rhai") {
25+
#[cfg(feature = "rhai")]
26+
script_integration_test_harness::execute_rhai_integration_test(&self.path.to_string_lossy())?
27+
} else {
28+
panic!("no 'rhai' feature")
29+
}
3030
},
3131

3232
}

0 commit comments

Comments
 (0)