Repository: bevyengine/bevy
File: crates/bevy_ecs/src/system/system_registry.rs (Rust)

Code before:
            static SYSTEM_ID: Cell<Option<SystemId>> = default();
        }

        fn system(mut commands: Commands) {
            let count = INVOCATIONS_LEFT.get() - 1;
            INVOCATIONS_LEFT.set(count);
            if count > 0 {
                commands.run_system(SYSTEM_ID.get().unwrap());
            }
        }

        let mut world = World::new();
        let id = world.register_system(system);
        SYSTEM_ID.set(Some(id));
        world.run_system(id).unwrap();

        assert_eq!(INVOCATIONS_LEFT.get(), 0);
    }

    #[test]
    fn world_run_system_recursive() {
        #[derive(Component)]
        struct RecursionMarker(u8);

        fn world_recursive(world: &mut World) {
            let sys_id = world.register_system_cached(world_recursive);
            if let Some(RecursionMarker(i)) = world.entity_mut(sys_id.entity()).take() {
                if i == 10 {
                    return;
                }
                world
                    .entity_mut(sys_id.entity())
                    .insert(RecursionMarker(i + 1));
            } else {
                world.entity_mut(sys_id.entity()).insert(RecursionMarker(0));
            }
            world.run_system_cached(world_recursive).unwrap();
        }

        let mut world = World::new();
        world.run_system_cached(world_recursive).unwrap();
    }

    #[test]
    fn run_system_exclusive_adapters() {
        let mut world = World::new();
        fn system(_: &mut World) {}
        world.run_system_cached(system).unwrap();
        world.run_system_cached(system.pipe(system)).unwrap();
        world.run_system_cached(system.map(|()| {})).unwrap();
    }

Review comment (performance):
Why did you change this test?  The first version you had verified that `run_system_cached` returned `RegisteredSystemError::Recursive` when called recursively.  Don't we still need to test that?  ... oh, I see, `run_system_cached` never returns `RegisteredSystemError::Recursive` because it always re-registers the system.  You'd need to use `register_system` and `run_system` to check that case.  

I'd be inclined to remove the current version of the test, since I don't think it's testing anything especially interesting.  It would be even better if we could make a test that verified `RegisteredSystemError::Recursive` is returned when appropriate, but we didn't have that before so I don't think it's critical.  

Code after:
        // The `S: IntoSystem` types are different, so they should be cached
        // as separate systems, even though the `<S as IntoSystem>::System`
        // types / values are the same (`ApplyDeferred`).
        assert_ne!(foo1, bar1);

        // But if the `S: IntoSystem` types are the same, they'll be cached
        // as the same system.
        assert_eq!(foo1, foo2);
        assert_eq!(bar1, bar2);
    }

    #[test]
    fn system_with_input_ref() {
        fn with_ref(InRef(input): InRef<u8>, mut counter: ResMut<Counter>) {
            counter.0 += *input;
        }

        let mut world = World::new();
        world.insert_resource(Counter(0));

        let id = world.register_system(with_ref);
        world.run_system_with(id, &2).unwrap();
        assert_eq!(*world.resource::<Counter>(), Counter(2));
    }

    #[test]
    fn system_with_input_mut() {
        #[derive(Event)]
        struct MyEvent {
            cancelled: bool,
        }

        fn post(InMut(event): InMut<MyEvent>, counter: ResMut<Counter>) {
            if counter.0 > 0 {
                event.cancelled = true;
            }
        }

        let mut world = World::new();
        world.insert_resource(Counter(0));
        let post_system = world.register_system(post);

        let mut event = MyEvent { cancelled: false };
        world.run_system_with(post_system, &mut event).unwrap();
        assert!(!event.cancelled);

        world.resource_mut::<Counter>().0 = 1;
        world.run_system_with(post_system, &mut event).unwrap();
        assert!(event.cancelled);
    }