Add prototype serialization tests. (#18458)

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2023-08-06 14:47:45 +12:00
committed by GitHub
parent b97be440dd
commit 28a5e32f5e
18 changed files with 138 additions and 53 deletions

View File

@@ -0,0 +1,52 @@
using Robust.Shared.Prototypes;
using Robust.UnitTesting;
namespace Content.IntegrationTests.Tests.PrototypeTests;
public sealed class PrototypeTests
{
/// <summary>
/// This test writes all known prototypes as yaml files, then validates that the result is valid yaml.
/// Can help prevent instances where prototypes have bad C# default values.
/// </summary>
[Test]
public async Task TestAllPrototypesAreSerializable()
{
await using var pairTracker = await PoolManager.GetServerClient();
var context = new PrototypeSaveTest.TestEntityUidContext();
Assert.Multiple(() =>
{
Validate(pairTracker.Pair.Server, "server", context);
// TODO fix client serialization
//Validate(pairTracker.Pair.Client, "client", context);
});
await pairTracker.CleanReturnAsync();
}
public void Validate(RobustIntegrationTest.IntegrationInstance instance, string instanceId,
PrototypeSaveTest.TestEntityUidContext ctx)
{
var protoMan = instance.ResolveDependency<IPrototypeManager>();
var errors = protoMan.ValidateAllPrototypesSerializable(ctx);
if (errors.Count == 0)
return;
Assert.Multiple(() =>
{
foreach (var (kind, ids) in errors)
{
foreach (var (id, nodes) in ids)
{
var msg = $"Error when validating {instanceId} prototype ({kind.Name}, {id}). Errors: \n";
foreach (var errorNode in nodes)
{
msg += $" - {errorNode.ErrorReason}\n";
}
Assert.Fail(msg);
}
}
});
}
}