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,202 @@
using PkmnLib.Dynamic.Models;
using PkmnLib.Dynamic.ScriptHandling;
using PkmnLib.Dynamic.ScriptHandling.Registry;
using PkmnLib.Plugin.Gen7.Scripts.Moves;
using PkmnLib.Plugin.Gen7.Scripts.Side;
using PkmnLib.Static.Moves;
namespace PkmnLib.Plugin.Gen7.Tests.Scripts.Moves;
/// <summary>
/// Tests for the <see cref="MatBlock"/> move script and the <see cref="MatBlockEffect"/> it applies.
/// Gen VII Bulbapedia behavior: "Mat Block protects all Pokémon on the user's side of the field from any
/// physical or special moves for that turn."
/// </summary>
public class MatBlockTests
{
private static (MatBlock script, IExecutingMove move, IPokemon target, IHitData hitData, IScriptSet sideVolatile)
CreateTestSetup(uint switchInTurn, uint currentTurn)
{
var script = new MatBlock();
var side = Substitute.For<IBattleSide>();
// Use a real script set so the side script added by Mat Block can be inspected afterwards.
var sideVolatile = new ScriptSet(side);
side.VolatileScripts.Returns(sideVolatile);
side.GetScripts().Returns(_ => new ScriptIterator(new List<IEnumerable<ScriptContainer>>()));
var battle = Substitute.For<IBattle>();
battle.CurrentTurnNumber.Returns(currentTurn);
var battleData = Substitute.For<IPokemonBattleData>();
battleData.Battle.Returns(battle);
battleData.BattleSide.Returns(side);
battleData.SwitchInTurn.Returns(switchInTurn);
var user = Substitute.For<IPokemon>();
user.BattleData.Returns(battleData);
var move = Substitute.For<IExecutingMove>();
move.User.Returns(user);
var target = Substitute.For<IPokemon>();
var hitData = Substitute.For<IHitData>();
move.GetHitData(target, 0).Returns(hitData);
return (script, move, target, hitData, sideVolatile);
}
private static IExecutingMove CreateExecutingMoveOfCategory(MoveCategory category)
{
var useMove = Substitute.For<IMoveData>();
useMove.Category.Returns(category);
var move = Substitute.For<IExecutingMove>();
move.UseMove.Returns(useMove);
return move;
}
/// <summary>
/// Bulbapedia: "Mat Block protects all Pokémon on the user's side of the field" — using the move on
/// the user's first turn on the field attaches the <see cref="MatBlockEffect"/> to the user's side.
/// </summary>
[Test]
public async Task OnSecondaryEffect_UsedOnSwitchInTurn_AddsMatBlockEffectToUsersSide()
{
// Arrange
var (script, move, target, _, sideVolatile) = CreateTestSetup(1, 1);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
await Assert.That(sideVolatile.Contains(ScriptUtils.ResolveName<MatBlockEffect>())).IsTrue();
}
/// <summary>
/// Bulbapedia: Mat Block only fails "if not used on the first turn after the Pokémon enters the
/// field" — on the user's first turn on the field the hit is not marked as failed.
/// </summary>
[Test]
public async Task OnSecondaryEffect_UsedOnSwitchInTurn_HitDoesNotFail()
{
// Arrange
var (script, move, target, hitData, _) = CreateTestSetup(1, 1);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
await Assert.That(hitData.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "Fail")).IsFalse();
}
/// <summary>
/// Bulbapedia: "Mat Block will fail if not used on the first turn after the Pokémon enters the field;
/// the Pokémon must be switched out and back in to be able to use Mat Block again."
/// </summary>
[Test]
public async Task OnSecondaryEffect_NotFirstTurnOnField_HitFails()
{
// Arrange
var (script, move, target, hitData, _) = CreateTestSetup(1, 2);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
await Assert.That(hitData.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "Fail")).IsTrue();
}
/// <summary>
/// Bulbapedia: "Mat Block will fail if not used on the first turn after the Pokémon enters the field"
/// — when it fails, no protection is set up on the user's side.
/// </summary>
[Test]
public async Task OnSecondaryEffect_NotFirstTurnOnField_NoEffectAdded()
{
// Arrange
var (script, move, target, _, sideVolatile) = CreateTestSetup(1, 2);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert
await Assert.That(sideVolatile.Contains(ScriptUtils.ResolveName<MatBlockEffect>())).IsFalse();
}
/// <summary>
/// Bulbapedia: the protection covers "any physical or special moves" — the
/// <see cref="MatBlockEffect"/> blocks an incoming damaging hit of either category.
/// </summary>
[Test, Arguments(MoveCategory.Physical), Arguments(MoveCategory.Special)]
public async Task MatBlockEffect_DamagingMove_IsBlocked(MoveCategory category)
{
// Arrange
var effect = new MatBlockEffect();
var incomingMove = CreateExecutingMoveOfCategory(category);
var block = false;
// Act
effect.BlockIncomingHit(incomingMove, Substitute.For<IPokemon>(), 0, ref block);
// Assert
await Assert.That(block).IsTrue();
}
/// <summary>
/// Bulbapedia: Mat Block protects "from any physical or special moves" — unlike Protect it does not
/// stop status moves, so an incoming status move is not blocked.
/// </summary>
[Test]
public async Task MatBlockEffect_StatusMove_IsNotBlocked()
{
// Arrange
var effect = new MatBlockEffect();
var incomingMove = CreateExecutingMoveOfCategory(MoveCategory.Status);
var block = false;
// Act
effect.BlockIncomingHit(incomingMove, Substitute.For<IPokemon>(), 0, ref block);
// Assert
await Assert.That(block).IsFalse();
}
/// <summary>
/// Bulbapedia: the protection lasts "for that turn" — at the end of the turn the
/// <see cref="MatBlockEffect"/> removes itself from the side.
/// </summary>
[Test]
public async Task MatBlockEffect_OnEndTurn_EffectExpires()
{
// Arrange
var effect = new MatBlockEffect();
var side = Substitute.For<IBattleSide>();
side.GetScripts().Returns(_ => new ScriptIterator(new List<IEnumerable<ScriptContainer>>()));
IScriptSet sideVolatile = new ScriptSet(side);
sideVolatile.Add(effect);
// Act
effect.OnEndTurn(side, Substitute.For<IBattle>());
// Assert
await Assert.That(sideVolatile.Contains(ScriptUtils.ResolveName<MatBlockEffect>())).IsFalse();
}
/// <summary>
/// Technical test: outside of battle (no battle data) no protection can be raised and nothing
/// happens.
/// </summary>
[Test]
public async Task OnSecondaryEffect_NullBattleData_DoesNothing()
{
// Arrange
var (script, move, target, hitData, sideVolatile) = CreateTestSetup(1, 1);
move.User.BattleData.Returns((IPokemonBattleData?)null);
// Act
script.OnSecondaryEffect(move, target, 0);
// Assert - no effect is added and the hit is not failed
await Assert.That(sideVolatile.Contains(ScriptUtils.ResolveName<MatBlockEffect>())).IsFalse();
await Assert.That(hitData.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "Fail")).IsFalse();
}
}