Skip to content

Commit 970a42e

Browse files
More Path extensions tests
1 parent 24386f0 commit 970a42e

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

tests/ImageSharp.Drawing.Tests/Shapes/PathExtentionTests.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ public class PathExtentionTests
1010
{
1111
private RectangleF bounds;
1212
private readonly Mock<IPath> mockPath;
13+
private readonly Mock<IPathCollection> mockPathCollection;
1314

1415
public PathExtentionTests()
1516
{
1617
this.bounds = new RectangleF(10, 10, 20, 20);
1718
this.mockPath = new Mock<IPath>();
1819
this.mockPath.Setup(x => x.Bounds).Returns(() => this.bounds);
20+
this.mockPathCollection = new Mock<IPathCollection>();
21+
this.mockPathCollection.Setup(x => x.Bounds).Returns(() => this.bounds);
1922
}
2023

2124
[Fact]
@@ -96,4 +99,105 @@ public void TranslateXY()
9699

97100
this.mockPath.Verify(p => p.Transform(It.IsAny<Matrix4x4>()), Times.Once);
98101
}
102+
103+
[Fact]
104+
public void TranslatePoint()
105+
{
106+
PointF point = new(98, 120);
107+
Matrix4x4 targetMatrix = Matrix4x4.CreateTranslation(point.X, point.Y, 0);
108+
109+
this.VerifyPathTransform(path => path.Translate(point), targetMatrix);
110+
}
111+
112+
[Fact]
113+
public void ScaleXY()
114+
{
115+
Matrix4x4 targetMatrix = Matrix4x4.CreateScale(2, 3, 1, new Vector3(RectangleF.Center(this.bounds), 0));
116+
117+
this.VerifyPathTransform(path => path.Scale(2, 3), targetMatrix);
118+
}
119+
120+
[Fact]
121+
public void ScaleUniform()
122+
{
123+
Matrix4x4 targetMatrix = Matrix4x4.CreateScale(4, 4, 1, new Vector3(RectangleF.Center(this.bounds), 0));
124+
125+
this.VerifyPathTransform(path => path.Scale(4), targetMatrix);
126+
}
127+
128+
[Fact]
129+
public void CollectionRotateInRadians()
130+
{
131+
const float Angle = (float)Math.PI;
132+
Matrix4x4 targetMatrix = new(Matrix3x2.CreateRotation(Angle, RectangleF.Center(this.bounds)));
133+
134+
this.VerifyPathCollectionTransform(path => path.Rotate(Angle), targetMatrix);
135+
}
136+
137+
[Fact]
138+
public void CollectionRotateInDegrees()
139+
{
140+
const float Angle = 90;
141+
const float Radians = (float)(Math.PI * Angle / 180.0);
142+
Matrix4x4 targetMatrix = new(Matrix3x2.CreateRotation(Radians, RectangleF.Center(this.bounds)));
143+
144+
this.VerifyPathCollectionTransform(path => path.RotateDegree(Angle), targetMatrix);
145+
}
146+
147+
[Fact]
148+
public void CollectionTranslatePoint()
149+
{
150+
PointF point = new(98, 120);
151+
Matrix4x4 targetMatrix = Matrix4x4.CreateTranslation(point.X, point.Y, 0);
152+
153+
this.VerifyPathCollectionTransform(path => path.Translate(point), targetMatrix);
154+
}
155+
156+
[Fact]
157+
public void CollectionTranslateXY()
158+
{
159+
const float X = 76;
160+
const float Y = 7;
161+
Matrix4x4 targetMatrix = Matrix4x4.CreateTranslation(X, Y, 0);
162+
163+
this.VerifyPathCollectionTransform(path => path.Translate(X, Y), targetMatrix);
164+
}
165+
166+
[Fact]
167+
public void CollectionScaleXY()
168+
{
169+
Matrix4x4 targetMatrix = Matrix4x4.CreateScale(2, 3, 1, new Vector3(RectangleF.Center(this.bounds), 0));
170+
171+
this.VerifyPathCollectionTransform(path => path.Scale(2, 3), targetMatrix);
172+
}
173+
174+
[Fact]
175+
public void CollectionScaleUniform()
176+
{
177+
Matrix4x4 targetMatrix = Matrix4x4.CreateScale(4, 4, 1, new Vector3(RectangleF.Center(this.bounds), 0));
178+
179+
this.VerifyPathCollectionTransform(path => path.Scale(4), targetMatrix);
180+
}
181+
182+
private void VerifyPathTransform(Action<IPath> transform, Matrix4x4 targetMatrix)
183+
{
184+
this.mockPath.Setup(x => x.Transform(It.IsAny<Matrix4x4>()))
185+
.Callback<Matrix4x4>(m => Assert.Equal(targetMatrix, m))
186+
.Returns(this.mockPath.Object);
187+
188+
transform(this.mockPath.Object);
189+
190+
this.mockPath.Verify(x => x.Transform(It.IsAny<Matrix4x4>()), Times.Once);
191+
}
192+
193+
private void VerifyPathCollectionTransform(Action<IPathCollection> transform, Matrix4x4 targetMatrix)
194+
{
195+
this.mockPathCollection.Setup(x => x.Transform(It.IsAny<Matrix4x4>()))
196+
.Callback<Matrix4x4>(m => Assert.Equal(targetMatrix, m))
197+
.Returns(this.mockPathCollection.Object);
198+
199+
transform(this.mockPathCollection.Object);
200+
201+
this.mockPathCollection.Verify(x => x.Transform(It.IsAny<Matrix4x4>()), Times.Once);
202+
}
99203
}

