Move all battle state from IPokemon to an ephemeral IBattlePokemon wrapper

This commit is contained in:
2026-08-28 15:20:26 +02:00
parent 942be8eaeb
commit 8a2733a0a9
859 changed files with 4408 additions and 5331 deletions

View File

@@ -17,8 +17,8 @@ public class GearUpTests
/// Creates a fully mocked test setup for Gear Up tests. The user is on side 0; the given side lists
/// are installed as the battle's sides.
/// </summary>
private static (GearUp script, IExecutingMove move, IPokemon user) CreateTestSetup(
params IReadOnlyList<IPokemon?>[] sidePokemon)
private static (GearUp script, IExecutingMove move, IBattlePokemon user) CreateTestSetup(
params IReadOnlyList<IBattlePokemon?>[] sidePokemon)
{
var script = new GearUp();
var move = Substitute.For<IExecutingMove>();
@@ -32,23 +32,21 @@ public class GearUpTests
}).ToArray();
battle.Sides.Returns(sides);
var user = Substitute.For<IPokemon>();
var battleData = Substitute.For<IPokemonBattleData>();
battleData.Battle.Returns(battle);
battleData.SideIndex.Returns((byte)0);
user.BattleData.Returns(battleData);
var user = Substitute.For<IBattlePokemon>();
user.Battle.Returns(battle);
user.SideIndex.Returns((byte)0);
move.User.Returns(user);
return (script, move, user);
}
/// <summary>
/// Creates a mocked Pokémon whose <see cref="IPokemon.ActiveAbility"/> has the given name, or no
/// Creates a mocked Pokémon whose <see cref="IBattlePokemon.ActiveAbility"/> has the given name, or no
/// ability at all when <paramref name="abilityName"/> is null.
/// </summary>
private static IPokemon CreatePokemon(string? abilityName)
private static IBattlePokemon CreatePokemon(string? abilityName)
{
var pokemon = Substitute.For<IPokemon>();
var pokemon = Substitute.For<IBattlePokemon>();
if (abilityName != null)
{
var ability = Substitute.For<IAbility>();
@@ -68,7 +66,7 @@ public class GearUpTests
/// null when that stat was not boosted. Received-call inspection is used instead of NSubstitute
/// argument matchers because the trailing EventBatchId parameter cannot be bound by <c>Arg.Any</c>.
/// </summary>
private static object?[]? GetStatBoostArgs(IPokemon pokemon, Statistic stat) =>
private static object?[]? GetStatBoostArgs(IBattlePokemon pokemon, Statistic stat) =>
pokemon.ReceivedCalls().Where(c => c.GetMethodInfo().Name == "ChangeStatBoost").Select(c => c.GetArguments())
.FirstOrDefault(args => (Statistic)args[0]! == stat);
@@ -81,10 +79,10 @@ public class GearUpTests
{
// Arrange
var ally = CreatePokemon("plus");
var (script, move, _) = CreateTestSetup(new List<IPokemon?> { ally });
var (script, move, _) = CreateTestSetup(new List<IBattlePokemon?> { ally });
// Act
script.OnSecondaryEffect(move, Substitute.For<IPokemon>(), 0);
script.OnSecondaryEffect(move, Substitute.For<IBattlePokemon>(), 0);
// Assert
var attackArgs = GetStatBoostArgs(ally, Statistic.Attack);
@@ -104,10 +102,10 @@ public class GearUpTests
{
// Arrange
var ally = CreatePokemon("minus");
var (script, move, _) = CreateTestSetup(new List<IPokemon?> { ally });
var (script, move, _) = CreateTestSetup(new List<IBattlePokemon?> { ally });
// Act
script.OnSecondaryEffect(move, Substitute.For<IPokemon>(), 0);
script.OnSecondaryEffect(move, Substitute.For<IBattlePokemon>(), 0);
// Assert
await Assert.That(GetStatBoostArgs(ally, Statistic.Attack)).IsNotNull();
@@ -127,11 +125,11 @@ public class GearUpTests
ability.Name.Returns(new StringKey("plus"));
user.ActiveAbility.Returns(ability);
var side = Substitute.For<IBattleSide>();
side.Pokemon.Returns(new List<IPokemon?> { user });
user.BattleData!.Battle.Sides.Returns(new[] { side });
side.Pokemon.Returns(new List<IBattlePokemon?> { user });
user.Battle.Sides.Returns(new[] { side });
// Act
script.OnSecondaryEffect(move, Substitute.For<IPokemon>(), 0);
script.OnSecondaryEffect(move, Substitute.For<IBattlePokemon>(), 0);
// Assert
var attackArgs = GetStatBoostArgs(user, Statistic.Attack);
@@ -148,10 +146,10 @@ public class GearUpTests
{
// Arrange
var ally = CreatePokemon("levitate");
var (script, move, _) = CreateTestSetup(new List<IPokemon?> { ally });
var (script, move, _) = CreateTestSetup(new List<IBattlePokemon?> { ally });
// Act
script.OnSecondaryEffect(move, Substitute.For<IPokemon>(), 0);
script.OnSecondaryEffect(move, Substitute.For<IBattlePokemon>(), 0);
// Assert
await Assert.That(ally.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "ChangeStatBoost")).IsFalse();
@@ -165,10 +163,10 @@ public class GearUpTests
{
// Arrange
var opponent = CreatePokemon("plus");
var (script, move, _) = CreateTestSetup(new List<IPokemon?>(), new List<IPokemon?> { opponent });
var (script, move, _) = CreateTestSetup(new List<IBattlePokemon?>(), new List<IBattlePokemon?> { opponent });
// Act
script.OnSecondaryEffect(move, Substitute.For<IPokemon>(), 0);
script.OnSecondaryEffect(move, Substitute.For<IBattlePokemon>(), 0);
// Assert
await Assert.That(opponent.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "ChangeStatBoost")).IsFalse();
@@ -182,10 +180,10 @@ public class GearUpTests
{
// Arrange
var ally = CreatePokemon(null);
var (script, move, _) = CreateTestSetup(new List<IPokemon?> { ally });
var (script, move, _) = CreateTestSetup(new List<IBattlePokemon?> { ally });
// Act
script.OnSecondaryEffect(move, Substitute.For<IPokemon>(), 0);
script.OnSecondaryEffect(move, Substitute.For<IBattlePokemon>(), 0);
// Assert
await Assert.That(ally.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "ChangeStatBoost")).IsFalse();
@@ -199,31 +197,12 @@ public class GearUpTests
{
// Arrange
var ally = CreatePokemon("plus");
var (script, move, _) = CreateTestSetup(new List<IPokemon?> { null, ally });
var (script, move, _) = CreateTestSetup(new List<IBattlePokemon?> { null, ally });
// Act
script.OnSecondaryEffect(move, Substitute.For<IPokemon>(), 0);
script.OnSecondaryEffect(move, Substitute.For<IBattlePokemon>(), 0);
// Assert
await Assert.That(GetStatBoostArgs(ally, Statistic.Attack)).IsNotNull();
}
/// <summary>
/// Technical test: when the user has no <see cref="IPokemon.BattleData"/> (it is not in a battle),
/// the script does nothing instead of throwing.
/// </summary>
[Test]
public async Task OnSecondaryEffect_NullBattleData_DoesNothing()
{
// Arrange
var ally = CreatePokemon("plus");
var (script, move, user) = CreateTestSetup(new List<IPokemon?> { ally });
user.BattleData.Returns((IPokemonBattleData?)null);
// Act
script.OnSecondaryEffect(move, Substitute.For<IPokemon>(), 0);
// Assert
await Assert.That(ally.ReceivedCalls().Any(c => c.GetMethodInfo().Name == "ChangeStatBoost")).IsFalse();
}
}