Many more tests and fixes
All checks were successful
Build / Build (push) Successful in 3m12s

This commit is contained in:
2026-08-27 17:11:12 +02:00
parent 32b3ef9c4a
commit d2a82b5fe3
204 changed files with 20255 additions and 242 deletions

View File

@@ -0,0 +1,270 @@
using PkmnLib.Dynamic.Events;
using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Dynamic.ScriptHandling.Registry;
using PkmnLib.Plugin.Gen7.Scripts;
using PkmnLib.Plugin.Gen7.Scripts.Moves;
using PkmnLib.Plugin.Gen7.Scripts.Pokemon;
using PkmnLib.Static.Utils;
namespace PkmnLib.Plugin.Gen7.Tests.Scripts.Moves;
/// <summary>
/// Tests for the <see cref="IceBurn"/> move script, a two-turn charge move using
/// <see cref="RequireChargeEffect"/>.
/// Gen VII Bulbapedia behavior: "Ice Burn deals damage and has a 30% chance of burning the target." On the
/// initial turn, the user enters a charging state while "cloaked in a freezing air"; the actual damage occurs
/// on the subsequent turn. "A Power Herb item can bypass the charging phase entirely."
/// </summary>
public class IceBurnTests
{
/// <summary>
/// Test helper script that bypasses the charge turn through the
/// <see cref="CustomTriggers.BypassChargeMove"/> custom trigger, the same way the Power Herb item
/// script does.
/// </summary>
[Script(ScriptCategory.Pokemon, "test_charge_bypasser")]
private class ChargeBypasser : Script, IScriptCustomTrigger
{
public void CustomTrigger(StringKey eventName, ICustomTriggerArgs args)
{
if (eventName == CustomTriggers.BypassChargeMove && args is CustomTriggers.BypassChargeMoveArgs bypassArgs)
bypassArgs.Bypass = true;
}
}
/// <summary>
/// Creates a fully mocked test setup for Ice Burn tests. The user gets a real <see cref="ScriptSet"/> as
/// its volatile set, so the charge volatile added by the move can be inspected afterwards.
/// </summary>
private static (IceBurn script, IExecutingMove move, IPokemon user, ScriptSet userVolatile, IBattleRandom random,
EventHook eventHook) CreateTestSetup()
{
var script = new IceBurn();
var move = Substitute.For<IExecutingMove>();
move.GetScripts().Returns(_ => new ScriptIterator(new List<IEnumerable<ScriptContainer>>()));
var user = Substitute.For<IPokemon>();
user.GetScripts().Returns(_ => new ScriptIterator(new List<IEnumerable<ScriptContainer>>()));
var userVolatile = new ScriptSet(user);
user.Volatile.Returns(userVolatile);
move.User.Returns(user);
var eventHook = new EventHook();
var random = Substitute.For<IBattleRandom>();
var battle = Substitute.For<IBattle>();
battle.EventHook.Returns(eventHook);
battle.Random.Returns(random);
var battleData = Substitute.For<IPokemonBattleData>();
battleData.Battle.Returns(battle);
user.BattleData.Returns(battleData);
return (script, move, user, userVolatile, random, eventHook);
}
/// <summary>
/// Helper to check whether a Pokémon received any SetStatus call. (Checked via ReceivedCalls, as arg
/// matchers cannot be used for <see cref="EventBatchId"/> parameters.)
/// </summary>
private static bool ReceivedSetStatus(IPokemon pokemon) =>
pokemon.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "SetStatus");
/// <summary>
/// Bulbapedia: on the initial turn, the user enters a charging state; the actual damage occurs on the
/// subsequent turn. The first use is prevented from executing.
/// </summary>
[Test]
public async Task PreventMove_FirstUse_PreventsMoveForChargeTurn()
{
// Arrange
var (script, move, _, _, _, _) = CreateTestSetup();
var prevent = false;
// Act
script.PreventMove(move, ref prevent);
// Assert
await Assert.That(prevent).IsTrue();
}
/// <summary>
/// Bulbapedia: the user enters a charging state on the first turn. The charge turn attaches the
/// <see cref="RequireChargeEffect"/> volatile to the user.
/// </summary>
[Test]
public async Task PreventMove_FirstUse_AddsRequireChargeEffectToUser()
{
// Arrange
var (script, move, _, userVolatile, _, _) = CreateTestSetup();
var prevent = false;
// Act
script.PreventMove(move, ref prevent);
// Assert
await Assert.That(userVolatile.Contains(ScriptUtils.ResolveName<RequireChargeEffect>())).IsTrue();
}
/// <summary>
/// Bulbapedia: the user is "cloaked in a freezing air" on the charge turn — the charge turn announces
/// itself through a <see cref="DialogEvent"/> so the battle log can show the charging message.
/// </summary>
[Test]
public async Task PreventMove_FirstUse_FiresBeganChargingDialogEvent()
{
// Arrange
var (script, move, user, _, _, eventHook) = CreateTestSetup();
DialogEvent? captured = null;
eventHook.Handler += (_, args) =>
{
if (args is DialogEvent dialogEvent)
captured = dialogEvent;
};
var prevent = false;
// Act
script.PreventMove(move, ref prevent);
// Assert
await Assert.That(captured).IsNotNull();
await Assert.That(captured!.Message).IsEqualTo("began_charging");
var userParameter = captured!.Parameters?.GetValueOrDefault("user");
await Assert.That(ReferenceEquals(userParameter, user)).IsTrue();
}
/// <summary>
/// Bulbapedia: "The actual damage occurs on the subsequent turn." When the user already carries the
/// charge volatile from the previous turn, the move is not prevented again.
/// </summary>
[Test]
public async Task PreventMove_UserAlreadyCharged_DoesNotPreventMove()
{
// Arrange
var (script, move, user, userVolatile, _, _) = CreateTestSetup();
userVolatile.Add(script.CreateVolatile(user));
var prevent = false;
// Act
script.PreventMove(move, ref prevent);
// Assert
await Assert.That(prevent).IsFalse();
}
/// <summary>
/// The attack executes on the second turn, ending the charging state: once the move executes, the
/// <see cref="RequireChargeEffect"/> is removed from the user.
/// </summary>
[Test]
public async Task OnBeforeMove_ChargeCompleted_RemovesRequireChargeEffect()
{
// Arrange
var (script, move, user, userVolatile, _, _) = CreateTestSetup();
userVolatile.Add(script.CreateVolatile(user));
// Act
script.OnBeforeMove(move);
// Assert
await Assert.That(userVolatile.Contains(ScriptUtils.ResolveName<RequireChargeEffect>())).IsFalse();
}
/// <summary>
/// Bulbapedia: "Ice Burn deals damage and has a 30% chance of burning the target." When the burn roll
/// succeeds, the target is burned by the user.
/// </summary>
[Test]
public void OnSecondaryEffect_BurnRollSucceeds_TargetIsBurned()
{
// Arrange
var (script, move, user, _, random, _) = CreateTestSetup();
var target = Substitute.For<IPokemon>();
random.EffectChance(30, move, target, 0).Returns(true);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
target.Received(1).SetStatus("burned", user);
}
/// <summary>
/// Bulbapedia: "has a 30% chance of burning the target." The burn roll is made with a 30 percent chance.
/// </summary>
[Test]
public void OnSecondaryEffect_BurnRoll_UsesThirtyPercentChance()
{
// Arrange
var (script, move, _, _, random, _) = CreateTestSetup();
var target = Substitute.For<IPokemon>();
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
random.Received(1).EffectChance(30, move, target, 0);
}
/// <summary>
/// Bulbapedia: "has a 30% chance of burning the target." When the burn roll fails, the target is not
/// burned.
/// </summary>
[Test]
public async Task OnSecondaryEffect_BurnRollFails_TargetIsNotBurned()
{
// Arrange
var (script, move, _, _, random, _) = CreateTestSetup();
var target = Substitute.For<IPokemon>();
random.EffectChance(30, move, target, 0).Returns(false);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
await Assert.That(ReceivedSetStatus(target)).IsFalse();
}
/// <summary>
/// Technical test: if the user has no <see cref="IPokemon.BattleData"/>, the secondary effect does
/// nothing.
/// </summary>
[Test]
public async Task OnSecondaryEffect_UserHasNoBattleData_DoesNothing()
{
// Arrange
var script = new IceBurn();
var move = Substitute.For<IExecutingMove>();
var user = Substitute.For<IPokemon>();
user.BattleData.Returns((IPokemonBattleData?)null);
move.User.Returns(user);
var target = Substitute.For<IPokemon>();
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
await Assert.That(ReceivedSetStatus(target)).IsFalse();
}
/// <summary>
/// Bulbapedia: "A Power Herb item can bypass the charging phase entirely." The bypass flows through the
/// <see cref="CustomTriggers.BypassChargeMove"/> custom trigger; a script that sets the bypass flag
/// (as the Power Herb item script does) lets the user attack on the first turn without charging.
/// </summary>
[Test]
public async Task PreventMove_ChargeBypassedByTrigger_MoveIsNotPrevented()
{
// Arrange
var (script, move, _, _, _, _) = CreateTestSetup();
move.GetScripts().Returns(_ => new ScriptIterator(new List<IEnumerable<ScriptContainer>>
{
new ScriptContainer(new ChargeBypasser()),
}));
var prevent = false;
// Act
script.PreventMove(move, ref prevent);
// Assert
await Assert.That(prevent).IsFalse();
}
}