Files
QuestPDF/QuestPDF.UnitTests/TestEngine/TestPlan.cs
T

298 lines
11 KiB
C#
Raw Normal View History

2021-01-16 01:31:39 +01:00
using System;
using System.Collections.Generic;
using System.Text.Json;
2021-09-18 23:40:21 +02:00
using FluentAssertions;
2021-01-16 01:31:39 +01:00
using NUnit.Framework;
2021-09-02 21:48:20 +02:00
using QuestPDF.Drawing;
2021-04-09 19:10:55 +02:00
using QuestPDF.Elements;
2021-08-25 03:40:16 +02:00
using QuestPDF.Helpers;
2021-10-01 02:32:35 +02:00
using QuestPDF.Infrastructure;
using QuestPDF.UnitTests.TestEngine.Operations;
2021-01-16 01:31:39 +01:00
namespace QuestPDF.UnitTests.TestEngine
2021-01-16 01:31:39 +01:00
{
internal class TestPlan
{
private const string DefaultChildName = "child";
2021-09-18 23:40:21 +02:00
private static Random Random { get; } = new Random();
2021-01-16 01:31:39 +01:00
private Element Element { get; set; }
private ICanvas Canvas { get; }
private Size OperationInput { get; set; }
private Queue<OperationBase> Operations { get; } = new Queue<OperationBase>();
2021-01-16 01:31:39 +01:00
public TestPlan()
{
Canvas = CreateCanvas();
}
public static TestPlan For(Func<TestPlan, Element> create)
{
var plan = new TestPlan();
plan.Element = create(plan);
return plan;
}
private T GetExpected<T>() where T : OperationBase
2021-01-16 01:31:39 +01:00
{
if (Operations.TryDequeue(out var value) && value is T result)
return result;
var gotType = value?.GetType()?.Name ?? "null";
Assert.Fail($"Expected: {typeof(T).Name}, got {gotType}: {JsonSerializer.Serialize(value)}");
return null;
}
private ICanvas CreateCanvas()
{
2021-09-18 23:40:21 +02:00
return new MockCanvas
2021-01-16 01:31:39 +01:00
{
TranslateFunc = position =>
{
2021-09-18 23:40:21 +02:00
var expected = GetExpected<CanvasTranslateOperation>();
Assert.AreEqual(expected.Position.X, position.X, "Translate X");
Assert.AreEqual(expected.Position.Y, position.Y, "Translate Y");
2021-01-16 01:31:39 +01:00
},
RotateFunc = angle =>
{
var expected = GetExpected<CanvasRotateOperation>();
Assert.AreEqual(expected.Angle, angle, "Rotate angle");
},
ScaleFunc = (scaleX, scaleY) =>
{
var expected = GetExpected<CanvasScaleOperation>();
Assert.AreEqual(expected.ScaleX, scaleX, "Scale X");
Assert.AreEqual(expected.ScaleY, scaleY, "Scale Y");
2021-01-16 01:31:39 +01:00
},
DrawRectFunc = (position, size, color) =>
{
2021-09-18 23:40:21 +02:00
var expected = GetExpected<CanvasDrawRectangleOperation>();
2021-01-16 01:31:39 +01:00
Assert.AreEqual(expected.Position.X, position.X, "Draw rectangle: X");
Assert.AreEqual(expected.Position.Y, position.Y, "Draw rectangle: Y");
2021-01-16 01:31:39 +01:00
Assert.AreEqual(expected.Size.Width, size.Width, "Draw rectangle: width");
Assert.AreEqual(expected.Size.Height, size.Height, "Draw rectangle: height");
2021-01-16 01:31:39 +01:00
Assert.AreEqual(expected.Color, color, "Draw rectangle: color");
2021-01-16 01:31:39 +01:00
},
DrawTextFunc = (text, position, style) =>
{
2021-09-18 23:40:21 +02:00
var expected = GetExpected<CanvasDrawTextOperation>();
2021-01-16 01:31:39 +01:00
Assert.AreEqual(expected.Text, text);
Assert.AreEqual(expected.Position.X, position.X, "Draw text: X");
Assert.AreEqual(expected.Position.Y, position.Y, "Draw text: Y");
2021-01-16 01:31:39 +01:00
Assert.AreEqual(expected.Style.Color, style.Color, "Draw text: color");
Assert.AreEqual(expected.Style.FontType, style.FontType, "Draw text: font");
Assert.AreEqual(expected.Style.Size, style.Size, "Draw text: size");
2021-01-16 01:31:39 +01:00
},
DrawImageFunc = (image, position, size) =>
{
2021-09-18 23:40:21 +02:00
var expected = GetExpected<CanvasDrawImageOperation>();
2021-01-16 01:31:39 +01:00
Assert.AreEqual(expected.Position.X, position.X, "Draw image: X");
Assert.AreEqual(expected.Position.Y, position.Y, "Draw image: Y");
2021-01-16 01:31:39 +01:00
Assert.AreEqual(expected.Size.Width, size.Width, "Draw image: width");
Assert.AreEqual(expected.Size.Height, size.Height, "Draw image: height");
2021-01-16 01:31:39 +01:00
}
};
}
public Element CreateChild() => CreateChild(DefaultChildName);
2021-01-16 01:31:39 +01:00
public Element CreateChild(string id)
{
return new ElementMock
{
Id = id,
MeasureFunc = availableSpace =>
{
2021-09-18 23:40:21 +02:00
var expected = GetExpected<ChildMeasureOperation>();
2021-01-16 01:31:39 +01:00
Assert.AreEqual(expected.ChildId, id);
Assert.AreEqual(expected.Input.Width, availableSpace.Width, $"Measure: width of child '{expected.ChildId}'");
Assert.AreEqual(expected.Input.Height, availableSpace.Height, $"Measure: height of child '{expected.ChildId}'");
2021-01-16 01:31:39 +01:00
return expected.Output;
},
DrawFunc = availableSpace =>
{
2021-09-18 23:40:21 +02:00
var expected = GetExpected<ChildDrawOperation>();
2021-01-16 01:31:39 +01:00
Assert.AreEqual(expected.ChildId, id);
Assert.AreEqual(expected.Input.Width, availableSpace.Width, $"Draw: width of child '{expected.ChildId}'");
Assert.AreEqual(expected.Input.Height, availableSpace.Height, $"Draw: width of child '{expected.ChildId}'");
2021-01-16 01:31:39 +01:00
}
};
}
public TestPlan MeasureElement(Size input)
{
OperationInput = input;
return this;
}
public TestPlan DrawElement(Size input)
{
OperationInput = input;
return this;
}
private TestPlan AddOperation(OperationBase operationBase)
2021-01-16 01:31:39 +01:00
{
Operations.Enqueue(operationBase);
2021-01-16 01:31:39 +01:00
return this;
}
2021-09-02 21:48:20 +02:00
public TestPlan ExpectChildMeasure(Size expectedInput, SpacePlan returns)
{
return ExpectChildMeasure(DefaultChildName, expectedInput, returns);
}
2021-09-02 21:48:20 +02:00
public TestPlan ExpectChildMeasure(string child, Size expectedInput, SpacePlan returns)
2021-01-16 01:31:39 +01:00
{
2021-09-18 23:40:21 +02:00
return AddOperation(new ChildMeasureOperation(child, expectedInput, returns));
2021-01-16 01:31:39 +01:00
}
public TestPlan ExpectChildDraw(Size expectedInput)
{
return ExpectChildDraw(DefaultChildName, expectedInput);
}
2021-01-16 01:31:39 +01:00
public TestPlan ExpectChildDraw(string child, Size expectedInput)
{
2021-09-18 23:40:21 +02:00
return AddOperation(new ChildDrawOperation(child, expectedInput));
2021-01-16 01:31:39 +01:00
}
public TestPlan ExpectCanvasTranslate(Position position)
{
2021-09-18 23:40:21 +02:00
return AddOperation(new CanvasTranslateOperation(position));
2021-01-16 01:31:39 +01:00
}
public TestPlan ExpectCanvasTranslate(float left, float top)
{
2021-09-18 23:40:21 +02:00
return AddOperation(new CanvasTranslateOperation(new Position(left, top)));
2021-01-16 01:31:39 +01:00
}
public TestPlan ExpectCanvasScale(float scaleX, float scaleY)
{
return AddOperation(new CanvasScaleOperation(scaleX, scaleY));
}
public TestPlan ExpectCanvasRotate(float angle)
{
return AddOperation(new CanvasRotateOperation(angle));
}
2021-01-16 01:31:39 +01:00
public TestPlan ExpectCanvasDrawRectangle(Position position, Size size, string color)
{
2021-09-18 23:40:21 +02:00
return AddOperation(new CanvasDrawRectangleOperation(position, size, color));
2021-01-16 01:31:39 +01:00
}
public TestPlan ExpectCanvasDrawText(string text, Position position, TextStyle style)
{
2021-09-18 23:40:21 +02:00
return AddOperation(new CanvasDrawTextOperation(text, position, style));
2021-01-16 01:31:39 +01:00
}
public TestPlan ExpectCanvasDrawImage(Position position, Size size)
{
2021-09-18 23:40:21 +02:00
return AddOperation(new CanvasDrawImageOperation(position, size));
2021-01-16 01:31:39 +01:00
}
2021-09-02 21:48:20 +02:00
public TestPlan CheckMeasureResult(SpacePlan expected)
2021-01-16 01:31:39 +01:00
{
2021-07-26 12:26:31 +02:00
Element.HandleVisitor(x => x?.Initialize(null, Canvas));
2021-01-16 01:31:39 +01:00
var actual = Element.Measure(OperationInput);
Assert.AreEqual(expected.GetType(), actual.GetType());
2021-09-02 21:48:20 +02:00
Assert.AreEqual(expected.Width, actual.Width, "Measure: width");
Assert.AreEqual(expected.Height, actual.Height, "Measure: height");
Assert.AreEqual(expected.Type, actual.Type, "Measure: height");
2021-01-16 01:31:39 +01:00
return this;
}
public TestPlan CheckDrawResult()
{
2021-07-26 12:26:31 +02:00
Element.HandleVisitor(x => x?.Initialize(null, Canvas));
2021-07-26 11:59:48 +02:00
Element.Draw(OperationInput);
2021-01-16 01:31:39 +01:00
return this;
}
2021-04-09 19:10:55 +02:00
public TestPlan CheckState(Func<Element, bool> condition)
{
Assert.IsTrue(condition(Element), "Checking condition");
return this;
}
public TestPlan CheckState<T>(Func<T, bool> condition) where T : Element
{
Assert.IsTrue(Element is T);
Assert.IsTrue(condition(Element as T), "Checking condition");
return this;
}
2021-04-09 19:10:55 +02:00
public static Element CreateUniqueElement()
{
2021-09-30 22:48:39 +02:00
return new Constrained
2021-04-09 19:10:55 +02:00
{
2021-09-30 22:48:39 +02:00
MinWidth = 90,
MinHeight = 60,
Child = new DynamicImage
{
Source = Placeholders.Image
}
2021-04-09 19:10:55 +02:00
};
}
2021-09-18 23:40:21 +02:00
public static void CompareOperations(Element value, Element expected, Size? availableSpace = null)
{
CompareMeasureOperations(value, expected, availableSpace);
CompareDrawOperations(value, expected, availableSpace);
}
private static void CompareMeasureOperations(Element value, Element expected, Size? availableSpace = null)
{
availableSpace ??= new Size(400, 300);
var canvas = new FreeCanvas();
value.HandleVisitor(x => x.Initialize(null, canvas));
2021-10-01 02:32:35 +02:00
var valueMeasure = value.Measure(availableSpace.Value);
2021-09-18 23:40:21 +02:00
expected.HandleVisitor(x => x.Initialize(null, canvas));
2021-10-01 02:32:35 +02:00
var expectedMeasure = expected.Measure(availableSpace.Value);
2021-09-18 23:40:21 +02:00
valueMeasure.Should().BeEquivalentTo(expectedMeasure);
}
private static void CompareDrawOperations(Element value, Element expected, Size? availableSpace = null)
{
availableSpace ??= new Size(400, 300);
var valueCanvas = new OperationRecordingCanvas();
value.HandleVisitor(x => x.Initialize(null, valueCanvas));
2021-10-01 02:32:35 +02:00
value.Draw(availableSpace.Value);
2021-09-18 23:40:21 +02:00
var expectedCanvas = new OperationRecordingCanvas();
expected.HandleVisitor(x => x.Initialize(null, expectedCanvas));
2021-10-01 02:32:35 +02:00
expected.Draw(availableSpace.Value);
2021-09-18 23:40:21 +02:00
valueCanvas.Operations.Should().BeEquivalentTo(expectedCanvas.Operations);
}
2021-01-16 01:31:39 +01:00
}
}