|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Anvil.API; |
| 6 | +using Anvil.API.Events; |
| 7 | +using Anvil.Services; |
| 8 | +using Anvil.Tests.Resources; |
| 9 | +using NUnit.Framework; |
| 10 | + |
| 11 | +namespace Anvil.Tests.API.Events |
| 12 | +{ |
| 13 | + [TestFixture(Category = "API.Events.Native")] |
| 14 | + public sealed class OnEffectRemoveEventTest |
| 15 | + { |
| 16 | + private const string EffectTag = "test_tag"; |
| 17 | + |
| 18 | + [Inject] |
| 19 | + private static EventService EventService { get; set; } = null!; |
| 20 | + |
| 21 | + private NwCreature? creature; |
| 22 | + |
| 23 | + private readonly List<EffectEventData> beforeEvents = []; |
| 24 | + private readonly List<EffectEventData> afterEvents = []; |
| 25 | + |
| 26 | + [SetUp] |
| 27 | + public void Setup() |
| 28 | + { |
| 29 | + creature = NwCreature.Create(StandardResRef.Creature.nw_bandit001, NwModule.Instance.StartingLocation); |
| 30 | + } |
| 31 | + |
| 32 | + [Test] |
| 33 | + public async Task EffectRemoveAreEventsCalled() |
| 34 | + { |
| 35 | + Assert.That(creature, Is.Not.Null); |
| 36 | + |
| 37 | + creature!.OnEffectRemove += OnEffectRemoveBefore; |
| 38 | + EventService.Subscribe<OnEffectRemove, OnEffectRemove.Factory>(creature, OnEffectRemoveAfter, EventCallbackType.After); |
| 39 | + |
| 40 | + Effect effect = Effect.Blindness(); |
| 41 | + effect.Tag = EffectTag; |
| 42 | + |
| 43 | + creature.ApplyEffect(EffectDuration.Temporary, effect, TimeSpan.FromSeconds(2)); |
| 44 | + |
| 45 | + await NwTask.Delay(TimeSpan.FromSeconds(3)); |
| 46 | + |
| 47 | + Assert.Multiple(() => |
| 48 | + { |
| 49 | + Assert.That(beforeEvents, Has.Count.EqualTo(1)); |
| 50 | + Assert.That(afterEvents, Has.Count.EqualTo(1)); |
| 51 | + }); |
| 52 | + |
| 53 | + EffectEventData beforeEventData = beforeEvents.First(); |
| 54 | + EffectEventData afterEventData = afterEvents.First(); |
| 55 | + Assert.Multiple(() => |
| 56 | + { |
| 57 | + Assert.That(beforeEventData, Is.Not.Null); |
| 58 | + Assert.That(beforeEventData.EffectType, Is.EqualTo(EffectType.Blindness)); |
| 59 | + Assert.That(beforeEventData.Tag, Is.EqualTo(EffectTag)); |
| 60 | + Assert.That(beforeEventData.DurationType, Is.EqualTo(EffectDuration.Temporary)); |
| 61 | + |
| 62 | + Assert.That(afterEventData, Is.Not.Null); |
| 63 | + Assert.That(afterEventData.EffectType, Is.EqualTo(EffectType.Blindness)); |
| 64 | + Assert.That(afterEventData.Tag, Is.EqualTo(EffectTag)); |
| 65 | + Assert.That(afterEventData.DurationType, Is.EqualTo(EffectDuration.Temporary)); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + private void OnEffectRemoveBefore(OnEffectRemove eventData) |
| 70 | + { |
| 71 | + Effect effect = eventData.Effect; |
| 72 | + if (effect.Tag != EffectTag) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + beforeEvents.Add(new EffectEventData |
| 78 | + { |
| 79 | + EffectType = effect.EffectType, |
| 80 | + Tag = effect.Tag, |
| 81 | + DurationType = effect.DurationType, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + private void OnEffectRemoveAfter(OnEffectRemove eventData) |
| 86 | + { |
| 87 | + Effect effect = eventData.Effect; |
| 88 | + if (effect.Tag != EffectTag) |
| 89 | + { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + afterEvents.Add(new EffectEventData |
| 94 | + { |
| 95 | + EffectType = effect.EffectType, |
| 96 | + Tag = effect.Tag, |
| 97 | + DurationType = effect.DurationType, |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + [TearDown] |
| 102 | + public void TearDown() |
| 103 | + { |
| 104 | + if (creature != null) |
| 105 | + { |
| 106 | + creature.ClearEventSubscriptions(); |
| 107 | + creature.Destroy(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private sealed class EffectEventData |
| 112 | + { |
| 113 | + public required EffectType EffectType { get; init; } |
| 114 | + public required string? Tag { get; init; } |
| 115 | + public required EffectDuration DurationType { get; init; } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments