Skip to content

Commit 4933b82

Browse files
committed
check
1 parent 0700320 commit 4933b82

File tree

1 file changed

+110
-109
lines changed

1 file changed

+110
-109
lines changed

src/signal.rs

Lines changed: 110 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<U: 'static> Signal for Box<dyn Signal<Item = U> + Send + Sync> {
6060
/// type can't be known at compile-time.
6161
pub trait SignalDynClone: Signal + DynClone {}
6262

63-
clone_trait_object!(<T> SignalDynClone <Item = T>);
63+
clone_trait_object!(<T> SignalDynClone<Item = T>);
6464

6565
impl<T: Signal + Clone + 'static> SignalDynClone for T {}
6666

@@ -1521,15 +1521,12 @@ pub trait SignalExt: Signal {
15211521
Throttle {
15221522
signal: self.map(
15231523
move |In(item): In<Self::Item>, time: Res<Time>, mut timer_option: Local<Option<Timer>>| {
1524-
bevy_log::info!("delta y: {:?}", time.delta());
15251524
match timer_option.as_mut() {
15261525
None => {
15271526
*timer_option = Some(Timer::new(duration, TimerMode::Once));
1528-
bevy_log::info!("delta f: {:?}", time.delta());
15291527
Some(item)
15301528
}
15311529
Some(timer) => {
1532-
bevy_log::info!("delta: {:?}", time.delta());
15331530
if timer.tick(time.delta()).finished() {
15341531
timer.reset();
15351532
Some(item)
@@ -2529,118 +2526,123 @@ mod tests {
25292526

25302527
#[test]
25312528
fn test_switch_signal_vec() {
2532-
// --- Setup ---
2533-
let mut app = create_test_app();
2534-
app.init_resource::<SignalVecOutput<i32>>();
2535-
2536-
// Two different data sources to switch between.
2537-
let list_a = MutableVecBuilder::from([10, 20]).spawn(app.world_mut());
2538-
let list_b = MutableVecBuilder::from([100, 200, 300]).spawn(app.world_mut());
2539-
2540-
// A resource to control which list is active.
2541-
#[derive(Resource, Clone, Copy, PartialEq, Debug)]
2542-
enum ListSelector {
2543-
A,
2544-
B,
2545-
}
2546-
2547-
app.insert_resource(ListSelector::A);
2548-
2549-
// The control signal reads the resource.
2550-
let control_signal = SignalBuilder::from_system(|_: In<()>, selector: Res<ListSelector>| *selector).dedupe();
2551-
2552-
// The signal chain under test.
2553-
let switched_signal = control_signal.dedupe().switch_signal_vec(
2554-
clone!((list_a, list_b) move |In(selector): In<ListSelector>| match selector {
2555-
ListSelector:: A => list_a.signal_vec().map_in(identity).boxed_clone(),
2556-
ListSelector:: B => list_b.signal_vec().boxed_clone(),
2557-
}),
2558-
);
2529+
{
2530+
// --- Setup ---
2531+
let mut app = create_test_app();
2532+
app.init_resource::<SignalVecOutput<i32>>();
2533+
2534+
// Two different data sources to switch between.
2535+
let list_a = MutableVecBuilder::from([10, 20]).spawn(app.world_mut());
2536+
let list_b = MutableVecBuilder::from([100, 200, 300]).spawn(app.world_mut());
2537+
2538+
// A resource to control which list is active.
2539+
#[derive(Resource, Clone, Copy, PartialEq, Debug)]
2540+
enum ListSelector {
2541+
A,
2542+
B,
2543+
}
25592544

2560-
// Register the final signal to a capture system.
2561-
let handle = switched_signal.for_each(capture_vec_output).register(app.world_mut());
2545+
app.insert_resource(ListSelector::A);
25622546

2563-
// --- Test 1: Initial State --- The first update should select List A and emit
2564-
// its initial state.
2565-
app.update();
2566-
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2567-
assert_eq!(diffs.len(), 1, "Initial update should produce one diff.");
2568-
assert_eq!(
2569-
diffs[0],
2570-
VecDiff::Replace { values: vec![10, 20] },
2571-
"Initial state should be a Replace with List A's contents."
2572-
);
2547+
// The control signal reads the resource.
2548+
let control_signal =
2549+
SignalBuilder::from_system(|_: In<()>, selector: Res<ListSelector>| *selector).dedupe();
25732550

2574-
// --- Test 2: Forwarding Diffs from Active List (A) --- A mutation to List A
2575-
// should be forwarded.
2576-
list_a.write(app.world_mut()).push(30);
2577-
app.update();
2578-
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2579-
assert_eq!(diffs.len(), 1, "Push to active list should produce one diff.");
2580-
assert_eq!(
2581-
diffs[0],
2582-
VecDiff::Push { value: 30 },
2583-
"Should forward Push diff from List A."
2584-
);
2551+
// The signal chain under test.
2552+
let switched_signal = control_signal.dedupe().switch_signal_vec(
2553+
clone!((list_a, list_b) move |In(selector): In<ListSelector>| match selector {
2554+
ListSelector:: A => list_a.signal_vec().map_in(identity).boxed_clone(),
2555+
ListSelector:: B => list_b.signal_vec().boxed_clone(),
2556+
}),
2557+
);
25852558

2586-
// --- Test 3: The Switch to List B --- Change the selector and update. This
2587-
// should trigger a switch.
2588-
*app.world_mut().resource_mut::<ListSelector>() = ListSelector::B;
2589-
app.update();
2590-
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2591-
assert_eq!(diffs.len(), 1, "Switching lists should produce one diff.");
2592-
assert_eq!(
2593-
diffs[0],
2594-
VecDiff::Replace {
2595-
values: vec![100, 200, 300]
2596-
},
2597-
"Switch should emit a Replace with List B's contents."
2598-
);
2559+
// Register the final signal to a capture system.
2560+
let handle = switched_signal.for_each(capture_vec_output).register(app.world_mut());
25992561

2600-
// --- Test 4: Ignoring Diffs from Old List (A) --- A mutation to the now-inactive
2601-
// List A should be ignored. This should not be seen
2602-
list_a.write(app.world_mut()).push(99);
2603-
app.update();
2604-
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2605-
assert!(diffs.is_empty(), "Should ignore diffs from the old, inactive list.");
2562+
// --- Test 1: Initial State --- The first update should select List A and emit
2563+
// its initial state.
2564+
app.update();
2565+
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2566+
assert_eq!(diffs.len(), 1, "Initial update should produce one diff.");
2567+
assert_eq!(
2568+
diffs[0],
2569+
VecDiff::Replace { values: vec![10, 20] },
2570+
"Initial state should be a Replace with List A's contents."
2571+
);
2572+
2573+
// --- Test 2: Forwarding Diffs from Active List (A) --- A mutation to List A
2574+
// should be forwarded.
2575+
list_a.write(app.world_mut()).push(30);
2576+
app.update();
2577+
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2578+
assert_eq!(diffs.len(), 1, "Push to active list should produce one diff.");
2579+
assert_eq!(
2580+
diffs[0],
2581+
VecDiff::Push { value: 30 },
2582+
"Should forward Push diff from List A."
2583+
);
2584+
2585+
// --- Test 3: The Switch to List B --- Change the selector and update. This
2586+
// should trigger a switch.
2587+
*app.world_mut().resource_mut::<ListSelector>() = ListSelector::B;
2588+
app.update();
2589+
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2590+
assert_eq!(diffs.len(), 1, "Switching lists should produce one diff.");
2591+
assert_eq!(
2592+
diffs[0],
2593+
VecDiff::Replace {
2594+
values: vec![100, 200, 300]
2595+
},
2596+
"Switch should emit a Replace with List B's contents."
2597+
);
26062598

2607-
// --- Test 5: Forwarding Diffs from New Active List (B) --- A mutation to List B
2608-
// should now be forwarded. remove 100
2609-
list_b.write(app.world_mut()).remove(0);
2610-
app.update();
2611-
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2612-
assert_eq!(diffs.len(), 1, "RemoveAt from new active list should produce one diff.");
2613-
assert_eq!(
2614-
diffs[0],
2615-
VecDiff::RemoveAt { index: 0 },
2616-
"Should forward RemoveAt diff from List B."
2617-
);
2599+
// --- Test 4: Ignoring Diffs from Old List (A) --- A mutation to the now-inactive
2600+
// List A should be ignored. This should not be seen
2601+
list_a.write(app.world_mut()).push(99);
2602+
app.update();
2603+
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2604+
assert!(diffs.is_empty(), "Should ignore diffs from the old, inactive list.");
26182605

2619-
// --- Test 6: Memoization (No-Op Switch) --- "Switching" to the already active
2620-
// list should produce no diffs.
2621-
*app.world_mut().resource_mut::<ListSelector>() = ListSelector::B;
2622-
app.update();
2623-
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2624-
assert!(
2625-
diffs.is_empty(),
2626-
"Re-selecting the same list should produce no diffs due to memoization."
2627-
);
2606+
// --- Test 5: Forwarding Diffs from New Active List (B) --- A mutation to List B
2607+
// should now be forwarded. remove 100
2608+
list_b.write(app.world_mut()).remove(0);
2609+
app.update();
2610+
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2611+
assert_eq!(diffs.len(), 1, "RemoveAt from new active list should produce one diff.");
2612+
assert_eq!(
2613+
diffs[0],
2614+
VecDiff::RemoveAt { index: 0 },
2615+
"Should forward RemoveAt diff from List B."
2616+
);
2617+
2618+
// --- Test 6: Memoization (No-Op Switch) --- "Switching" to the already active
2619+
// list should produce no diffs.
2620+
*app.world_mut().resource_mut::<ListSelector>() = ListSelector::B;
2621+
app.update();
2622+
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2623+
assert!(
2624+
diffs.is_empty(),
2625+
"Re-selecting the same list should produce no diffs due to memoization."
2626+
);
2627+
2628+
// --- Test 7: Switch Back to List A --- Switching back should give us the current
2629+
// state of List A.
2630+
*app.world_mut().resource_mut::<ListSelector>() = ListSelector::A;
2631+
app.update();
2632+
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2633+
assert_eq!(diffs.len(), 1, "Switching back to A should produce one diff.");
2634+
assert_eq!(
2635+
diffs[0],
2636+
VecDiff::Replace {
2637+
values: vec![10, 20, 30, 99],
2638+
// Note: includes the `push(30)` from earlier
2639+
},
2640+
"Switching back should Replace with the current state of List A."
2641+
);
2642+
handle.cleanup(app.world_mut());
2643+
}
26282644

2629-
// --- Test 7: Switch Back to List A --- Switching back should give us the current
2630-
// state of List A.
2631-
*app.world_mut().resource_mut::<ListSelector>() = ListSelector::A;
2632-
app.update();
2633-
let diffs = get_and_clear_vec_output::<i32>(app.world_mut());
2634-
assert_eq!(diffs.len(), 1, "Switching back to A should produce one diff.");
2635-
assert_eq!(
2636-
diffs[0],
2637-
VecDiff::Replace {
2638-
values: vec![10, 20, 30, 99],
2639-
// Note: includes the `push(30)` from earlier
2640-
},
2641-
"Switching back should Replace with the current state of List A."
2642-
);
2643-
handle.cleanup(app.world_mut());
2645+
crate::signal_vec::tests::cleanup(true);
26442646
}
26452647

26462648
#[test]
@@ -2653,7 +2655,6 @@ mod tests {
26532655
// A simple counter to generate a new value (1, 2, 3...) for each update.
26542656
let source_signal = SignalBuilder::from_system(|_: In<()>, mut counter: Local<i32>| {
26552657
*counter += 1;
2656-
bevy_log::info!("fuck");
26572658
*counter
26582659
});
26592660

0 commit comments

Comments
 (0)