Tweaks for JSON loading, minor refactor of unit tests
This commit is contained in:
parent
51dfc4d07e
commit
58e9f4c3d1
|
@ -15,17 +15,15 @@ public static class AbilityDataLoader
|
|||
{
|
||||
private static Dictionary<string, SerializedAbility> LoadAbilitiesData(Stream stream)
|
||||
{
|
||||
var obj = JsonSerializer.Deserialize<JsonObject>(stream);
|
||||
var obj = JsonSerializer.Deserialize<JsonObject>(stream, JsonOptions.DefaultOptions);
|
||||
if (obj == null)
|
||||
throw new InvalidDataException("Ability data is empty.");
|
||||
obj.Remove("$schema");
|
||||
var cleanedString = obj.ToJsonString();
|
||||
|
||||
var objects = JsonSerializer.Deserialize<Dictionary<string, SerializedAbility>>(cleanedString,
|
||||
new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
var objects =
|
||||
JsonSerializer.Deserialize<Dictionary<string, SerializedAbility>>(cleanedString,
|
||||
JsonOptions.DefaultOptions);
|
||||
if (objects == null)
|
||||
throw new InvalidDataException("Ability data is empty.");
|
||||
return objects;
|
||||
|
|
|
@ -10,7 +10,7 @@ public static class GrowthRateDataLoader
|
|||
{
|
||||
public static GrowthRateLibrary LoadGrowthRates(Stream stream)
|
||||
{
|
||||
var objects = JsonSerializer.Deserialize<Dictionary<string, uint[]>>(stream)!;
|
||||
var objects = JsonSerializer.Deserialize<Dictionary<string, uint[]>>(stream, JsonOptions.DefaultOptions)!;
|
||||
var library = new GrowthRateLibrary();
|
||||
foreach (var (key, value) in objects)
|
||||
{
|
||||
|
|
|
@ -17,10 +17,7 @@ public static class ItemDataLoader
|
|||
public static ItemLibrary LoadItems(Stream stream)
|
||||
{
|
||||
var library = new ItemLibrary();
|
||||
var obj = JsonSerializer.Deserialize<SerializedItem[]>(stream, new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
var obj = JsonSerializer.Deserialize<SerializedItem[]>(stream, JsonOptions.DefaultOptions);
|
||||
if (obj == null)
|
||||
throw new InvalidDataException("Item data is empty.");
|
||||
var items = obj.Select(DeserializeItem);
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
using System.Text.Json;
|
||||
|
||||
namespace PkmnLib.Dataloader;
|
||||
|
||||
internal static class JsonOptions
|
||||
{
|
||||
public static JsonSerializerOptions DefaultOptions => new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
AllowTrailingCommas = true,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
};
|
||||
}
|
|
@ -17,11 +17,7 @@ public static class MoveDataLoader
|
|||
public static MoveLibrary LoadMoves(Stream stream, TypeLibrary typeLibrary)
|
||||
{
|
||||
var library = new MoveLibrary();
|
||||
var objects = JsonSerializer.Deserialize<SerializedMoveDataWrapper>(stream,
|
||||
new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
var objects = JsonSerializer.Deserialize<SerializedMoveDataWrapper>(stream, JsonOptions.DefaultOptions);
|
||||
if (objects == null)
|
||||
throw new InvalidDataException("Move data is empty.");
|
||||
var moves = objects.Data.Select(x => DeserializeMove(x, typeLibrary));
|
||||
|
|
|
@ -17,15 +17,11 @@ public static class SpeciesDataLoader
|
|||
{
|
||||
private static Dictionary<string, SerializedSpecies> LoadSpeciesData(Stream stream)
|
||||
{
|
||||
var obj = JsonSerializer.Deserialize<JsonObject>(stream);
|
||||
var obj = JsonSerializer.Deserialize<JsonObject>(stream, JsonOptions.DefaultOptions);
|
||||
if (obj == null)
|
||||
throw new InvalidDataException("Species data is empty.");
|
||||
var jsonConfig = new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
};
|
||||
return obj.Where(x => x.Key != "$schema")
|
||||
.ToDictionary(x => x.Key, x => x.Value.Deserialize<SerializedSpecies>(jsonConfig));
|
||||
.ToDictionary(x => x.Key, x => x.Value.Deserialize<SerializedSpecies>(JsonOptions.DefaultOptions));
|
||||
}
|
||||
|
||||
public static SpeciesLibrary LoadSpecies(Stream[] streams, IReadOnlyTypeLibrary typeLibrary)
|
||||
|
|
|
@ -3614,6 +3614,7 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
// Done up to here
|
||||
{
|
||||
"name": "fire_spin",
|
||||
"type": "fire",
|
||||
|
|
|
@ -1,12 +1,22 @@
|
|||
using PkmnLib.Dynamic.Libraries;
|
||||
using PkmnLib.Dynamic.ScriptHandling;
|
||||
using PkmnLib.Static.Moves;
|
||||
using PkmnLib.Tests.Integration;
|
||||
|
||||
namespace PkmnLib.Tests.DataTests;
|
||||
|
||||
public class MoveDataTests
|
||||
{
|
||||
[Test]
|
||||
public async Task AllMoveEffectsHaveValidScripts()
|
||||
public record TestCaseData(IDynamicLibrary Library, IMoveData Move)
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return Move.Name + " has valid scripts";
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<TestCaseData> AllMovesHaveValidScriptsData()
|
||||
{
|
||||
var library = LibraryHelpers.LoadLibrary();
|
||||
var moveLibrary = library.StaticLibrary.Moves;
|
||||
|
@ -14,17 +24,26 @@ public class MoveDataTests
|
|||
{
|
||||
if (move.SecondaryEffect == null)
|
||||
continue;
|
||||
var scriptName = move.SecondaryEffect.Name;
|
||||
yield return new TestCaseData(library, move);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await Assert.That(library.ScriptResolver.TryResolve(ScriptCategory.Move, scriptName,
|
||||
move.SecondaryEffect.Parameters, out var script)).IsTrue();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new AggregateException($"Failed to resolve script for move {move.Name} with effect {scriptName}", e);
|
||||
}
|
||||
[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 var script)).IsTrue();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new AggregateException($"Failed to resolve script for move {test.Move.Name} with effect {scriptName}", e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,6 +15,8 @@ public class IntegrationTestRunner
|
|||
var serializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
AllowTrailingCommas = true,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
};
|
||||
foreach (var file in files)
|
||||
{
|
||||
|
|
|
@ -6,6 +6,12 @@ public class IntegrationTestModel
|
|||
public string Description { get; set; } = null!;
|
||||
public IntegrationTestBattleSetup BattleSetup { get; set; } = null!;
|
||||
public IntegrationTestAction[] Actions { get; set; } = null!;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
|
||||
public class IntegrationTestBattleSetup
|
||||
|
|
|
@ -2,6 +2,7 @@ using PkmnLib.Plugin.Gen7.Scripts.Pokemon;
|
|||
|
||||
namespace PkmnLib.Plugin.Gen7.Scripts.Moves;
|
||||
|
||||
[Script(ScriptCategory.Move, "fire_fang")]
|
||||
public class FireFang : Script
|
||||
{
|
||||
/// <inheritdoc />
|
||||
|
|
Loading…
Reference in New Issue