Skip to content

Commit

Permalink
test: bring coverage to 100% for Drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Apr 3, 2023
1 parent 8b4fd45 commit 9417ee6
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 4 deletions.
43 changes: 43 additions & 0 deletions X10D.Tests/src/Drawing/PolygonFTests.cs
Expand Up @@ -19,6 +19,22 @@ public void AddVertices_ShouldAddVertices()
Assert.AreEqual(0, PolygonF.Empty.VertexCount);
}

[TestMethod]
public void AddVertices_ShouldThrowArgumentNullException_GivenNullEnumerableOfPointF()
{
var polygon = PolygonF.Empty;
IEnumerable<PointF> vertices = null!;
Assert.ThrowsException<ArgumentNullException>(() => polygon.AddVertices(vertices));
}

[TestMethod]
public void AddVertices_ShouldThrowArgumentNullException_GivenNullEnumerableOfVector2()
{
var polygon = PolygonF.Empty;
IEnumerable<Vector2> vertices = null!;
Assert.ThrowsException<ArgumentNullException>(() => polygon.AddVertices(vertices));
}

[TestMethod]
public void ClearVertices_ShouldClearVertices()
{
Expand All @@ -42,6 +58,20 @@ public void Constructor_ShouldPopulateVertices_GivenPolygon()
Assert.AreEqual(2, pointPolygon.VertexCount);
Assert.AreEqual(2, vectorPolygon.VertexCount);
}

[TestMethod]
public void Constructor_ShouldThrowArgumentNullException_GivenNullEnumerableOfPointF()
{
IEnumerable<PointF> vertices = null!;
Assert.ThrowsException<ArgumentNullException>(() => new PolygonF(vertices));
}

[TestMethod]
public void Constructor_ShouldThrowArgumentNullException_GivenNullEnumerableOfVector2()
{
IEnumerable<Vector2> vertices = null!;
Assert.ThrowsException<ArgumentNullException>(() => new PolygonF(vertices));
}

[TestMethod]
public void CopyConstructor_ShouldCopyVertices_GivenPolygon()
Expand All @@ -61,6 +91,13 @@ public void CopyConstructor_ShouldCopyVertices_GivenPolygon()
Assert.AreEqual(0, PolygonF.Empty.VertexCount);
}

[TestMethod]
public void CopyConstructor_ShouldThrowArgumentNullException_GivenNullPolygonF()
{
PolygonF polygon = null!;
Assert.ThrowsException<ArgumentNullException>(() => new PolygonF(polygon));
}

[TestMethod]
public void Equals_ShouldBeTrue_GivenTwoEmptyPolygons()
{
Expand Down Expand Up @@ -109,6 +146,12 @@ public void Equals_ShouldBeFalse_GivenHexagonAndEmptyPolygon()
Assert.IsTrue(second != first);
}

[TestMethod]
public void FromPolygon_ShouldThrowArgumentNullException_GivenNullPolygonF()
{
Assert.ThrowsException<ArgumentNullException>(() => PolygonF.FromPolygon(null!));
}

[TestMethod]
public void IsConvex_ShouldBeFalse_GivenEmptyPolygon()
{
Expand Down
62 changes: 61 additions & 1 deletion X10D.Tests/src/Drawing/PolygonTests.cs
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Drawing;
using System.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Drawing;

Expand All @@ -19,6 +20,14 @@ public void AddVertices_ShouldAddVertices()
Assert.AreEqual(0, Polygon.Empty.VertexCount);
}

[TestMethod]
public void AddVertices_ShouldThrowArgumentNullException_GivenNullEnumerable()
{
var polygon = Polygon.Empty;
IEnumerable<Point> vertices = null!;
Assert.ThrowsException<ArgumentNullException>(() => polygon.AddVertices(vertices));
}

[TestMethod]
public void ClearVertices_ShouldClearVertices()
{
Expand All @@ -33,6 +42,21 @@ public void ClearVertices_ShouldClearVertices()
Assert.AreEqual(0, polygon.VertexCount);
}

[TestMethod]
public void Constructor_ShouldPopulateVertices_GivenPolygon()
{
var pointPolygon = new Polygon(new[] {new Point(1, 2), new Point(3, 4)});

Assert.AreEqual(2, pointPolygon.VertexCount);
}

[TestMethod]
public void Constructor_ShouldThrowArgumentNullException_GivenNullEnumerableOfPoint()
{
IEnumerable<Point> vertices = null!;
Assert.ThrowsException<ArgumentNullException>(() => new Polygon(vertices));
}

[TestMethod]
public void CopyConstructor_ShouldCopyVertices_GivenPolygon()
{
Expand All @@ -51,6 +75,13 @@ public void CopyConstructor_ShouldCopyVertices_GivenPolygon()
Assert.AreEqual(0, Polygon.Empty.VertexCount);
}

[TestMethod]
public void CopyConstructor_ShouldThrowArgumentNullException_GivenNullPolygon()
{
Polygon polygon = null!;
Assert.ThrowsException<ArgumentNullException>(() => new Polygon(polygon));
}

[TestMethod]
public void Equals_ShouldBeTrue_GivenTwoEmptyPolygons()
{
Expand Down Expand Up @@ -99,6 +130,23 @@ public void Equals_ShouldBeFalse_GivenHexagonAndEmptyPolygon()
Assert.IsTrue(second != first);
}

[TestMethod]
public void FromPolygonF_ShouldReturnEquivalentPolygon_GivenPolygon()
{
PolygonF hexagon = CreateHexagonF();

Polygon expected = CreateHexagon();
Polygon actual = Polygon.FromPolygonF(hexagon);

Assert.AreEqual(expected, actual);
}

