SemanticTreeTestRunner.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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? actualRoot, SemanticTreeNode? expectedRoot)
  16. {
  17. if (expectedRoot == null && actualRoot == null)
  18. return;
  19. if (expectedRoot == null)
  20. {
  21. Assert.Fail($"Expected null but got node of type '{actualRoot?.Type}'");
  22. return;
  23. }
  24. if (actualRoot == null)
  25. {
  26. Assert.Fail($"Expected node of type '{expectedRoot.Type}' but got null");
  27. return;
  28. }
  29. var currentPath = new Stack<string>();
  30. try
  31. {
  32. Compare(actualRoot, expectedRoot);
  33. }
  34. catch
  35. {
  36. var pathText = string.Join(" -> ", currentPath.Reverse());
  37. Console.WriteLine("Problem location");
  38. Console.WriteLine(pathText);
  39. throw;
  40. }
  41. void Compare(SemanticTreeNode actual, SemanticTreeNode expected)
  42. {
  43. if (!currentPath.Any())
  44. currentPath.Push(actual.Type);
  45. Assert.That(actual.Type, Is.EqualTo(expected.Type), "Type mismatch");
  46. Assert.That(actual.Alt, Is.EqualTo(expected.Alt), "Alt mismatch");
  47. Assert.That(actual.Lang, Is.EqualTo(expected.Lang), "Lang mismatch");
  48. CompareAttributes();
  49. CompareChildren();
  50. void CompareChildren()
  51. {
  52. Assert.That(actual.Children.Count, Is.EqualTo(expected.Children.Count), "Children count mismatch");
  53. var hasMultipleChildren = actual.Children.Count > 1;
  54. foreach (var (actualChild, expectedChild) in actual.Children.Zip(expected.Children))
  55. {
  56. var prefix = hasMultipleChildren ? $"{actual.Children.IndexOf(actualChild)}:" : "";
  57. currentPath.Push(prefix + actualChild.Type);
  58. Compare(actualChild, expectedChild);
  59. currentPath.Pop();
  60. }
  61. }
  62. void CompareAttributes()
  63. {
  64. Assert.That(actual.Attributes.Count, Is.EqualTo(expected.Attributes.Count), "Attribute count mismatch");
  65. var actualList = actual.Attributes.OrderBy(a => a.Owner).ThenBy(a => a.Name);
  66. var expectedList = expected.Attributes.OrderBy(a => a.Owner).ThenBy(a => a.Name);
  67. foreach (var (actualAttribute, expectedAttribute) in actualList.Zip(expectedList))
  68. {
  69. Assert.That(actualAttribute.Owner, Is.EqualTo(expectedAttribute.Owner), "Attribute owner mismatch");
  70. Assert.That(actualAttribute.Name, Is.EqualTo(expectedAttribute.Name), "Attribute name mismatch");
  71. Assert.That(actualAttribute.Value, Is.EqualTo(expectedAttribute.Value), $"Attribute value mismatch for '{expectedAttribute.Owner}:{expectedAttribute.Name}");
  72. }
  73. }
  74. }
  75. }
  76. }
  77. internal static class ExpectedSemanticTree
  78. {
  79. public static SemanticTreeNode DocumentRoot(Action<SemanticTreeNode> configuration)
  80. {
  81. var root = new SemanticTreeNode
  82. {
  83. Type = "Document"
  84. };
  85. configuration(root);
  86. return root;
  87. }
  88. public static void Child(this SemanticTreeNode parent, string type, Action<SemanticTreeNode>? configuration = null)
  89. {
  90. var child = new SemanticTreeNode
  91. {
  92. Type = type
  93. };
  94. configuration?.Invoke(child);
  95. parent.Children.Add(child);
  96. }
  97. public static SemanticTreeNode Attribute(this SemanticTreeNode node, string owner, string name, string value)
  98. {
  99. var attribute = new SemanticTreeNode.Attribute
  100. {
  101. Owner = owner,
  102. Name = name,
  103. Value = value
  104. };
  105. node.Attributes.Add(attribute);
  106. return node;
  107. }
  108. public static SemanticTreeNode Alt(this SemanticTreeNode node, string alt)
  109. {
  110. node.Alt = alt;
  111. return node;
  112. }
  113. public static SemanticTreeNode Lang(this SemanticTreeNode node, string lang)
  114. {
  115. node.Lang = lang;
  116. return node;
  117. }
  118. }