TestPlan.cs 9.3 KB

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