Skip to content

Commit 229ed49

Browse files
committed
Add a test in lib.rs to test that using the short name actually results in the correct processing overall.
1 parent 40f921c commit 229ed49

File tree

1 file changed

+77
-16
lines changed

1 file changed

+77
-16
lines changed

crates/bevy_asset/src/lib.rs

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,7 @@ mod tests {
23022302
unused,
23032303
reason = "We only use this for asset processor tests, which are only compiled with the `multi_threaded` feature."
23042304
)]
2305+
#[derive(TypePath)]
23052306
struct CoolTextSaver;
23062307

23072308
impl AssetSaver for CoolTextSaver {
@@ -2343,12 +2344,13 @@ mod tests {
23432344
unused,
23442345
reason = "We only use this for asset processor tests, which are only compiled with the `multi_threaded` feature."
23452346
)]
2347+
#[derive(TypePath)]
23462348
// Note: while we allow any Fn, since closures are unnameable types, creating a processor with a
23472349
// closure cannot be used (since we need to include the name of the transformer in the meta
23482350
// file).
23492351
struct RootAssetTransformer<M: MutateAsset<A>, A: Asset>(M, PhantomData<fn(&mut A)>);
23502352

2351-
trait MutateAsset<A: Asset>: Send + Sync + 'static {
2353+
trait MutateAsset<A: Asset>: TypePath + Send + Sync + 'static {
23522354
fn mutate(&self, asset: &mut A);
23532355
}
23542356

@@ -2424,6 +2426,19 @@ mod tests {
24242426
assert_eq!(processed_asset, source_asset);
24252427
}
24262428

2429+
// The asset processor currently requires multi_threaded.
2430+
#[cfg(feature = "multi_threaded")]
2431+
#[derive(TypePath)]
2432+
struct AddText;
2433+
2434+
// The asset processor currently requires multi_threaded.
2435+
#[cfg(feature = "multi_threaded")]
2436+
impl MutateAsset<CoolText> for AddText {
2437+
fn mutate(&self, text: &mut CoolText) {
2438+
text.text.push_str("_def");
2439+
}
2440+
}
2441+
24272442
// The asset processor currently requires multi_threaded.
24282443
#[cfg(feature = "multi_threaded")]
24292444
#[test]
@@ -2434,14 +2449,6 @@ mod tests {
24342449
processed_dir,
24352450
} = create_app_with_asset_processor();
24362451

2437-
struct AddText;
2438-
2439-
impl MutateAsset<CoolText> for AddText {
2440-
fn mutate(&self, text: &mut CoolText) {
2441-
text.text.push_str("_def");
2442-
}
2443-
}
2444-
24452452
type CoolTextProcessor = LoadTransformAndSave<
24462453
CoolTextLoader,
24472454
RootAssetTransformer<AddText, CoolText>,
@@ -2494,13 +2501,67 @@ mod tests {
24942501
processed_dir,
24952502
} = create_app_with_asset_processor();
24962503

2497-
struct AddText;
2504+
type CoolTextProcessor = LoadTransformAndSave<
2505+
CoolTextLoader,
2506+
RootAssetTransformer<AddText, CoolText>,
2507+
CoolTextSaver,
2508+
>;
2509+
app.register_asset_loader(CoolTextLoader)
2510+
.register_asset_processor(CoolTextProcessor::new(
2511+
RootAssetTransformer::new(AddText),
2512+
CoolTextSaver,
2513+
));
24982514

2499-
impl MutateAsset<CoolText> for AddText {
2500-
fn mutate(&self, text: &mut CoolText) {
2501-
text.text.push_str("_def");
2502-
}
2503-
}
2515+
let path = Path::new("abc.cool.ron");
2516+
source_dir.insert_asset_text(
2517+
path,
2518+
r#"(
2519+
text: "abc",
2520+
dependencies: [],
2521+
embedded_dependencies: [],
2522+
sub_texts: [],
2523+
)"#,
2524+
);
2525+
source_dir.insert_meta_text(path, r#"(
2526+
meta_format_version: "1.0",
2527+
asset: Process(
2528+
processor: "bevy_asset::processor::process::LoadTransformAndSave<bevy_asset::tests::CoolTextLoader, bevy_asset::tests::RootAssetTransformer<bevy_asset::tests::AddText, bevy_asset::tests::CoolText>, bevy_asset::tests::CoolTextSaver>",
2529+
settings: (
2530+
loader_settings: (),
2531+
transformer_settings: (),
2532+
saver_settings: (),
2533+
),
2534+
),
2535+
)"#);
2536+
2537+
// Start the app, which also starts the asset processor.
2538+
app.update();
2539+
2540+
// Wait for all processing to finish.
2541+
bevy_tasks::block_on(
2542+
app.world()
2543+
.resource::<AssetProcessor>()
2544+
.data()
2545+
.wait_until_finished(),
2546+
);
2547+
2548+
let processed_asset = processed_dir.get_asset(path).unwrap();
2549+
let processed_asset = str::from_utf8(processed_asset.value()).unwrap();
2550+
assert_eq!(
2551+
processed_asset,
2552+
r#"(text:"abc_def",dependencies:[],embedded_dependencies:[],sub_texts:[])"#
2553+
);
2554+
}
2555+
2556+
// The asset processor currently requires multi_threaded.
2557+
#[cfg(feature = "multi_threaded")]
2558+
#[test]
2559+
fn asset_processor_transforms_asset_with_short_path_meta() {
2560+
let AppWithProcessor {
2561+
mut app,
2562+
source_dir,
2563+
processed_dir,
2564+
} = create_app_with_asset_processor();
25042565

25052566
type CoolTextProcessor = LoadTransformAndSave<
25062567
CoolTextLoader,
@@ -2526,7 +2587,7 @@ mod tests {
25262587
source_dir.insert_meta_text(path, r#"(
25272588
meta_format_version: "1.0",
25282589
asset: Process(
2529-
processor: "bevy_asset::processor::process::LoadTransformAndSave<bevy_asset::tests::CoolTextLoader, bevy_asset::tests::RootAssetTransformer<bevy_asset::tests::asset_processor_transforms_asset_with_meta::AddText, bevy_asset::tests::CoolText>, bevy_asset::tests::CoolTextSaver>",
2590+
processor: "LoadTransformAndSave<CoolTextLoader, RootAssetTransformer<AddText, CoolText>, CoolTextSaver>",
25302591
settings: (
25312592
loader_settings: (),
25322593
transformer_settings: (),

0 commit comments

Comments
 (0)