Skip to content

Commit 3bbe8c3

Browse files
Update UnityDataTool.Tests/SerializedFileCommandTests.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 9f29721 commit 3bbe8c3

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

‎UnityDataTool.Tests/SerializedFileCommandTests.cs‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,134 @@ public async Task Metadata_LegacyVersion_ReturnsError()
492492
}
493493
}
494494

495+
[Test]
496+
public async Task Metadata_TextOutput_SucceedsAndContainsExpectedFields()
497+
{
498+
// Use PlayerWithTypeTrees test data, which is known to be a supported SerializedFile
499+
// with TypeTrees enabled.
500+
var dataDir = Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", "PlayerWithTypeTrees");
501+
var serializedFilePath = Path.Combine(dataDir, "globalgamemanagers");
502+
503+
if (!File.Exists(serializedFilePath))
504+
{
505+
Assert.Ignore("PlayerWithTypeTrees serialized file not found");
506+
return;
507+
}
508+
509+
using var sw = new StringWriter();
510+
var currentOut = Console.Out;
511+
try
512+
{
513+
Console.SetOut(sw);
514+
515+
var result = await Program.Main(new string[]
516+
{
517+
"serialized-file",
518+
"metadata",
519+
serializedFilePath
520+
});
521+
522+
Assert.AreEqual(0, result, "Metadata command should succeed for supported file");
523+
524+
var output = sw.ToString();
525+
526+
// Basic sanity checks on text output; exact formatting is left flexible,
527+
// but the output should mention metadata and TypeTree-related information.
528+
StringAssert.Contains("Metadata", output, "Text output should mention metadata");
529+
StringAssert.Contains("Type", output, "Text output should mention type information");
530+
StringAssert.Contains("Tree", output, "Text output should mention TypeTree information");
531+
}
532+
finally
533+
{
534+
Console.SetOut(currentOut);
535+
}
536+
}
537+
538+
[Test]
539+
public async Task Metadata_JsonOutput_SucceedsAndContainsTypeTreeInfo()
540+
{
541+
// Use PlayerWithTypeTrees test data to validate JSON metadata output,
542+
// including TypeTree-related properties.
543+
var dataDir = Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", "PlayerWithTypeTrees");
544+
var serializedFilePath = Path.Combine(dataDir, "globalgamemanagers");
545+
546+
if (!File.Exists(serializedFilePath))
547+
{
548+
Assert.Ignore("PlayerWithTypeTrees serialized file not found");
549+
return;
550+
}
551+
552+
using var sw = new StringWriter();
553+
var currentOut = Console.Out;
554+
try
555+
{
556+
Console.SetOut(sw);
557+
558+
var result = await Program.Main(new string[]
559+
{
560+
"serialized-file",
561+
"metadata",
562+
serializedFilePath,
563+
"-f",
564+
"json"
565+
});
566+
567+
Assert.AreEqual(0, result, "Metadata command should succeed for supported file in JSON mode");
568+
569+
var output = sw.ToString();
570+
Assert.IsNotEmpty(output, "JSON output should not be empty");
571+
572+
using var doc = JsonDocument.Parse(output);
573+
var root = doc.RootElement;
574+
575+
Assert.AreEqual(JsonValueKind.Object, root.ValueKind, "Root JSON element should be an object");
576+
577+
// Validate the presence of some core metadata properties.
578+
Assert.IsTrue(root.TryGetProperty("filePath", out _)
579+
|| root.TryGetProperty("path", out _),
580+
"JSON metadata should contain a file path property");
581+
582+
Assert.IsTrue(root.TryGetProperty("unityVersion", out _)
583+
|| root.TryGetProperty("version", out _),
584+
"JSON metadata should contain a Unity version property");
585+
586+
// Validate TypeTree-related information: either a count or an array of TypeTrees.
587+
JsonElement typeTreesElement;
588+
bool hasTypeTrees =
589+
root.TryGetProperty("typeTrees", out typeTreesElement) ||
590+
root.TryGetProperty("typeTree", out typeTreesElement) ||
591+
root.TryGetProperty("typeTreeInfos", out typeTreesElement);
592+
593+
Assert.IsTrue(hasTypeTrees, "JSON metadata should contain TypeTree information");
594+
595+
if (typeTreesElement.ValueKind == JsonValueKind.Array)
596+
{
597+
Assert.Greater(typeTreesElement.GetArrayLength(), 0, "TypeTree array should contain at least one entry");
598+
599+
var first = typeTreesElement[0];
600+
Assert.AreEqual(JsonValueKind.Object, first.ValueKind, "Each TypeTree entry should be an object");
601+
602+
// Check for some typical fields on a TypeTree entry (IDs/counts/arrays).
603+
Assert.IsTrue(first.TryGetProperty("typeId", out _)
604+
|| first.TryGetProperty("classId", out _),
605+
"TypeTree entry should contain a type or class ID");
606+
607+
Assert.IsTrue(first.TryGetProperty("nodes", out var nodesElement)
608+
? nodesElement.ValueKind == JsonValueKind.Array
609+
: true,
610+
"If present, TypeTree nodes should be an array");
611+
}
612+
else if (typeTreesElement.ValueKind == JsonValueKind.Number)
613+
{
614+
// Some formats may expose only a count.
615+
Assert.Greater(typeTreesElement.GetInt32(), 0, "TypeTree count should be greater than zero");
616+
}
617+
}
618+
finally
619+
{
620+
Console.SetOut(currentOut);
621+
}
622+
}
495623
#endregion
496624

497625
#region Cross-Validation with Analyze Command

0 commit comments

Comments
 (0)