SemanticTreeTestRunner.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using QuestPDF.Drawing;
  2. using QuestPDF.Infrastructure;
  3. namespace QuestPDF.ConformanceTests.TestEngine;
  4. internal static class SemanticTreeTestRunner
  5. {
  6. public static void TestSemanticTree(this IDocument document, SemanticTreeNode? semanticTreeRootNode)
  7. {
  8. Settings.EnableCaching = false;
  9. Settings.EnableDebugging = false;
  10. var canvas = new SemanticAwareDocumentCanvas();
  11. var settings = new DocumentSettings { PDFA_Conformance = PDFA_Conformance.PDFA_3A };
  12. DocumentGenerator.RenderDocument(canvas, document, settings);
  13. CompareSemanticTrees(canvas.SemanticTree, semanticTreeRootNode);
  14. }
  15. private static void CompareSemanticTrees(SemanticTreeNode? actual, SemanticTreeNode? expected)
  16. {
  17. if (expected == null && actual == null)
  18. return;
  19. if (expected == null)
  20. Assert.Fail($"Expected null but got node of type '{actual?.Type}'");
  21. if (actual == null)
  22. Assert.Fail($"Expected node of type '{expected.Type}' but got null");
  23. Assert.That(actual.Type, Is.EqualTo(expected.Type), $"Node type mismatch");
  24. Assert.That(actual.Alt, Is.EqualTo(expected.Alt), $"Alt mismatch for node type '{expected.Type}'");
  25. Assert.That(actual.Lang, Is.EqualTo(expected.Lang), $"Lang mismatch for node type '{expected.Type}'");
  26. CompareAttributes(actual.Attributes, expected.Attributes, expected.Type);
  27. Assert.That(actual.Children.Count, Is.EqualTo(expected.Children.Count), $"Children count mismatch for node type '{expected.Type}'");
  28. foreach (var (actualChild, expectedChild) in actual.Children.Zip(expected.Children))
  29. CompareSemanticTrees(actualChild, expectedChild);
  30. static void CompareAttributes(ICollection<SemanticTreeNode.Attribute> actual, ICollection<SemanticTreeNode.Attribute> expected, string nodeType)
  31. {
  32. Assert.That(actual.Count, Is.EqualTo(expected.Count), $"Attribute count mismatch for node type '{nodeType}'");
  33. var actualList = actual.OrderBy(a => a.Owner).ThenBy(a => a.Name).ToList();
  34. var expectedList = expected.OrderBy(a => a.Owner).ThenBy(a => a.Name).ToList();
  35. for (var i = 0; i < expectedList.Count; i++)
  36. {
  37. var actualAttr = actualList[i];
  38. var expectedAttr = expectedList[i];
  39. Assert.That(actualAttr.Owner, Is.EqualTo(expectedAttr.Owner), $"Attribute owner mismatch for node type '{nodeType}'");
  40. Assert.That(actualAttr.Name, Is.EqualTo(expectedAttr.Name), $"Attribute name mismatch for node type '{nodeType}'");
  41. Assert.That(actualAttr.Value, Is.EqualTo(expectedAttr.Value), $"Attribute value mismatch for '{expectedAttr.Owner}:{expectedAttr.Name}' in node type '{nodeType}'");
  42. }
  43. }
  44. }
  45. }