From 9417ee6be13d3c930de0acb8af719491f3b4e52e Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 3 Apr 2023 14:13:32 +0100 Subject: [PATCH] test: bring coverage to 100% for Drawing --- X10D.Tests/src/Drawing/PolygonFTests.cs | 43 ++++++++++++++++ X10D.Tests/src/Drawing/PolygonTests.cs | 62 ++++++++++++++++++++++- X10D.Tests/src/Drawing/PolyhedronTests.cs | 27 ++++++++++ X10D/src/Drawing/Polygon.cs | 25 ++++++++- X10D/src/Drawing/PolygonF.cs | 22 +++++++- 5 files changed, 175 insertions(+), 4 deletions(-) diff --git a/X10D.Tests/src/Drawing/PolygonFTests.cs b/X10D.Tests/src/Drawing/PolygonFTests.cs index ab5655e4f..6f7cb99d1 100644 --- a/X10D.Tests/src/Drawing/PolygonFTests.cs +++ b/X10D.Tests/src/Drawing/PolygonFTests.cs @@ -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 vertices = null!; + Assert.ThrowsException(() => polygon.AddVertices(vertices)); + } + + [TestMethod] + public void AddVertices_ShouldThrowArgumentNullException_GivenNullEnumerableOfVector2() + { + var polygon = PolygonF.Empty; + IEnumerable vertices = null!; + Assert.ThrowsException(() => polygon.AddVertices(vertices)); + } + [TestMethod] public void ClearVertices_ShouldClearVertices() { @@ -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 vertices = null!; + Assert.ThrowsException(() => new PolygonF(vertices)); + } + + [TestMethod] + public void Constructor_ShouldThrowArgumentNullException_GivenNullEnumerableOfVector2() + { + IEnumerable vertices = null!; + Assert.ThrowsException(() => new PolygonF(vertices)); + } [TestMethod] public void CopyConstructor_ShouldCopyVertices_GivenPolygon() @@ -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(() => new PolygonF(polygon)); + } + [TestMethod] public void Equals_ShouldBeTrue_GivenTwoEmptyPolygons() { @@ -109,6 +146,12 @@ public void Equals_ShouldBeFalse_GivenHexagonAndEmptyPolygon() Assert.IsTrue(second != first); } + [TestMethod] + public void FromPolygon_ShouldThrowArgumentNullException_GivenNullPolygonF() + { + Assert.ThrowsException(() => PolygonF.FromPolygon(null!)); + } + [TestMethod] public void IsConvex_ShouldBeFalse_GivenEmptyPolygon() { diff --git a/X10D.Tests/src/Drawing/PolygonTests.cs b/X10D.Tests/src/Drawing/PolygonTests.cs index 639eec9cc..f8d55ae80 100644 --- a/X10D.Tests/src/Drawing/PolygonTests.cs +++ b/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; @@ -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 vertices = null!; + Assert.ThrowsException(() => polygon.AddVertices(vertices)); + } + [TestMethod] public void ClearVertices_ShouldClearVertices() { @@ -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 vertices = null!; + Assert.ThrowsException(() => new Polygon(vertices)); + } + [TestMethod] public void CopyConstructor_ShouldCopyVertices_GivenPolygon() { @@ -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(() => new Polygon(polygon)); + } + [TestMethod] public void Equals_ShouldBeTrue_GivenTwoEmptyPolygons() { @@ -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(() => Polygon.FromPolygonF(null!)); + } + [TestMethod] public void IsConvex_ShouldBeFalse_GivenEmptyPolygon() { @@ -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(); diff --git a/X10D.Tests/src/Drawing/PolyhedronTests.cs b/X10D.Tests/src/Drawing/PolyhedronTests.cs index 9d72ec030..f45ea1bf8 100644 --- a/X10D.Tests/src/Drawing/PolyhedronTests.cs +++ b/X10D.Tests/src/Drawing/PolyhedronTests.cs @@ -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 vertices = null!; + Assert.ThrowsException(() => polygon.AddVertices(vertices)); + } + [TestMethod] public void ClearVertices_ShouldClearVertices() { @@ -39,6 +47,13 @@ public void Constructor_ShouldPopulateVertices_GivenPolyhedron() Assert.AreEqual(2, polyhedron.VertexCount); } + [TestMethod] + public void Constructor_ShouldThrowArgumentNullException_GivenNullEnumerableOfVector3() + { + IEnumerable vertices = null!; + Assert.ThrowsException(() => new Polyhedron(vertices)); + } + [TestMethod] public void CopyConstructor_ShouldCopyVertices_GivenPolyhedron() { @@ -105,6 +120,18 @@ public void Equals_ShouldBeFalse_GivenHexagonAndEmptyPolyhedron() Assert.IsTrue(second != first); } + [TestMethod] + public void FromPolygon_ShouldThrowArgumentNullException_GivenNullPolygonF() + { + Assert.ThrowsException(() => Polyhedron.FromPolygon(null!)); + } + + [TestMethod] + public void FromPolygonF_ShouldThrowArgumentNullException_GivenNullPolygonF() + { + Assert.ThrowsException(() => Polyhedron.FromPolygonF(null!)); + } + [TestMethod] public void op_Implicit_ShouldReturnEquivalentPolyhedron_GivenPolyhedron() { diff --git a/X10D/src/Drawing/Polygon.cs b/X10D/src/Drawing/Polygon.cs index f6564ad11..63abae8b3 100644 --- a/X10D/src/Drawing/Polygon.cs +++ b/X10D/src/Drawing/Polygon.cs @@ -21,8 +21,22 @@ public Polygon() /// Initializes a new instance of the class by copying the specified polygon. /// 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(); + for (var index = 0; index < polygon._vertices.Count; index++) + { + Point vertex = polygon._vertices[index]; + _vertices.Add(vertex); + } } /// @@ -31,6 +45,15 @@ public Polygon(Polygon polygon) /// An enumerable collection of vertices from which the polygon should be constructed. public Polygon(IEnumerable vertices) { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(vertices); +#else + if (vertices is null) + { + throw new ArgumentNullException(nameof(vertices)); + } +#endif + _vertices = new List(vertices); } diff --git a/X10D/src/Drawing/PolygonF.cs b/X10D/src/Drawing/PolygonF.cs index 560f984a6..51d9eebe9 100644 --- a/X10D/src/Drawing/PolygonF.cs +++ b/X10D/src/Drawing/PolygonF.cs @@ -24,8 +24,21 @@ public PolygonF() /// /// is . 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(); + for (var index = 0; index < polygon._vertices.Count; index++) + { + PointF vertex = polygon._vertices[index]; + _vertices.Add(vertex); + } } /// @@ -34,7 +47,6 @@ public PolygonF(PolygonF polygon) /// An enumerable collection of vertices from which the polygon should be constructed. /// is . public PolygonF(IEnumerable vertices) - : this(vertices.Select(p => p.ToPointF())) { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(vertices); @@ -44,6 +56,12 @@ public PolygonF(IEnumerable vertices) throw new ArgumentNullException(nameof(vertices)); } #endif + + _vertices = new List(); + foreach (Vector2 vertex in vertices) + { + _vertices.Add(vertex.ToPointF()); + } } ///