using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using YamlDotNet.Core; using YamlDotNet.RepresentationModel; using YamlDotNet.Serialization; namespace ReFuel.Gltf.Tests; [DebuggerDisplay("{Kind,nq} path={DocumentUnderTestPath}")] public class GltfTestDocument(FileInfo documentUnderTestPath) : IDisposable { public FileInfo DocumentUnderTestPath { get; } = documentUnderTestPath; public FileInfo? SummaryDocumentFile { get; init; } = FindSummary(documentUnderTestPath); public DirectoryInfo WorkingDirectory { get; init; } = documentUnderTestPath.Directory ?? new DirectoryInfo(Environment.CurrentDirectory); public GltfDocumentKind Kind { get; init; } = documentUnderTestPath.Extension.ToLowerInvariant() switch { ".gltf" => GltfDocumentKind.Json, ".glb" => GltfDocumentKind.Binary, _ => GltfDocumentKind.Any, }; public FileStream? Stream { get; private set; } public GltfDocument? DocumentUnderTest { get; private set; } public YamlDocument? SummaryDocument { get; private set; } public ValidationDocument? ValidationDocument { get; private set; } public bool HasValidation => SummaryDocumentFile != null; public GltfDocument Parse() { Stream = DocumentUnderTestPath.OpenRead(); return DocumentUnderTest = GltfDocument.Open(Stream); } [MemberNotNullWhen(true, nameof(ValidationDocument))] public bool LoadValidationDocument() { if (SummaryDocumentFile == null) return false; using StreamReader reader = new StreamReader(SummaryDocumentFile.OpenRead()); ValidationDocument = Deserializer.Deserialize(reader.ReadToEnd()); return true; } public void Dispose() { DocumentUnderTest?.Dispose(); DocumentUnderTest = null; Stream?.Dispose(); Stream = null; } private static FileInfo? FindSummary(FileInfo documentUnderTest) { return documentUnderTest.Directory? .EnumerateFiles() .FirstOrDefault(x => string.Equals(x.Name, "conversion.yaml", StringComparison.InvariantCultureIgnoreCase)); } public override string ToString() { string kind = Kind switch { GltfDocumentKind.Json => "Gltf JSON", GltfDocumentKind.Binary => "Gltf Binary", _ => "Unknown Gltf", }; return $"{kind} \"{DocumentUnderTestPath.Name}\" HasValidation={(HasValidation ? "Yes" : "No")}"; } private static IDeserializer Deserializer { get; } static GltfTestDocument() { Deserializer = new DeserializerBuilder() .IgnoreUnmatchedProperties() .Build(); } }