PkmnLib.NET/PkmnLib.Tests/Integration/IntegrationTestRunner.cs

64 lines
2.3 KiB
C#
Raw Normal View History

2024-08-23 09:15:53 +00:00
using System.Collections;
using System.Text.Json;
2024-12-27 13:30:22 +00:00
using EnumerableAsyncProcessor.Extensions;
2024-08-23 09:15:53 +00:00
using PkmnLib.Dynamic.Models;
using PkmnLib.Static.Species;
using PkmnLib.Tests.Integration.Models;
namespace PkmnLib.Tests.Integration;
public class IntegrationTestRunner
{
2024-12-27 13:30:22 +00:00
public static IEnumerable<IntegrationTestModel> TestCases()
2024-08-23 09:15:53 +00:00
{
2024-12-27 13:30:22 +00:00
var files = Directory.GetFiles("Integration/Tests", "*.json");
var serializerOptions = new JsonSerializerOptions
2024-08-23 09:15:53 +00:00
{
2024-12-27 13:30:22 +00:00
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
2024-12-27 13:30:22 +00:00
};
foreach (var file in files)
{
var json = File.ReadAllText(file);
var test = JsonSerializer.Deserialize<IntegrationTestModel>(json, serializerOptions)!;
yield return test;
2024-08-23 09:15:53 +00:00
}
}
2025-03-02 16:19:57 +00:00
[Test, MethodDataSource(nameof(TestCases))]
2024-12-27 13:30:22 +00:00
public async Task RunIntegrationTest(IntegrationTestModel test)
2024-08-23 09:15:53 +00:00
{
var library = LibraryHelpers.LoadLibrary();
2024-08-23 09:15:53 +00:00
2024-12-27 13:30:22 +00:00
var parties = await test.BattleSetup.Parties.SelectAsync(async Task<IBattleParty> (x) =>
2024-08-23 09:15:53 +00:00
{
var party = new PokemonParty(6);
for (var index = 0; index < x.Pokemon.Length; index++)
{
var pokemon = x.Pokemon[index];
2024-12-27 13:30:22 +00:00
await Assert.That(library.StaticLibrary.Species.TryGet(pokemon.Species, out var species)).IsTrue();
2024-08-23 09:15:53 +00:00
var mon = new PokemonImpl(library, species!, species!.GetDefaultForm(), new AbilityIndex
2025-03-02 16:19:57 +00:00
{
IsHidden = false,
Index = 0,
}, pokemon.Level, 0, Gender.Genderless, 0, "hardy");
2024-08-23 09:15:53 +00:00
foreach (var move in pokemon.Moves)
{
mon.LearnMove(move, MoveLearnMethod.Unknown, 255);
}
party.SwapInto(mon, index);
}
return new BattlePartyImpl(party, x.Indices.Select(y => new ResponsibleIndex(y[0], y[1])).ToArray());
2024-12-27 13:30:22 +00:00
}).ProcessOneAtATime().GetResultsAsync();
2024-08-23 09:15:53 +00:00
var battle = new BattleImpl(library, parties, test.BattleSetup.CanFlee, test.BattleSetup.NumberOfSides,
2025-03-02 16:19:57 +00:00
test.BattleSetup.PositionsPerSide, test.BattleSetup.Seed);
2024-08-23 09:15:53 +00:00
foreach (var action in test.Actions)
{
2024-12-27 13:30:22 +00:00
await action.Execute(battle);
2024-08-23 09:15:53 +00:00
}
}
}