Files
linuxdistro-roguelite/Assets/Tests/EditMode/SerializedEffectTargetingTests.cs
T

163 lines
6.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using KernelPanic.Combat;
using KernelPanic.Data;
using NUnit.Framework;
using UnityEngine;
public class SerializedEffectTargetingTests
{
private const BindingFlags Private = BindingFlags.Instance | BindingFlags.NonPublic;
private readonly List<UnityEngine.Object> temporary = new();
[TearDown]
public void TearDown()
{
foreach (UnityEngine.Object item in temporary) UnityEngine.Object.DestroyImmediate(item);
temporary.Clear();
}
[TestCase("lang_c_malloc", CardTargeting.None, false, false)]
[TestCase("lang_c_malloc", CardTargeting.SingleEnemy, true, false)]
[TestCase("lang_c_malloc", CardTargeting.AllEnemies, false, true)]
[TestCase("external_card", CardTargeting.None, false, false)]
[TestCase("external_card", CardTargeting.SingleEnemy, true, false)]
[TestCase("external_card", CardTargeting.AllEnemies, false, true)]
public void SerializedTargetingOverridesCatalogForKnownAndUnknownCards(
string id, CardTargeting targeting, bool single, bool all)
{
var effect = Effect(targeting);
var definition = Card(id, null, effect);
Assert.That(CardEffectFactory.RequiresSingleTarget(definition), Is.EqualTo(single));
Assert.That(CardEffectFactory.TargetsAllEnemies(definition), Is.EqualTo(all));
Assert.That(effect.FactoryCalls, Is.Zero, "Targeting only reads authoring metadata.");
}
[Test]
public void TargetingAndRulesNeverInstantiateRuntimeEffects()
{
var effect = Effect(CardTargeting.AllEnemies);
effect.ThrowOnCreate = true;
var definition = Card("lang_c_malloc", effect);
typeof(CardDefinition).GetField("description", Private).SetValue(definition, "custom all-enemy effect");
Assert.That(CardEffectFactory.RequiresSingleTarget(definition), Is.False);
Assert.That(CardEffectFactory.TargetsAllEnemies(definition), Is.True);
Assert.That(CardEffectFactory.GetRulesText(new CardInstance(definition)), Is.EqualTo("custom all-enemy effect"));
Assert.That(effect.FactoryCalls, Is.Zero);
}
[TestCase(false)]
[TestCase(true)]
public void MixedTargetingPreservesSingleEnemySelectionRegardlessOfEffectOrder(bool reverse)
{
var effects = new[] { Effect(CardTargeting.AllEnemies), Effect(CardTargeting.None), Effect(CardTargeting.SingleEnemy) };
if (reverse) Array.Reverse(effects);
var definition = Card("lang_py_for_loop", effects);
Assert.That(CardEffectFactory.RequiresSingleTarget(definition), Is.True);
Assert.That(CardEffectFactory.TargetsAllEnemies(definition), Is.False,
"The card must capture the selected enemy; all-enemy effects resolve their own enemy list.");
}
[Test]
public void UntargetedEffectsDoNotHideAllEnemyTargeting()
{
var definition = Card("lang_c_malloc", Effect(CardTargeting.None), null, Effect(CardTargeting.AllEnemies));
Assert.That(CardEffectFactory.RequiresSingleTarget(definition), Is.False);
Assert.That(CardEffectFactory.TargetsAllEnemies(definition), Is.True);
}
[TestCase("lang_c_malloc", true, false)]
[TestCase("lang_py_for_loop", false, false)]
[TestCase("lang_rs_borrow", false, false)]
[TestCase("external_card", false, false)]
public void EmptyAndNullOnlyOverridesUseCatalogTargeting(string id, bool single, bool all)
{
var definition = Card(id);
Assert.That(CardEffectFactory.RequiresSingleTarget(definition), Is.EqualTo(single));
Assert.That(CardEffectFactory.TargetsAllEnemies(definition), Is.EqualTo(all));
typeof(CardDefinition).GetField("effects", Private).SetValue(definition, new List<CardEffectDefinition> { null });
Assert.That(CardEffectFactory.RequiresSingleTarget(definition), Is.EqualTo(single));
Assert.That(CardEffectFactory.TargetsAllEnemies(definition), Is.EqualTo(all));
if (id == "external_card")
Assert.That(CardEffectFactory.CreateEffects(definition)[0], Is.TypeOf<NoOpCardEffect>());
else
Assert.That(CardEffectFactory.CreateEffects(definition)[0], Is.Not.TypeOf<NoOpCardEffect>());
}
[Test]
public void NullRuntimeResultKeepsOverrideTargetingAndDoesNotExecuteCatalogEffect()
{
var effect = Effect(CardTargeting.None);
effect.ReturnNull = true;
var definition = Card("lang_c_malloc", effect);
var runtime = CardEffectFactory.CreateEffects(definition);
Assert.That(runtime.Count, Is.EqualTo(1));
Assert.That(runtime[0], Is.TypeOf<NoOpCardEffect>());
Assert.That(CardEffectFactory.RequiresSingleTarget(definition), Is.False);
Assert.That(CardEffectFactory.TargetsAllEnemies(definition), Is.False);
Assert.That(effect.FactoryCalls, Is.EqualTo(1));
}
[Test]
public void NullRuntimeResultRejectsEntireSerializedOverride()
{
var empty = Effect(CardTargeting.None);
empty.ReturnNull = true;
var valid = Effect(CardTargeting.None);
var definition = Card("lang_c_malloc", empty, null, valid);
var runtime = CardEffectFactory.CreateEffects(definition);
Assert.That(runtime.Count, Is.EqualTo(1));
Assert.That(runtime[0], Is.TypeOf<NoOpCardEffect>());
Assert.That(empty.FactoryCalls, Is.EqualTo(1));
Assert.That(valid.FactoryCalls, Is.Zero, "Do not instantiate more effects after an invalid override is found.");
}
[Test]
public void NullDefinitionRequiresNoTarget()
{
Assert.That(CardEffectFactory.RequiresSingleTarget(null), Is.False);
Assert.That(CardEffectFactory.TargetsAllEnemies(null), Is.False);
}
private CardDefinition Card(string id, params CardEffectDefinition[] effects)
{
var definition = ScriptableObject.CreateInstance<CardDefinition>();
temporary.Add(definition);
typeof(CardDefinition).GetField("id", Private).SetValue(definition, id);
typeof(CardDefinition).GetField("effects", Private).SetValue(definition, new List<CardEffectDefinition>(effects));
return definition;
}
private SerializedTargetingTestEffect Effect(CardTargeting targeting)
{
var effect = ScriptableObject.CreateInstance<SerializedTargetingTestEffect>();
temporary.Add(effect);
typeof(CardEffectDefinition).GetField("targeting", Private).SetValue(effect, targeting);
return effect;
}
}
public sealed class SerializedTargetingTestEffect : CardEffectDefinition
{
public int FactoryCalls;
public bool ReturnNull;
public bool ThrowOnCreate;
public override ICardEffect CreateRuntimeEffect()
{
FactoryCalls++;
if (ThrowOnCreate) throw new InvalidOperationException("Targeting must not instantiate runtime effects.");
return ReturnNull ? null : new ShieldEffect(37);
}
}