@@ -146,14 +146,11 @@ impl<T: GetScriptHandle + Event + Clone> LoadedWithHandles<'_, '_, T> {
146
146
let strong = StrongScriptHandle :: from_assets ( handle, & mut self . assets ) ;
147
147
if let Some ( strong) = strong {
148
148
self . loaded_with_handles . push_front ( ( e. clone ( ) , strong) ) ;
149
- true
150
- } else {
151
- false
152
149
}
150
+ false
153
151
}
154
152
Some ( LoadState :: Loading ) => true ,
155
- Some ( _) => false ,
156
- None => false ,
153
+ _ => false ,
157
154
}
158
155
} ) ;
159
156
@@ -382,3 +379,59 @@ impl<P: IntoScriptPluginParams> Command for DetachScript<P> {
382
379
RunProcessingPipelineOnce :: < P > :: new ( ) . apply ( world)
383
380
}
384
381
}
382
+
383
+ #[ cfg( test) ]
384
+ mod test {
385
+ use bevy_asset:: { AssetApp , AssetId , AssetPlugin } ;
386
+ use bevy_ecs:: world:: FromWorld ;
387
+ use bevy_mod_scripting_asset:: Language ;
388
+
389
+ use super :: * ;
390
+ #[ test]
391
+ fn test_system_params ( ) {
392
+ let mut app = App :: default ( ) ;
393
+ app. add_event :: < ScriptAttachedEvent > ( ) ;
394
+ app. add_plugins ( AssetPlugin :: default ( ) ) ;
395
+ app. init_asset :: < ScriptAsset > ( ) ;
396
+ app. finish ( ) ;
397
+
398
+ let world = app. world_mut ( ) ;
399
+ let mut system_state =
400
+ SystemState :: < LoadedWithHandles < ScriptAttachedEvent > > :: from_world ( world) ;
401
+ // start empty
402
+ {
403
+ let mut state = system_state. get_mut ( world) ;
404
+ let loaded = state. get_loaded ( ) . collect :: < Vec < _ > > ( ) ;
405
+ assert ! ( loaded. is_empty( ) )
406
+ }
407
+
408
+ // send event with loading asset
409
+ // let assets = world.get_resource_mut::<Assets<ScriptAsset>>().unwrap();
410
+ let asset_server = world. get_resource_mut :: < AssetServer > ( ) . unwrap ( ) ;
411
+ let asset = ScriptAsset {
412
+ content : "asd" . to_string ( ) . into_boxed_str ( ) . into_boxed_bytes ( ) ,
413
+ language : Language :: Lua ,
414
+ } ;
415
+ let handle = asset_server. add ( asset) ;
416
+ let handle_invalid = Handle :: Weak ( AssetId :: invalid ( ) ) ;
417
+ world. send_event ( ScriptAttachedEvent ( ScriptAttachment :: StaticScript ( handle) ) ) ;
418
+ world. send_event ( ScriptAttachedEvent ( ScriptAttachment :: StaticScript (
419
+ handle_invalid,
420
+ ) ) ) ;
421
+
422
+ // expect one loading, one invalid
423
+ {
424
+ let mut state = system_state. get_mut ( world) ;
425
+ let loaded = state. get_loaded ( ) . collect :: < Vec < _ > > ( ) ;
426
+ assert ! ( loaded. is_empty( ) ) ;
427
+ assert_eq ! ( state. loading. len( ) , 1 ) ;
428
+ }
429
+
430
+ // now on next call the old ones don't persist
431
+ {
432
+ let mut state = system_state. get_mut ( world) ;
433
+ let loaded = state. get_loaded ( ) . collect :: < Vec < _ > > ( ) ;
434
+ assert ! ( loaded. is_empty( ) )
435
+ }
436
+ }
437
+ }
0 commit comments