88 lines
3.5 KiB
C#
88 lines
3.5 KiB
C#
using PkmnLib.Dynamic.Libraries;
|
|
using PkmnLib.Dynamic.ScriptHandling;
|
|
using PkmnLib.Static.Moves;
|
|
using PkmnLib.Tests.Integration;
|
|
|
|
namespace PkmnLib.Tests.DataTests;
|
|
|
|
public class MoveDataTests
|
|
{
|
|
public record TestCaseData(IDynamicLibrary Library, IMoveData Move)
|
|
{
|
|
/// <inheritdoc />
|
|
public override string ToString() => Move.Name + " has valid scripts";
|
|
}
|
|
|
|
public static IEnumerable<Func<TestCaseData>> AllMovesHaveValidScriptsData()
|
|
{
|
|
var library = LibraryHelpers.LoadLibrary();
|
|
var moveLibrary = library.StaticLibrary.Moves;
|
|
foreach (var move in moveLibrary)
|
|
{
|
|
if (move.SecondaryEffect == null)
|
|
continue;
|
|
yield return () => new TestCaseData(library, move);
|
|
}
|
|
}
|
|
|
|
[Test, MethodDataSource(nameof(AllMovesHaveValidScriptsData))]
|
|
public async Task AllMoveEffectsHaveValidScripts(TestCaseData test)
|
|
{
|
|
if (test.Move.SecondaryEffect == null)
|
|
return;
|
|
var scriptName = test.Move.SecondaryEffect.Name;
|
|
|
|
try
|
|
{
|
|
await Assert.That(test.Library.ScriptResolver.TryResolve(ScriptCategory.Move, scriptName,
|
|
test.Move.SecondaryEffect.Parameters, out _)).IsTrue();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// Helper method to find the line number of the effect in the JSON file
|
|
var file = Path.GetFullPath("../../../../Plugins/PkmnLib.Plugin.Gen7/Data/Moves.jsonc");
|
|
var json = await File.ReadAllLinesAsync(file);
|
|
var moveLineNumber = json.Select((line, index) => new { line, index })
|
|
.FirstOrDefault(x => x.line.Contains($"\"name\": \"{test.Move.Name}\""))?.index + 1;
|
|
var effectLineNumber = moveLineNumber + json.Skip(moveLineNumber ?? 0)
|
|
.Select((line, index) => new { line, index }).FirstOrDefault(x => x.line.Contains("effect"))
|
|
?.index +
|
|
1 ?? 0;
|
|
|
|
await TestContext.Current!.OutputWriter.WriteLineAsync("File: " + $"file://{file}:{effectLineNumber}");
|
|
throw new AggregateException($"Failed to resolve script for move {test.Move.Name} with effect {scriptName}",
|
|
e);
|
|
}
|
|
}
|
|
|
|
public record SetStatusTestCaseData(IDynamicLibrary Library, IMoveData Move)
|
|
{
|
|
/// <inheritdoc />
|
|
public override string ToString() => Move.Name + " has valid status: " +
|
|
Move.SecondaryEffect?.Parameters.GetValueOrDefault("status");
|
|
}
|
|
|
|
public static IEnumerable<Func<SetStatusTestCaseData>> SetStatusMovesHaveValidStatusData()
|
|
{
|
|
var library = LibraryHelpers.LoadLibrary();
|
|
var moveLibrary = library.StaticLibrary.Moves;
|
|
foreach (var move in moveLibrary)
|
|
{
|
|
if (move.SecondaryEffect?.Name != "set_status")
|
|
continue;
|
|
yield return () => new SetStatusTestCaseData(library, move);
|
|
}
|
|
}
|
|
|
|
[Test, MethodDataSource(nameof(SetStatusMovesHaveValidStatusData))]
|
|
public async Task SetStatusMovesHaveValidStatus(SetStatusTestCaseData test)
|
|
{
|
|
if (test.Move.SecondaryEffect == null)
|
|
return;
|
|
var status = test.Move.SecondaryEffect.Parameters["status"]?.ToString();
|
|
if (status == null)
|
|
throw new Exception("Missing required parameter 'status'");
|
|
|
|
await Assert.That(test.Library.ScriptResolver.TryResolve(ScriptCategory.Status, status, null, out _)).IsTrue();
|
|
}
|
|
} |