Migrate to TUnit for unit tests
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections;
|
||||
using System.Text.Json;
|
||||
using EnumerableAsyncProcessor.Extensions;
|
||||
using PkmnLib.Dynamic.Models;
|
||||
using PkmnLib.Static.Species;
|
||||
using PkmnLib.Tests.Integration.Models;
|
||||
@@ -8,36 +9,34 @@ namespace PkmnLib.Tests.Integration;
|
||||
|
||||
public class IntegrationTestRunner
|
||||
{
|
||||
private static IEnumerable TestCases
|
||||
public static IEnumerable<IntegrationTestModel> TestCases()
|
||||
{
|
||||
get
|
||||
var files = Directory.GetFiles("Integration/Tests", "*.json");
|
||||
var serializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
var files = Directory.GetFiles("Integration/Tests", "*.json");
|
||||
var serializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
};
|
||||
foreach (var file in files)
|
||||
{
|
||||
var json = File.ReadAllText(file);
|
||||
var test = JsonSerializer.Deserialize<IntegrationTestModel>(json, serializerOptions)!;
|
||||
yield return new TestCaseData(test).SetName(test.Name).SetDescription(test.Description);
|
||||
}
|
||||
PropertyNameCaseInsensitive = true,
|
||||
};
|
||||
foreach (var file in files)
|
||||
{
|
||||
var json = File.ReadAllText(file);
|
||||
var test = JsonSerializer.Deserialize<IntegrationTestModel>(json, serializerOptions)!;
|
||||
yield return test;
|
||||
}
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(TestCases))]
|
||||
public void RunIntegrationTest(IntegrationTestModel test)
|
||||
[Test]
|
||||
[MethodDataSource(nameof(TestCases))]
|
||||
public async Task RunIntegrationTest(IntegrationTestModel test)
|
||||
{
|
||||
var library = LibraryHelpers.LoadLibrary();
|
||||
|
||||
var parties = test.BattleSetup.Parties.Select(IBattleParty (x) =>
|
||||
var parties = await test.BattleSetup.Parties.SelectAsync(async Task<IBattleParty> (x) =>
|
||||
{
|
||||
var party = new PokemonParty(6);
|
||||
for (var index = 0; index < x.Pokemon.Length; index++)
|
||||
{
|
||||
var pokemon = x.Pokemon[index];
|
||||
Assert.That(library.StaticLibrary.Species.TryGet(pokemon.Species, out var species), Is.True);
|
||||
await Assert.That(library.StaticLibrary.Species.TryGet(pokemon.Species, out var species)).IsTrue();
|
||||
var mon = new PokemonImpl(library, species!, species!.GetDefaultForm(), new AbilityIndex
|
||||
{
|
||||
IsHidden = false,
|
||||
@@ -53,13 +52,13 @@ public class IntegrationTestRunner
|
||||
}
|
||||
|
||||
return new BattlePartyImpl(party, x.Indices.Select(y => new ResponsibleIndex(y[0], y[1])).ToArray());
|
||||
}).ToArray();
|
||||
}).ProcessOneAtATime().GetResultsAsync();
|
||||
var battle = new BattleImpl(library, parties, test.BattleSetup.CanFlee, test.BattleSetup.NumberOfSides,
|
||||
test.BattleSetup.PositionsPerSide);
|
||||
|
||||
foreach (var action in test.Actions)
|
||||
{
|
||||
action.Execute(battle);
|
||||
await action.Execute(battle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,19 +9,19 @@ public static class LibraryHelpers
|
||||
{
|
||||
public static IDynamicLibrary LoadLibrary()
|
||||
{
|
||||
using var typesFile = File.Open("Data/Types.csv", FileMode.Open);
|
||||
using var typesFile = File.Open("Data/Types.csv", FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var types = TypeDataLoader.LoadTypeLibrary(typesFile);
|
||||
using var naturesFile = File.Open("Data/Natures.csv", FileMode.Open);
|
||||
using var naturesFile = File.Open("Data/Natures.csv", FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var natures = NatureDataLoader.LoadNatureLibrary(naturesFile);
|
||||
using var movesFile = File.Open("Data/Moves.json", FileMode.Open);
|
||||
using var movesFile = File.Open("Data/Moves.json", FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var moves = MoveDataLoader.LoadMoves(movesFile, types);
|
||||
using var itemsFile = File.Open("Data/Items.json", FileMode.Open);
|
||||
using var itemsFile = File.Open("Data/Items.json", FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var items = ItemDataLoader.LoadItems(itemsFile);
|
||||
using var abilitiesFile = File.Open("Data/Abilities.json", FileMode.Open);
|
||||
using var abilitiesFile = File.Open("Data/Abilities.json", FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var abilities = AbilityDataLoader.LoadAbilities(abilitiesFile);
|
||||
using var growthRatesFile = File.Open("Data/GrowthRates.json", FileMode.Open);
|
||||
using var growthRatesFile = File.Open("Data/GrowthRates.json", FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var growthRates = GrowthRateDataLoader.LoadGrowthRates(growthRatesFile);
|
||||
using var speciesFile = File.Open("Data/Pokemon.json", FileMode.Open);
|
||||
using var speciesFile = File.Open("Data/Pokemon.json", FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var species = SpeciesDataLoader.LoadSpecies(speciesFile, types);
|
||||
|
||||
var staticLibrary = new StaticLibraryImpl(new LibrarySettings()
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace PkmnLib.Tests.Integration.Models;
|
||||
[JsonDerivedType(typeof(AssertAction), "assert")]
|
||||
public abstract class IntegrationTestAction
|
||||
{
|
||||
public abstract void Execute(IBattle battle);
|
||||
public abstract Task Execute(IBattle battle);
|
||||
}
|
||||
|
||||
public class SetPokemonAction : IntegrationTestAction
|
||||
@@ -22,10 +22,11 @@ public class SetPokemonAction : IntegrationTestAction
|
||||
public List<byte> Place { get; set; } = null!;
|
||||
public List<byte> FromParty { get; set; } = null!;
|
||||
|
||||
public override void Execute(IBattle battle)
|
||||
public override Task Execute(IBattle battle)
|
||||
{
|
||||
var mon = battle.Parties[FromParty[0]].Party[FromParty[1]];
|
||||
battle.Sides[Place[0]].SwapPokemon(Place[1], mon);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,14 +38,14 @@ public class SetMoveChoiceAction : IntegrationTestAction
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Execute(IBattle battle)
|
||||
public override async Task Execute(IBattle battle)
|
||||
{
|
||||
var user = battle.Sides[Place[0]].Pokemon[Place[1]];
|
||||
Assert.That(user, Is.Not.Null);
|
||||
await Assert.That(user).IsNotNull();
|
||||
var move = user.Moves.First(m => m?.MoveData.Name == Move);
|
||||
Assert.That(move, Is.Not.Null);
|
||||
await Assert.That(move).IsNotNull();
|
||||
var res = battle.TrySetChoice(new MoveChoice(user, move, Target[0], Target[1]));
|
||||
Assert.That(res, Is.True);
|
||||
await Assert.That(res).IsTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,12 +54,12 @@ public class SetPassChoiceAction : IntegrationTestAction
|
||||
public List<byte> Place { get; set; } = null!;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Execute(IBattle battle)
|
||||
public override async Task Execute(IBattle battle)
|
||||
{
|
||||
var user = battle.Sides[Place[0]].Pokemon[Place[1]];
|
||||
Assert.That(user, Is.Not.Null);
|
||||
await Assert.That(user).IsNotNull();
|
||||
var res = battle.TrySetChoice(new PassChoice(user));
|
||||
Assert.That(res, Is.True);
|
||||
await Assert.That(res).IsTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,12 +69,12 @@ public class AssertAction : IntegrationTestAction
|
||||
public JsonNode Expected { get; set; } = null!;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Execute(IBattle battle)
|
||||
public override async Task Execute(IBattle battle)
|
||||
{
|
||||
var list = battle.Path(Value).ToList();
|
||||
var value = list.Count == 1 ? list[0] : list;
|
||||
|
||||
var serialized = JsonSerializer.Serialize(value);
|
||||
Assert.That(serialized, Is.EqualTo(Expected.ToJsonString()));
|
||||
await Assert.That(serialized).IsEqualTo(Expected.ToJsonString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user