TestPlan.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using NUnit.Framework;
  5. using QuestPDF.Drawing.SpacePlan;
  6. using QuestPDF.Elements;
  7. using QuestPDF.Helpers;
  8. using QuestPDF.Infrastructure;
  9. using QuestPDF.UnitTests.TestEngine.Operations;
  10. namespace QuestPDF.UnitTests.TestEngine
  11. {
  12. internal class TestPlan
  13. {
  14. private const string DefaultChildName = "child";
  15. private Element Element { get; set; }
  16. private ICanvas Canvas { get; }
  17. private Size OperationInput { get; set; }
  18. private Queue<OperationBase> Operations { get; } = new Queue<OperationBase>();
  19. public TestPlan()
  20. {
  21. Canvas = CreateCanvas();
  22. }
  23. public static TestPlan For(Func<TestPlan, Element> create)
  24. {
  25. var plan = new TestPlan();
  26. plan.Element = create(plan);
  27. return plan;
  28. }
  29. private T GetExpected<T>() where T : OperationBase
  30. {
  31. if (Operations.TryDequeue(out var value) && value is T result)
  32. return result;
  33. var gotType = value?.GetType()?.Name ?? "null";
  34. Assert.Fail($"Expected: {typeof(T).Name}, got {gotType}: {JsonSerializer.Serialize(value)}");
  35. return null;
  36. }
  37. private ICanvas CreateCanvas()
  38. {
  39. return new CanvasMock
  40. {
  41. TranslateFunc = position =>
  42. {
  43. var expected = GetExpected<CanvasTranslateOperationBase>();
  44. Assert.AreEqual(expected.Position.X, position.X, "Translate X");
  45. Assert.AreEqual(expected.Position.Y, position.Y, "Translate Y");
  46. //position.Should().BeEquivalentTo(expected.Position);
  47. },
  48. DrawRectFunc = (position, size, color) =>
  49. {
  50. var expected = GetExpected<CanvasDrawRectangleOperationBase>();
  51. Assert.AreEqual(expected.Position.X, position.X, "Draw rectangle: X");
  52. Assert.AreEqual(expected.Position.Y, position.Y, "Draw rectangle: Y");
  53. Assert.AreEqual(expected.Size.Width, size.Width, "Draw rectangle: width");
  54. Assert.AreEqual(expected.Size.Height, size.Height, "Draw rectangle: height");
  55. Assert.AreEqual(expected.Color, color, "Draw rectangle: color");
  56. /*position.Should().BeEquivalentTo(expected.Position);
  57. size.Should().BeEquivalentTo(expected.Size);
  58. color.Should().Be(expected.Color);*/
  59. },
  60. DrawTextFunc = (text, position, style) =>
  61. {
  62. var expected = GetExpected<CanvasDrawTextOperationBase>();
  63. Assert.AreEqual(expected.Text, text);
  64. Assert.AreEqual(expected.Position.X, position.X, "Draw text: X");
  65. Assert.AreEqual(expected.Position.Y, position.Y, "Draw text: Y");
  66. Assert.AreEqual(expected.Style.Color, style.Color, "Draw text: color");
  67. Assert.AreEqual(expected.Style.FontType, style.FontType, "Draw text: font");
  68. Assert.AreEqual(expected.Style.Size, style.Size, "Draw text: size");
  69. /*text.Should().Be(expected.Text);
  70. position.Should().BeEquivalentTo(expected.Position);
  71. style.Should().BeEquivalentTo(expected.Style);*/
  72. },
  73. DrawImageFunc = (image, position, size) =>
  74. {
  75. var expected = GetExpected<CanvasDrawImageOperationBase>();
  76. Assert.AreEqual(expected.Position.X, position.X, "Draw image: X");
  77. Assert.AreEqual(expected.Position.Y, position.Y, "Draw image: Y");
  78. Assert.AreEqual(expected.Size.Width, size.Width, "Draw image: width");
  79. Assert.AreEqual(expected.Size.Height, size.Height, "Draw image: height");
  80. /*position.Should().BeEquivalentTo(expected.Position);
  81. size.Should().BeEquivalentTo(expected.Size);*/
  82. }
  83. };
  84. }
  85. public Element CreateChild() => CreateChild(DefaultChildName);
  86. public Element CreateChild(string id)
  87. {
  88. return new ElementMock
  89. {
  90. Id = id,
  91. MeasureFunc = availableSpace =>
  92. {
  93. var expected = GetExpected<ChildMeasureOperationBase>();
  94. Assert.AreEqual(expected.ChildId, id);
  95. Assert.AreEqual(expected.Input.Width, availableSpace.Width, $"Measure: width of child '{expected.ChildId}'");
  96. Assert.AreEqual(expected.Input.Height, availableSpace.Height, $"Measure: height of child '{expected.ChildId}'");
  97. // id.Should().Be(expected.ChildId);
  98. // availableSpace.Should().Be(expected.Input);
  99. return expected.Output;
  100. },
  101. DrawFunc = availableSpace =>
  102. {
  103. var expected = GetExpected<ChildDrawOperationBase>();
  104. Assert.AreEqual(expected.ChildId, id);
  105. Assert.AreEqual(expected.Input.Width, availableSpace.Width, $"Draw: width of child '{expected.ChildId}'");
  106. Assert.AreEqual(expected.Input.Height, availableSpace.Height, $"Draw: width of child '{expected.ChildId}'");
  107. /*id.Should().Be(expected.ChildId);
  108. availableSpace.Should().Be(expected.Input);*/
  109. }
  110. };
  111. }
  112. public TestPlan MeasureElement(Size input)
  113. {
  114. OperationInput = input;
  115. return this;
  116. }
  117. public TestPlan DrawElement(Size input)
  118. {
  119. OperationInput = input;
  120. return this;
  121. }
  122. private TestPlan AddOperation(OperationBase operationBase)
  123. {
  124. Operations.Enqueue(operationBase);
  125. return this;
  126. }
  127. public TestPlan ExpectChildMeasure(Size expectedInput, ISpacePlan returns)
  128. {
  129. return ExpectChildMeasure(DefaultChildName, expectedInput, returns);
  130. }
  131. public TestPlan ExpectChildMeasure(string child, Size expectedInput, ISpacePlan returns)
  132. {
  133. return AddOperation(new ChildMeasureOperationBase(child, expectedInput, returns));
  134. }
  135. public TestPlan ExpectChildDraw(Size expectedInput)
  136. {
  137. return ExpectChildDraw(DefaultChildName, expectedInput);
  138. }
  139. public TestPlan ExpectChildDraw(string child, Size expectedInput)
  140. {
  141. return AddOperation(new ChildDrawOperationBase(child, expectedInput));
  142. }
  143. public TestPlan ExpectCanvasTranslate(Position position)
  144. {
  145. return AddOperation(new CanvasTranslateOperationBase(position));
  146. }
  147. public TestPlan ExpectCanvasTranslate(float left, float top)
  148. {
  149. return AddOperation(new CanvasTranslateOperationBase(new Position(left, top)));
  150. }
  151. public TestPlan ExpectCanvasDrawRectangle(Position position, Size size, string color)
  152. {
  153. return AddOperation(new CanvasDrawRectangleOperationBase(position, size, color));
  154. }
  155. public TestPlan ExpectCanvasDrawText(string text, Position position, TextStyle style)
  156. {
  157. return AddOperation(new CanvasDrawTextOperationBase(text, position, style));
  158. }
  159. public TestPlan ExpectCanvasDrawImage(Position position, Size size)
  160. {
  161. return AddOperation(new CanvasDrawImageOperationBase(position, size));
  162. }
  163. public TestPlan CheckMeasureResult(ISpacePlan expected)
  164. {
  165. Element.HandleVisitor(x => x?.Initialize(null, Canvas));
  166. var actual = Element.Measure(OperationInput);
  167. Assert.AreEqual(expected.GetType(), actual.GetType());
  168. var expectedSize = expected as Size;
  169. var actualSize = actual as Size;
  170. if (expectedSize != null)
  171. {
  172. Assert.AreEqual(expectedSize.Width, actualSize.Width, "Measure: width");
  173. Assert.AreEqual(expectedSize.Height, actualSize.Height, "Measure: height");
  174. }
  175. return this;
  176. }
  177. public TestPlan CheckDrawResult()
  178. {
  179. Element.HandleVisitor(x => x?.Initialize(null, Canvas));
  180. Element.Draw(OperationInput);
  181. return this;
  182. }
  183. public TestPlan CheckState(Func<Element, bool> condition)
  184. {
  185. Assert.IsTrue(condition(Element), "Checking condition");
  186. return this;
  187. }
  188. public TestPlan CheckState<T>(Func<T, bool> condition) where T : Element
  189. {
  190. Assert.IsTrue(Element is T);
  191. Assert.IsTrue(condition(Element as T), "Checking condition");
  192. return this;
  193. }
  194. public static Element CreateUniqueElement()
  195. {
  196. return new DynamicImage
  197. {
  198. Source = Placeholders.Image
  199. };
  200. }
  201. }
  202. }