1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:25:43 +00:00

test: bring coverage to 100% for Drawing

This commit is contained in:
Oliver Booth 2023-04-03 14:13:32 +01:00
parent 8b4fd45e05
commit 9417ee6be1
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
5 changed files with 175 additions and 4 deletions

View File

@ -19,6 +19,22 @@ public class PolygonFTests
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()
{
@ -42,6 +58,20 @@ public class PolygonFTests
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()
@ -61,6 +91,13 @@ public class PolygonFTests
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()
{
@ -109,6 +146,12 @@ public class PolygonFTests
Assert.IsTrue(second != first);
}
[TestMethod]
public void FromPolygon_ShouldThrowArgumentNullException_GivenNullPolygonF()
{
Assert.ThrowsException<ArgumentNullException>(() => PolygonF.FromPolygon(null!));
}
[TestMethod]
public void IsConvex_ShouldBeFalse_GivenEmptyPolygon()
{

View File

@ -1,4 +1,5 @@
using System.Drawing;
using System.Drawing;
using System.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Drawing;
@ -19,6 +20,14 @@ public class PolygonTests
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()
{
@ -33,6 +42,21 @@ public class PolygonTests
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()
{
@ -51,6 +75,13 @@ public class PolygonTests
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()
{
@ -99,6 +130,23 @@ public class PolygonTests
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()
{
@ -155,6 +203,18 @@ public class PolygonTests
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();

View File

@ -18,6 +18,14 @@ public class PolyhedronTests
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()
{
@ -39,6 +47,13 @@ public class PolyhedronTests
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()
{
@ -105,6 +120,18 @@ public class PolyhedronTests
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()
{

View File

@ -21,8 +21,22 @@ public class Polygon : IEquatable<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>
@ -31,6 +45,15 @@ public class Polygon : IEquatable<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);
}

View File

@ -24,8 +24,21 @@ public class 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>
@ -34,7 +47,6 @@ public class PolygonF
/// <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);
@ -44,6 +56,12 @@ public class PolygonF
throw new ArgumentNullException(nameof(vertices));
}
#endif
_vertices = new List<PointF>();
foreach (Vector2 vertex in vertices)
{
_vertices.Add(vertex.ToPointF());
}
}
/// <summary>