tests/ImageSharp.Drawing.Tests/Shapes/PathTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,56 @@ public void EmptyPath_ToLinearGeometry_ReturnsEmptyGeometry()
8585

8686
Assert.False(segments.MoveNext());
8787
}
88+
89+
[Fact]
90+
public void PathCollection_EnumerableConstructor_PreservesPaths()
91+
{
92+
IPath first = new RectangularPolygon(1, 2, 3, 4);
93+
IPath second = new RectangularPolygon(10, 20, 5, 6);
94+
95+
PathCollection collection = new(new[] { first, second }.AsEnumerable());
96+
97+
IPath[] paths = collection.ToArray();
98+
Assert.Equal(2, paths.Length);
99+
Assert.Same(first, paths[0]);
100+
Assert.Same(second, paths[1]);
101+
}
102+
103+
[Fact]
104+
public void PathCollection_Bounds_AggregatesPathBounds()
105+
{
106+
PathCollection collection = new(
107+
new RectangularPolygon(1, 2, 3, 4),
108+
new RectangularPolygon(10, 20, 5, 6));
109+
110+
Assert.Equal(new RectangleF(1, 2, 14, 24), collection.Bounds);
111+
}
112+
113+
[Fact]
114+
public void PathCollection_Bounds_ReturnsEmptyForEmptyCollection()
115+
{
116+
PathCollection collection = new();
117+
118+
Assert.Equal(RectangleF.Empty, collection.Bounds);
119+
}
120+
121+
[Fact]
122+
public void PathCollection_Transform_TransformsEachPath()
123+
{
124+
PathCollection collection = new(
125+
new RectangularPolygon(1, 2, 3, 4),
126+
new RectangularPolygon(10, 20, 5, 6));
127+
Matrix4x4 matrix = Matrix4x4.CreateTranslation(7, 11, 0);
128+
129+
IPathCollection transformed = collection.Transform(matrix);
130+
131+
Assert.NotSame(collection, transformed);
132+
Assert.Equal(new RectangleF(8, 13, 14, 24), transformed.Bounds);
133+
Assert.Equal(new RectangleF(1, 2, 14, 24), collection.Bounds);
134+
135+
RectangleF[] transformedBounds = transformed.Select(x => x.Bounds).ToArray();
136+
Assert.Equal(2, transformedBounds.Length);
137+
Assert.Equal(new RectangleF(8, 13, 3, 4), transformedBounds[0]);
138+
Assert.Equal(new RectangleF(17, 31, 5, 6), transformedBounds[1]);
139+
}
88140
}

0 commit comments

Comments
 (0)