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

155 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using KernelPanic.Combat;
using KernelPanic.Core;
using KernelPanic.Data;
using KernelPanic.Run;
using NUnit.Framework;
using UnityEngine;
public class ShieldEffectExecutionTests
{
private const BindingFlags Private = BindingFlags.Instance | BindingFlags.NonPublic;
private readonly List<ScriptableObject> _definitions = new();
private readonly List<string> _messages = new();
private GameObject _owner;
private CombatManager _combat;
private CombatantState Player => _combat.PlayerState;
private static object Call(object owner, string method) => owner.GetType().GetMethod(method, Private).Invoke(owner, null);
private static void Set(object owner, string field, object value) => owner.GetType().GetField(field, Private).SetValue(owner, value);
[SetUp]
public void SetUp()
{
_owner = new GameObject("shield execution regression");
_combat = _owner.AddComponent<CombatManager>();
Call(_combat, "OnDisable");
Call(_combat, "Awake");
Set(_combat, "_playerState", new CombatantState(100, 32, 10) { FlatEffectBonus = 3 });
Set(_combat, "_handController", new HandController(32));
Set(_combat, "currentPhase", TurnPhase.Execute);
_combat.Compilation.BeginWave(4);
_combat.Compilation.AdvanceFromEquipment(3, 1); // 120% explicit Shield.
_combat.CombatLog += _messages.Add;
}
[TearDown]
public void TearDown()
{
if (_combat != null)
{
_combat.CombatLog -= _messages.Add;
Call(_combat, "OnDisable");
}
UnityEngine.Object.DestroyImmediate(_owner);
foreach (var definition in _definitions) UnityEngine.Object.DestroyImmediate(definition);
_definitions.Clear();
_messages.Clear();
}
private CardInstance Card(string id)
{
var definition = ScriptableObject.CreateInstance<CardDefinition>();
_definitions.Add(definition);
Set(definition, "id", id);
Set(definition, "language", Language.Haskell);
return new CardInstance(definition);
}
private CombatContext Context(CardInstance card)
{
var context = new CombatContext(card, Player, Array.Empty<CombatantState>(), _combat,
_combat.DamagePipeline, new StatusEffectController(), _combat.DeckController,
_combat.HandController, _combat.Enemies);
typeof(CombatContext).GetProperty(nameof(CombatContext.LazyMultiplierPercent)).SetValue(context, 150);
return context;
}
private ICardEffect Effect(string kind)
{
if (kind == "minimal")
{
Assert.That(_combat.HandController.Add(Card("exhaust_a")), Is.True);
Assert.That(_combat.HandController.Add(Card("exhaust_b")), Is.True);
return new MinimalInstallEffect();
}
return kind switch
{
"first" => new FirstCardShieldEffect(4, 5),
"maybe" => new HaskellMaybeEffect(),
_ => new ShieldEffect(4)
};
}
private void ConfigureMint(CardInstance exclusive = null)
{
var distro = ScriptableObject.CreateInstance<DistroDefinition>();
_definitions.Add(distro);
Set(distro, "id", "mint");
if (exclusive != null) Set(distro, "exclusiveCards", new List<CardDefinition> { exclusive.Definition });
Set(_combat, "_runConfig", new RunConfig(distro, Language.Haskell, Language.C, Array.Empty<CardDefinition>()));
}
[TestCase("normal", false, 16)]
[TestCase("first", false, 16)]
[TestCase("first", true, 25)]
[TestCase("minimal", false, 11)]
[TestCase("maybe", false, 16)]
public void ShieldRewardsApplyTheirBonusesAndExplicitMultipliersOnce(string kind, bool firstCard, int expected)
{
var card = Card("shield_test");
card.ApplyMagnitudeUpgrade(2);
card.MarkPlayedThisTurn(firstCard);
Effect(kind).Execute(Context(card));
Assert.That(Player.Shield, Is.EqualTo(expected),
"Normal rewards include +3 source and +2 magnitude before 150% lazy and 120% compilation scaling; Minimal Install keeps its fixed reward.");
if (kind == "minimal")
{
Assert.That(Player.Cycles, Is.EqualTo(12));
Assert.That(_combat.DeckController.ExhaustPile, Has.Count.EqualTo(2));
Assert.That(_combat.HandController.Cards, Is.Empty);
}
}
[TestCase("normal", 9)]
[TestCase("first", 14)]
[TestCase("minimal", 6)]
[TestCase("maybe", 9)]
public void MintBlocksAndReportsEveryExplicitCardShieldPath(string kind, int blockedAmount)
{
ConfigureMint();
var card = Card("nonexclusive_shield_test");
card.ApplyMagnitudeUpgrade(2);
card.MarkPlayedThisTurn(true);
Player.Shield = 7;
Effect(kind).Execute(Context(card));
Assert.That(Player.Shield, Is.EqualTo(7));
Assert.That(_messages.FindAll(message => message.StartsWith("shield blocked:")),
Is.EqualTo(new[] { $"shield blocked: {blockedAmount}" }));
if (kind == "minimal")
{
Assert.That(Player.Cycles, Is.EqualTo(12), "Shield eligibility does not block the exhaust or Cycle rewards.");
Assert.That(_combat.DeckController.ExhaustPile, Has.Count.EqualTo(2));
}
}
[Test]
public void MintStillAllowsShieldFromItsExclusiveCards()
{
var card = Card("mint_exclusive_shield_test");
card.ApplyMagnitudeUpgrade(2);
ConfigureMint(card);
new ShieldEffect(4).Execute(Context(card));
Assert.That(Player.Shield, Is.EqualTo(16));
Assert.That(_messages, Has.No.Member("shield blocked: 9"));
}
}