Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions S1API.Tests/Entities/NPCAwarenessApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Reflection;
using S1API.Entities;
using S1API.Vehicles;
using UnityEngine;

namespace S1API.Tests.Entities;

public sealed class NPCAwarenessApiTests
{
[Fact]
public void NoiseSnapshotCapturesReadOnlyManagedValues()
{
var origin = new Vector3(1f, 2f, 3f);
var snapshot = new NPCNoiseEvent(
origin,
24f,
NPCNoiseType.Gunshot,
source: null,
originInSewer: true);

Assert.Equal(origin, snapshot.Origin);
Assert.Equal(24f, snapshot.Range);
Assert.Equal(NPCNoiseType.Gunshot, snapshot.Type);
Assert.Null(snapshot.Source);
Assert.True(snapshot.OriginInSewer);
Assert.False(typeof(NPCNoiseEvent).GetProperty(nameof(NPCNoiseEvent.Origin))!.CanWrite);
Assert.False(typeof(NPCNoiseEvent).GetProperty(nameof(NPCNoiseEvent.Range))!.CanWrite);
Assert.False(typeof(NPCNoiseEvent).GetProperty(nameof(NPCNoiseEvent.Type))!.CanWrite);
Assert.False(typeof(NPCNoiseEvent).GetProperty(nameof(NPCNoiseEvent.Source))!.CanWrite);
Assert.False(typeof(NPCNoiseEvent).GetProperty(nameof(NPCNoiseEvent.OriginInSewer))!.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;
}
}
Loading
Loading