Add generic event listener for integration tests (#40367)

* Add generic event listener for integration tests

* cleanup

* assert that the entity has the component

* comments & new overload
This commit is contained in:
Leon Friedrich
2025-10-10 02:03:44 +13:00
committed by GitHub
parent fea8ac4522
commit dac2c5212a
4 changed files with 205 additions and 23 deletions

View File

@@ -1,10 +1,8 @@
#nullable enable
using System.Collections.Generic;
using Content.IntegrationTests.Tests.Interaction;
using Content.IntegrationTests.Tests.Helpers;
using Content.Shared.Movement.Components;
using Content.Shared.Slippery;
using Content.Shared.Stunnable;
using Robust.Shared.GameObjects;
using Robust.Shared.Input;
using Robust.Shared.Maths;
@@ -12,44 +10,32 @@ namespace Content.IntegrationTests.Tests.Movement;
public sealed class SlippingTest : MovementTest
{
public sealed class SlipTestSystem : EntitySystem
{
public HashSet<EntityUid> Slipped = new();
public override void Initialize()
{
SubscribeLocalEvent<SlipperyComponent, SlipEvent>(OnSlip);
}
private void OnSlip(EntityUid uid, SlipperyComponent component, ref SlipEvent args)
{
Slipped.Add(args.Slipped);
}
}
public sealed class SlipTestSystem : TestListenerSystem<SlipEvent>;
[Test]
public async Task BananaSlipTest()
{
var sys = SEntMan.System<SlipTestSystem>();
await SpawnTarget("TrashBananaPeel");
var modifier = Comp<MovementSpeedModifierComponent>(Player).SprintSpeedModifier;
Assert.That(modifier, Is.EqualTo(1), "Player is not moving at full speed.");
// Player is to the left of the banana peel and has not slipped.
// Player is to the left of the banana peel.
Assert.That(Delta(), Is.GreaterThan(0.5f));
Assert.That(sys.Slipped, Does.Not.Contain(SEntMan.GetEntity(Player)));
// Walking over the banana slowly does not trigger a slip.
await SetKey(EngineKeyFunctions.Walk, BoundKeyState.Down);
await Move(DirectionFlag.East, 1f);
await AssertFiresEvent<SlipEvent>(async () => await Move(DirectionFlag.East, 1f), count: 0);
Assert.That(Delta(), Is.LessThan(0.5f));
Assert.That(sys.Slipped, Does.Not.Contain(SEntMan.GetEntity(Player)));
AssertComp<KnockedDownComponent>(false, Player);
// Moving at normal speeds does trigger a slip.
await SetKey(EngineKeyFunctions.Walk, BoundKeyState.Up);
await Move(DirectionFlag.West, 1f);
Assert.That(sys.Slipped, Does.Contain(SEntMan.GetEntity(Player)));
await AssertFiresEvent<SlipEvent>(async () => await Move(DirectionFlag.West, 1f));
// And the person that slipped was the player
AssertEvent<SlipEvent>(predicate: @event => @event.Slipped == SPlayer);
AssertComp<KnockedDownComponent>(true, Player);
}
}