using System.Text.Json.Nodes; using PkmnLib.Dynamic.Libraries; using PkmnLib.Dynamic.ScriptHandling; using PkmnLib.Static.Moves; using PkmnLib.Tests.Integration; using TUnit.Core.Logging; namespace PkmnLib.Tests.DataTests; public class MoveDataTests { public record TestCaseData(IDynamicLibrary Library, IMoveData Move) { /// public override string ToString() => Move.Name + " has valid scripts"; } public static IEnumerable> 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("../../../../PkmnLib.Tests/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); } } }