[TestMethod]
public void FromPolygonF_ShouldThrowArgumentNullException_GivenNullPolygon()
{
Assert.ThrowsException<ArgumentNullException>(() => Polygon.FromPolygonF(null!));
}

[TestMethod]
public void IsConvex_ShouldBeFalse_GivenEmptyPolygon()
{
Expand Down Expand Up @@ -155,6 +203,18 @@ internal static Polygon CreateHexagon()
return hexagon;
}

internal static PolygonF CreateHexagonF()
{
var hexagon = new PolygonF();
hexagon.AddVertex(new PointF(0, 0));
hexagon.AddVertex(new PointF(1, 0));
hexagon.AddVertex(new PointF(1, 1));
hexagon.AddVertex(new PointF(0, 1));
hexagon.AddVertex(new PointF(-1, 1));
hexagon.AddVertex(new PointF(-1, 0));
return hexagon;
}

internal static Polygon CreateConcavePolygon()
{
var hexagon = new Polygon();
Expand Down
27 changes: 27 additions & 0 deletions X10D.Tests/src/Drawing/PolyhedronTests.cs
Expand Up @@ -18,6 +18,14 @@ public void AddVertices_ShouldAddVertices()
Assert.AreEqual(0, Polyhedron.Empty.VertexCount);
}

[TestMethod]
public void AddVertices_ShouldThrowArgumentNullException_GivenNullEnumerableOfVector3()
{
var polygon = Polyhedron.Empty;
IEnumerable<Vector3> vertices = null!;
Assert.ThrowsException<ArgumentNullException>(() => polygon.AddVertices(vertices));
}

[TestMethod]
public void ClearVertices_ShouldClearVertices()
{
Expand All @@ -39,6 +47,13 @@ public void Constructor_ShouldPopulateVertices_GivenPolyhedron()
Assert.AreEqual(2, polyhedron.VertexCount);
}

[TestMethod]
public void Constructor_ShouldThrowArgumentNullException_GivenNullEnumerableOfVector3()
{
IEnumerable<Vector3> vertices = null!;
Assert.ThrowsException<ArgumentNullException>(() => new Polyhedron(vertices));
}

[TestMethod]
public void CopyConstructor_ShouldCopyVertices_GivenPolyhedron()
{
Expand Down Expand Up @@ -105,6 +120,18 @@ public void Equals_ShouldBeFalse_GivenHexagonAndEmptyPolyhedron()
Assert.IsTrue(second != first);
}

[TestMethod]
public void FromPolygon_ShouldThrowArgumentNullException_GivenNullPolygonF()
{
Assert.ThrowsException<ArgumentNullException>(() => Polyhedron.FromPolygon(null!));
}

[TestMethod]
public void FromPolygonF_ShouldThrowArgumentNullException_GivenNullPolygonF()
{
Assert.ThrowsException<ArgumentNullException>(() => Polyhedron.FromPolygonF(null!));
}

[TestMethod]
public void op_Implicit_ShouldReturnEquivalentPolyhedron_GivenPolyhedron()
{
Expand Down
25 changes: 24 additions & 1 deletion X10D/src/Drawing/Polygon.cs
Expand Up @@ -21,8 +21,22 @@ public Polygon()
/// Initializes a new instance of the <see cref="Polygon" /> class by copying the specified polygon.
/// </summary>
public Polygon(Polygon polygon)
: this(polygon?._vertices ?? throw new ArgumentNullException(nameof(polygon)))
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(polygon);
#else
if (polygon is null)
{
throw new ArgumentNullException(nameof(polygon));
}
#endif

_vertices = new List<Point>();
for (var index = 0; index < polygon._vertices.Count; index++)
{
Point vertex = polygon._vertices[index];
_vertices.Add(vertex);
}
}

/// <summary>
Expand All @@ -31,6 +45,15 @@ public Polygon(Polygon polygon)
/// <param name="vertices">An enumerable collection of vertices from which the polygon should be constructed.</param>
public Polygon(IEnumerable<Point> vertices)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(vertices);
#else
if (vertices is null)
{
throw new ArgumentNullException(nameof(vertices));
}
#endif

_vertices = new List<Point>(vertices);
}

Expand Down
22 changes: 20 additions & 2 deletions X10D/src/Drawing/PolygonF.cs
Expand Up @@ -24,8 +24,21 @@ public PolygonF()
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="polygon" /> is <see langword="null" />.</exception>
public PolygonF(PolygonF polygon)
: this(polygon?._vertices ?? throw new ArgumentNullException(nameof(polygon)))
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(polygon);
#else
if (polygon is null)
{
throw new ArgumentNullException(nameof(polygon));
}
#endif
_vertices = new List<PointF>();
for (var index = 0; index < polygon._vertices.Count; index++)
{
PointF vertex = polygon._vertices[index];
_vertices.Add(vertex);
}
}

/// <summary>
Expand All @@ -34,7 +47,6 @@ public PolygonF(PolygonF polygon)
/// <param name="vertices">An enumerable collection of vertices from which the polygon should be constructed.</param>
/// <exception cref="ArgumentNullException"><paramref name="vertices" /> is <see langword="null" />.</exception>
public PolygonF(IEnumerable<Vector2> vertices)
: this(vertices.Select(p => p.ToPointF()))
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(vertices);
Expand All @@ -44,6 +56,12 @@ public PolygonF(IEnumerable<Vector2> vertices)
throw new ArgumentNullException(nameof(vertices));
}
#endif

_vertices = new List<PointF>();
foreach (Vector2 vertex in vertices)
{
_vertices.Add(vertex.ToPointF());
}
}

/// <summary>
Expand Down

0 comments on commit 9417ee6

Please sign in to comment.