125 lines
5.1 KiB
C#
125 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using KernelPanic.Combat;
|
|
using KernelPanic.Data;
|
|
using NUnit.Framework;
|
|
using UnityEngine;
|
|
using UnityEngine.TestTools;
|
|
|
|
public class SerializedEffectValidationTests
|
|
{
|
|
private const BindingFlags Private = BindingFlags.Instance | BindingFlags.NonPublic;
|
|
private readonly List<UnityEngine.Object> temporary = new();
|
|
private readonly List<string> executed = new();
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
foreach (UnityEngine.Object item in temporary) UnityEngine.Object.DestroyImmediate(item);
|
|
temporary.Clear();
|
|
executed.Clear();
|
|
}
|
|
|
|
[TestCase(false)]
|
|
[TestCase(true)]
|
|
public void InvalidOverrideExecutesNoPartialEffectsAndReportsAuthoredLocation(bool invalidFirst)
|
|
{
|
|
var first = Effect("first effect");
|
|
var invalid = Effect("broken barrier");
|
|
invalid.RuntimeEffect = null;
|
|
var last = Effect("last effect");
|
|
var definition = Card("lang_c_malloc", invalidFirst
|
|
? new CardEffectDefinition[] { invalid, null, first, last }
|
|
: new CardEffectDefinition[] { first, null, invalid, last });
|
|
var owner = new GameObject(nameof(SerializedEffectValidationTests));
|
|
temporary.Add(owner);
|
|
var combat = owner.AddComponent<CombatManager>();
|
|
var messages = new List<string>();
|
|
combat.CombatLog += messages.Add;
|
|
var context = new CombatContext(new CardInstance(definition), null, Array.Empty<CombatantState>(),
|
|
combat, combat.DamagePipeline, new StatusEffectController(), combat.DeckController,
|
|
null, combat.Enemies);
|
|
|
|
string expectedDiagnostic = $"Card '{definition.Id}': serialized effect {(invalidFirst ? 1 : 3)} '{invalid.name}' "
|
|
+ $"({nameof(SerializedValidationTestEffect)}) returned no runtime effect; card effects were not executed.";
|
|
LogAssert.Expect(LogType.Warning, expectedDiagnostic);
|
|
var runtime = CardEffectFactory.CreateEffects(definition);
|
|
foreach (ICardEffect effect in runtime) effect.Execute(context);
|
|
foreach (ICardEffect effect in runtime) effect.Execute(context);
|
|
|
|
Assert.That(runtime.Count, Is.EqualTo(1));
|
|
Assert.That(runtime[0], Is.TypeOf<NoOpCardEffect>(),
|
|
"A broken override cannot fall back to this card's catalog damage effect.");
|
|
Assert.That(executed, Is.Empty, "Effects created before the invalid slot must also be discarded.");
|
|
Assert.That(last.FactoryCalls, Is.Zero, "Validation stops at the first invalid authored effect.");
|
|
Assert.That(messages.Count, Is.EqualTo(1), "Repeated plays should not flood the combat log.");
|
|
Assert.That(messages[0], Does.Contain(definition.Id));
|
|
Assert.That(messages[0], Does.Contain("serialized effect " + (invalidFirst ? 1 : 3)),
|
|
"The diagnostic uses the original 1-based slot, including unassigned entries.");
|
|
Assert.That(messages[0], Does.Contain(invalid.name));
|
|
Assert.That(messages[0], Does.Contain(nameof(SerializedValidationTestEffect)));
|
|
Assert.That(messages[0], Does.Contain("not executed"));
|
|
}
|
|
|
|
[Test]
|
|
public void UnassignedSlotsPreserveValidSerializedEffectOrder()
|
|
{
|
|
var first = Effect("first effect");
|
|
var last = Effect("last effect");
|
|
var definition = Card("external_card", null, first, null, last, null);
|
|
|
|
var runtime = CardEffectFactory.CreateEffects(definition);
|
|
foreach (ICardEffect effect in runtime) effect.Execute(null);
|
|
|
|
Assert.That(runtime.Count, Is.EqualTo(2));
|
|
Assert.That(executed, Is.EqualTo(new[] { "first effect", "last effect" }));
|
|
Assert.That(first.FactoryCalls, Is.EqualTo(1));
|
|
Assert.That(last.FactoryCalls, Is.EqualTo(1));
|
|
}
|
|
|
|
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 SerializedValidationTestEffect Effect(string effectName)
|
|
{
|
|
var effect = ScriptableObject.CreateInstance<SerializedValidationTestEffect>();
|
|
temporary.Add(effect);
|
|
effect.name = effectName;
|
|
effect.RuntimeEffect = new RecordingEffect(effectName, executed);
|
|
return effect;
|
|
}
|
|
|
|
private sealed class RecordingEffect : ICardEffect
|
|
{
|
|
private readonly string name;
|
|
private readonly List<string> executed;
|
|
|
|
public RecordingEffect(string name, List<string> executed)
|
|
{
|
|
this.name = name;
|
|
this.executed = executed;
|
|
}
|
|
|
|
public void Execute(CombatContext context) => executed.Add(name);
|
|
}
|
|
}
|
|
|
|
public sealed class SerializedValidationTestEffect : CardEffectDefinition
|
|
{
|
|
public int FactoryCalls;
|
|
public ICardEffect RuntimeEffect;
|
|
|
|
public override ICardEffect CreateRuntimeEffect()
|
|
{
|
|
FactoryCalls++;
|
|
return RuntimeEffect;
|
|
}
|
|
}
|