Skip to content

Commit

Permalink
Add remove effect test. (#803)
Browse files Browse the repository at this point in the history
* Add remove effect test.

* Clear event subscriptions.
  • Loading branch information
jhett12321 authored Jan 31, 2025
1 parent d258f8d commit 1b4d3f3
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NWN.Anvil.Tests/NWN.Anvil.Tests.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Casync/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cenginestructure/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cenginestructures/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cevents_005Cnative/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cevents_005Cnative_005Ceffectevents/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cnui/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cnui_005Cbindings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cnui_005Clayout/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Anvil.API;
using Anvil.API.Events;
using Anvil.Services;
using Anvil.Tests.Resources;
using NUnit.Framework;

namespace Anvil.Tests.API.Events
{
[TestFixture(Category = "API.Events.Native")]
public sealed class OnEffectRemoveEventTest
{
private const string EffectTag = "test_tag";

[Inject]
private static EventService EventService { get; set; } = null!;

private NwCreature? creature;

private readonly List<EffectEventData> beforeEvents = [];
private readonly List<EffectEventData> afterEvents = [];

[SetUp]
public void Setup()
{
creature = NwCreature.Create(StandardResRef.Creature.nw_bandit001, NwModule.Instance.StartingLocation);
}

[Test]
public async Task EffectRemoveAreEventsCalled()
{
Assert.That(creature, Is.Not.Null);

creature!.OnEffectRemove += OnEffectRemoveBefore;
EventService.Subscribe<OnEffectRemove, OnEffectRemove.Factory>(creature, OnEffectRemoveAfter, EventCallbackType.After);

Effect effect = Effect.Blindness();
effect.Tag = EffectTag;

creature.ApplyEffect(EffectDuration.Temporary, effect, TimeSpan.FromSeconds(2));

await NwTask.Delay(TimeSpan.FromSeconds(3));

Assert.Multiple(() =>
{
Assert.That(beforeEvents, Has.Count.EqualTo(1));
Assert.That(afterEvents, Has.Count.EqualTo(1));
});

EffectEventData beforeEventData = beforeEvents.First();
EffectEventData afterEventData = afterEvents.First();
Assert.Multiple(() =>
{
Assert.That(beforeEventData, Is.Not.Null);
Assert.That(beforeEventData.EffectType, Is.EqualTo(EffectType.Blindness));
Assert.That(beforeEventData.Tag, Is.EqualTo(EffectTag));
Assert.That(beforeEventData.DurationType, Is.EqualTo(EffectDuration.Temporary));

Assert.That(afterEventData, Is.Not.Null);
Assert.That(afterEventData.EffectType, Is.EqualTo(EffectType.Blindness));
Assert.That(afterEventData.Tag, Is.EqualTo(EffectTag));
Assert.That(afterEventData.DurationType, Is.EqualTo(EffectDuration.Temporary));
});
}

private void OnEffectRemoveBefore(OnEffectRemove eventData)
{
Effect effect = eventData.Effect;
if (effect.Tag != EffectTag)
{
return;
}

beforeEvents.Add(new EffectEventData
{
EffectType = effect.EffectType,
Tag = effect.Tag,
DurationType = effect.DurationType,
});
}

private void OnEffectRemoveAfter(OnEffectRemove eventData)
{
Effect effect = eventData.Effect;
if (effect.Tag != EffectTag)
{
return;
}

afterEvents.Add(new EffectEventData
{
EffectType = effect.EffectType,
Tag = effect.Tag,
DurationType = effect.DurationType,
});
}

[TearDown]
public void TearDown()
{
if (creature != null)
{
creature.ClearEventSubscriptions();
creature.Destroy();
}
}

private sealed class EffectEventData
{
public required EffectType EffectType { get; init; }
public required string? Tag { get; init; }
public required EffectDuration DurationType { get; init; }
}
}
}

0 comments on commit 1b4d3f3

Please sign in to comment.