Skip to content
77 changes: 77 additions & 0 deletions S1API.Tests/Entities/NPCAwarenessApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Reflection;
using S1API.Entities;
using S1API.Vehicles;

namespace S1API.Tests.Entities;

public sealed class NPCAwarenessApiTests
{
[Theory]
[InlineData(nameof(NPCNoiseEvent.Origin), "UnityEngine.Vector3")]
[InlineData(nameof(NPCNoiseEvent.Range), "System.Single")]
[InlineData(nameof(NPCNoiseEvent.Type), "S1API.Entities.NPCNoiseType")]
[InlineData(nameof(NPCNoiseEvent.Source), "UnityEngine.GameObject")]
[InlineData(nameof(NPCNoiseEvent.OriginInSewer), "System.Boolean")]
public void NoiseSnapshotPropertiesAreReadOnly(string propertyName, string propertyTypeName)
{
PropertyInfo? property = typeof(NPCNoiseEvent).GetProperty(propertyName);

Assert.NotNull(property);
Assert.Equal(propertyTypeName, property!.PropertyType.FullName);
Assert.False(property.CanWrite);
}

[Fact]
public void NoiseTypesRetainNativeValues()
{
Assert.Equal(0, (int)NPCNoiseType.Footstep);
Assert.Equal(1, (int)NPCNoiseType.Gunshot);
Assert.Equal(2, (int)NPCNoiseType.Explosion);
}

[Theory]
[InlineData(nameof(NPC.OnNoticedDrugDealing), typeof(Player))]
[InlineData(nameof(NPC.OnNoticedGeneralCrime), typeof(Player))]
[InlineData(nameof(NPC.OnNoticedPettyCrime), typeof(Player))]
[InlineData(nameof(NPC.OnNoticedPlayerViolatingCurfew), typeof(Player))]
[InlineData(nameof(NPC.OnNoticedSuspiciousPlayer), typeof(Player))]
[InlineData(nameof(NPC.OnGunshotHeard), typeof(NPCNoiseEvent))]
[InlineData(nameof(NPC.OnExplosionHeard), typeof(NPCNoiseEvent))]
[InlineData(nameof(NPC.OnHitByCar), typeof(LandVehicle))]
public void AwarenessEventsExposeManagedArguments(string eventName, Type argumentType)
{
EventInfo? eventInfo = typeof(NPC).GetEvent(eventName);

Assert.NotNull(eventInfo);
Assert.Equal(typeof(Action<>).MakeGenericType(argumentType), eventInfo!.EventHandlerType);
}
}

internal static class NPCAwarenessApiCompileFixture
{
internal static void SubscribeAndUnsubscribe(NPC npc)
{
Action<Player?> playerHandler = _ => { };
Action<NPCNoiseEvent?> noiseHandler = _ => { };
Action<LandVehicle?> vehicleHandler = _ => { };

npc.OnNoticedDrugDealing += playerHandler;
npc.OnNoticedGeneralCrime += playerHandler;
npc.OnNoticedPettyCrime += playerHandler;
npc.OnNoticedPlayerViolatingCurfew += playerHandler;
npc.OnNoticedSuspiciousPlayer += playerHandler;
npc.OnGunshotHeard += noiseHandler;
npc.OnExplosionHeard += noiseHandler;
npc.OnHitByCar += vehicleHandler;

npc.OnNoticedDrugDealing -= playerHandler;
npc.OnNoticedGeneralCrime -= playerHandler;
npc.OnNoticedPettyCrime -= playerHandler;
npc.OnNoticedPlayerViolatingCurfew -= playerHandler;
npc.OnNoticedSuspiciousPlayer -= playerHandler;
npc.OnGunshotHeard -= noiseHandler;
npc.OnExplosionHeard -= noiseHandler;
npc.OnHitByCar -= vehicleHandler;
}
}
33 changes: 33 additions & 0 deletions S1API.Tests/Entities/NPCVehicleLifecycleApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Reflection;
using S1API.Entities;
using S1API.Vehicles;

namespace S1API.Tests.Entities;

public sealed class NPCVehicleLifecycleApiTests
{
[Theory]
[InlineData(nameof(NPC.OnEnterVehicle))]
[InlineData(nameof(NPC.OnExitVehicle))]
public void VehicleLifecycleEventsExposeManagedVehicleArguments(string eventName)
{
EventInfo? eventInfo = typeof(NPC).GetEvent(eventName);

Assert.NotNull(eventInfo);
Assert.Equal(typeof(Action<LandVehicle>), eventInfo!.EventHandlerType);
}
}

internal static class NPCVehicleLifecycleApiCompileFixture
{
internal static void SubscribeAndUnsubscribe(NPC npc)
{
Action<LandVehicle> handler = _ => { };

npc.OnEnterVehicle += handler;
npc.OnExitVehicle += handler;
npc.OnEnterVehicle -= handler;
npc.OnExitVehicle -= handler;
}
}
24 changes: 24 additions & 0 deletions S1API.Tests/Internal/Utils/ManagedEventRegistrationTrackerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,28 @@ public void DuplicateAddsAreRemovedOneAtATimeInReverseRegistrationOrder()
Assert.Equal("first", first);
Assert.False(tracker.TryTakeLast(handler, out _));
}

[Fact]
public void TakeAllReturnsEveryRegistrationAndClearsTheTracker()
{
var tracker = new ManagedEventRegistrationTracker<string>();
Action firstHandler = () => { };
Action<int> secondHandler = _ => { };

tracker.Add(firstHandler, "first");
tracker.Add(firstHandler, "second");
tracker.Add(secondHandler, "third");

var registrations = tracker.TakeAll();

Assert.Equal(3, registrations.Count);
Assert.Contains(registrations, registration =>
registration.ManagedHandler.Equals(firstHandler) && registration.NativeHandler == "first");
Assert.Contains(registrations, registration =>
registration.ManagedHandler.Equals(firstHandler) && registration.NativeHandler == "second");
Assert.Contains(registrations, registration =>
registration.ManagedHandler.Equals(secondHandler) && registration.NativeHandler == "third");
Assert.False(tracker.TryTakeLast(firstHandler, out _));
Assert.False(tracker.TryTakeLast(secondHandler, out _));
}
}
Loading
Loading