diff --git a/.idea/.idea.Shapes/.idea/contentModel.xml b/.idea/.idea.Shapes/.idea/contentModel.xml index 0b9b503..fff3ada 100644 --- a/.idea/.idea.Shapes/.idea/contentModel.xml +++ b/.idea/.idea.Shapes/.idea/contentModel.xml @@ -1,12 +1,12 @@ - - - - - - + + + + + + @@ -50,6 +50,7 @@ + diff --git a/Shapes/Circle.cs b/Shapes/Circle.cs index f82a601..d18c86d 100644 --- a/Shapes/Circle.cs +++ b/Shapes/Circle.cs @@ -1,14 +1,20 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.Serialization; +using System.Runtime.CompilerServices; + +[assembly:InternalsVisibleTo("UnitTests")] namespace Shapes { [DataContract] public class Circle : GeometricShape { + internal IFileIO _fileWriter; + /** * Constructor with x-y Location for center * @@ -19,6 +25,7 @@ namespace Shapes */ public Circle(double x, double y, double radius) { + _fileWriter = new FileIO(); Stroke = Color.Black; Validator.ValidatePositiveDouble(radius, "Invalid radius"); CenterPoint = new Point(x, y); @@ -36,10 +43,9 @@ namespace Shapes */ public Circle(Point center, double radius) { + _fileWriter = new FileIO(); Stroke = Color.Black; Validator.ValidatePositiveDouble(radius, "Invalid radius"); - if (center == null) - throw new ShapeException("Invalid center point"); CenterPoint = center; Radius = radius; Height = radius * 2; @@ -58,6 +64,7 @@ namespace Shapes [DataMember] public override double Width { get; internal set; } + [ExcludeFromCodeCoverage] internal override void ComputeCenter() { //Does nothing to a circle @@ -80,10 +87,10 @@ namespace Shapes public override void Save(Stream stream) { - var fileWriter = new FileIO(); - fileWriter.SaveShape(stream, this); + _fileWriter.SaveShape(stream, this); } + [ExcludeFromCodeCoverage] public override void Draw(Stream stream) { var tmp = new Bitmap((int) Radius * 4, (int) Radius * 4); diff --git a/Shapes/Rectangle.cs b/Shapes/Rectangle.cs index 0ce7aac..71d21e9 100644 --- a/Shapes/Rectangle.cs +++ b/Shapes/Rectangle.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.Drawing.Imaging; using System.IO; @@ -112,6 +113,7 @@ namespace Shapes public override void Scale(double scaleFactor) { + Validator.ValidatePositiveDouble(scaleFactor, "Invalid scale factor"); foreach (var point in Points) { point.X *= scaleFactor; @@ -124,6 +126,7 @@ namespace Shapes _fileWriter.SaveShape(stream, this); } + [ExcludeFromCodeCoverage] public override void Draw(Stream stream) { var tmp = new Bitmap((int) Width * 2, (int) Height * 2); diff --git a/Shapes/Shape.cs b/Shapes/Shape.cs index 669f754..e774a1f 100644 --- a/Shapes/Shape.cs +++ b/Shapes/Shape.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.IO; using System.Runtime.Serialization; @@ -28,6 +29,7 @@ namespace Shapes public abstract void Scale(double scaleFactor); public abstract void Save(Stream stream); + [ExcludeFromCodeCoverage] public abstract void Draw(Stream stream); } } \ No newline at end of file diff --git a/Shapes/Triangle.cs b/Shapes/Triangle.cs index 0f9ba03..5ed31fd 100644 --- a/Shapes/Triangle.cs +++ b/Shapes/Triangle.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.Drawing.Imaging; using System.IO; @@ -88,19 +89,13 @@ namespace Shapes public override void Scale(double scaleFactor) { + Validator.ValidatePositiveDouble(scaleFactor, "Invalid scale factor"); + foreach (var point in Points) { point.X *= scaleFactor; point.Y *= scaleFactor; } - - foreach (var line in Lines) - { - line.Point1.X *= scaleFactor; - line.Point1.Y *= scaleFactor; - line.Point2.X *= scaleFactor; - line.Point2.Y *= scaleFactor; - } } public override void Save(Stream stream) @@ -109,7 +104,7 @@ namespace Shapes fileWriter.SaveShape(stream, this); } - + [ExcludeFromCodeCoverage] public override void Draw(Stream stream) { var tmp = new Bitmap((int) Width * 2, (int) Height * 2); diff --git a/UnitTests/CircleTests.cs b/UnitTests/CircleTests.cs index 374f3dd..dbdc8cb 100644 --- a/UnitTests/CircleTests.cs +++ b/UnitTests/CircleTests.cs @@ -1,4 +1,8 @@ +using System; +using System.Collections.Generic; using System.Drawing; +using System.IO; +using Moq; using NUnit.Framework; using Shapes; @@ -6,23 +10,107 @@ namespace Tests { public class CircleTests { + private Mock _mockFileStream; + private Mock _mockFileIO; + [SetUp] public void Setup() { + _mockFileIO = new Mock(); + _mockFileStream = new Mock(); } [Test] - public void CircleXY() + public void CircleXYTest() { var circle = new Circle(10, 10, 1); Assert.AreEqual(10, circle.CenterPoint.X); Assert.AreEqual(10, circle.CenterPoint.Y); - Assert.AreEqual(2, circle.Height); Assert.AreEqual(2, circle.Height); + Assert.AreEqual(2, circle.Height); + Assert.AreEqual(2, circle.Height); Assert.AreEqual(2, circle.Width); Assert.AreEqual(Color.Empty, circle.Fill); Assert.AreEqual(Color.Black, circle.Stroke); + circle.Fill = Color.Aqua; + Assert.AreEqual(Color.Aqua, circle.Fill); Assert.IsNull(circle.Points); Assert.AreEqual(1, circle.Radius); } + + + [Test] + public void CirclePointRadiusTest() + { + var circle = new Circle(new Shapes.Point(10, 10), 1); + Assert.AreEqual(10, circle.CenterPoint.X); + Assert.AreEqual(10, circle.CenterPoint.Y); + Assert.AreEqual(2, circle.Height); + Assert.AreEqual(2, circle.Height); + Assert.AreEqual(2, circle.Width); + Assert.AreEqual(Color.Empty, circle.Fill); + Assert.AreEqual(Color.Black, circle.Stroke); + circle.Fill = Color.Aqua; + Assert.AreEqual(Color.Aqua, circle.Fill); + Assert.IsNull(circle.Points); + Assert.AreEqual(1, circle.Radius); + } + + [Test] + public void InvalidCircleXYTest() + { + Assert.That(() => new Circle(10, 10, -1), Throws.TypeOf() + .With.Message.EqualTo("Invalid radius")); + } + + [Test] + public void InvalidCirclePointRadiusTest() + { + Assert.That(() => new Circle(new Shapes.Point(10, 10), -1), Throws.TypeOf() + .With.Message.EqualTo("Invalid radius")); + } + + [Test] + public void ScaleTest() + { + var circle = new Circle(10, 10, 1); + circle.Scale(10); + Assert.AreEqual(10, circle.Radius); + } + + [Test] + public void BadScaleTest() + { + var circle = new Circle(10, 10, 1); + Assert.That(() => circle.Scale(-10), Throws.TypeOf() + .With.Message.EqualTo("Invalid scale factor")); + } + + [Test] + public void ComputeAreaTest() + { + var circle = new Circle(10, 10, 1.26); + Assert.AreEqual(Math.PI * Math.Pow(1.26, 2), circle.ComputeArea()); + + } + + [Test] + public void SaveTest() + { + + Shape createdObj = null; + var circle = new Circle( + new Shapes.Point(20, 20), 5); + _mockFileIO.Setup(x => x.SaveShape(It.IsAny(), It.IsAny())) + .Callback((i, x) => { createdObj = x; }); + + circle._fileWriter = _mockFileIO.Object; + + circle.Save(_mockFileStream.Object); + Assert.AreEqual(circle, createdObj); + + _mockFileIO.Verify(x => x.SaveShape(It.IsAny(), + It.IsAny()), Times.Once); + + } } } \ No newline at end of file diff --git a/UnitTests/LineTests.cs b/UnitTests/LineTests.cs new file mode 100644 index 0000000..0e3d6dd --- /dev/null +++ b/UnitTests/LineTests.cs @@ -0,0 +1,75 @@ +using System; +using NUnit.Framework; +using Shapes; + +namespace Tests +{ + public class LineTests + { + [SetUp] + public void SetUp() + { + + } + + [Test] + public void LinePtoP() + { + var point1 = new Point(1, 0); + var point2 = new Point(3, 0); + var line = new Line(point1, point2); + Assert.AreEqual(point1, line.Point1); + Assert.AreEqual(point2, line.Point2); + } + + [Test] + public void InvalidLinePtoP() + { + Point p = null; + Assert.That(() => new Line(p, p), Throws.TypeOf() + .With.Message.EqualTo("Invalid point")); + } + + [Test] + public void LineXY() + { + var point1x = 1; + var point1y = 0; + var point2x = 3; + var point2y = 0; + + var line = new Line(point1x, point1y, point2x, point2y); + Assert.AreEqual(point1x, line.Point1.X); + Assert.AreEqual(point1y, line.Point1.Y); + Assert.AreEqual(point2x, line.Point2.X); + Assert.AreEqual(point2y, line.Point2.Y); + + } + + [Test] + public void ComputeSlopeTest() + { + Assert.AreEqual(1, new Line(new Point(0,0), new Point(1,1)).ComputeSlope()); + } + + [Test] + public void ComputeLengthTest() + { + Assert.AreEqual(1, new Line(new Point(0,0), new Point(1,0)).ComputeLength()); + } + + [Test] + public void MoveTest() + { + var line = new Line(new Point(0, 0), new Point(0, 1)); + line.Move(20,20); + Assert.AreEqual(20, line.Point1.X); + Assert.AreEqual(20, line.Point1.Y); + Assert.AreEqual(20, line.Point2.X); + Assert.AreEqual(21, line.Point2.Y); + + } + + + } +} \ No newline at end of file diff --git a/UnitTests/RectangleTests.cs b/UnitTests/RectangleTests.cs index aa19939..39a0989 100644 --- a/UnitTests/RectangleTests.cs +++ b/UnitTests/RectangleTests.cs @@ -40,6 +40,8 @@ namespace Tests Assert.AreEqual(rectangle.ComputeArea(), 100); Assert.AreEqual(rectangle.CalculateHeight(), 10); Assert.AreEqual(rectangle.CalculateWidth(), 10); + rectangle.Fill = Color.Aqua; + Assert.AreEqual(Color.Aqua, rectangle.Fill); foreach (var line in rectangle.Lines) { Assert.IsTrue(line.ComputeLength() == 10); @@ -49,14 +51,12 @@ namespace Tests [Test] public void BadRectangleWithPoints() { - Assert.Throws(typeof(ShapeException), delegate - { - new Rectangle( - new Point(30, 20), - new Point(30, 20), - new Point(30, 30), - new Point(20, 30)); - }, $"Attempted to create an invalid shape {typeof(Rectangle)}"); + Assert.That(()=> new Rectangle( + new Point(30, 20), + new Point(30, 20), + new Point(30, 30), + new Point(20, 30)), Throws.TypeOf() + .With.Message.EqualTo($"Attempted to create an invalid shape {typeof(Rectangle)}")); } [Test] @@ -74,6 +74,8 @@ namespace Tests Assert.AreEqual(100, rectangle.ComputeArea()); Assert.AreEqual(10, rectangle.CalculateHeight()); Assert.AreEqual(10, rectangle.CalculateWidth()); + rectangle.Fill = Color.Aqua; + Assert.AreEqual(Color.Aqua, rectangle.Fill); foreach (var line in rectangle.Lines) { Assert.IsTrue(line.ComputeLength() == 10); @@ -97,6 +99,8 @@ namespace Tests Assert.AreEqual(100, rectangle.ComputeArea()); Assert.AreEqual(10, rectangle.CalculateHeight()); Assert.AreEqual(10, rectangle.CalculateWidth()); + rectangle.Fill = Color.Aqua; + Assert.AreEqual(Color.Aqua, rectangle.Fill); foreach (var line in rectangle.Lines) { Assert.IsTrue(line.ComputeLength() == 10); @@ -106,14 +110,12 @@ namespace Tests [Test] public void BadRectangleWithXY() { - Assert.Throws(typeof(ShapeException), delegate - { - new Rectangle( - 30, 20, - 30, 20, - 30, 30, - 20, 30); - }, $"Attempted to create an invalid shape {typeof(Rectangle)}"); + Assert.That(()=> new Rectangle( + 30, 20, + 30, 20, + 30, 30, + 20, 30), Throws.TypeOf() + .With.Message.EqualTo($"Attempted to create an invalid shape {typeof(Rectangle)}")); } [Test] @@ -174,6 +176,20 @@ namespace Tests } } + + [Test] + public void BadScaleTest() + { + List points = new List(); + + var rectangle = new Rectangle( + new Point(20, 20), + new Point(30, 20), + new Point(30, 30), + new Point(20, 30)); + Assert.That(()=> rectangle.Scale(-20), Throws.TypeOf() + .With.Message.EqualTo("Invalid scale factor")); + } } } \ No newline at end of file diff --git a/UnitTests/TriangleTests.cs b/UnitTests/TriangleTests.cs index b204d2d..0f1d2a0 100644 --- a/UnitTests/TriangleTests.cs +++ b/UnitTests/TriangleTests.cs @@ -1,4 +1,7 @@ +using System.Drawing; using NUnit.Framework; +using Shapes; +using Point = Shapes.Point; namespace Tests { @@ -10,9 +13,63 @@ namespace Tests } [Test] - public void Test1() + public void TrianglePoints() { - Assert.Pass(); + var point1 = new Point(0, 0); + var point2 = new Point(3, 0); + var point3 = new Point(0, 3); + var triangle = new Triangle(point1, point2, point3); + + Assert.AreEqual( Color.Empty, triangle.Fill); + triangle.Fill = Color.Aqua; + Assert.AreEqual(Color.Aqua, triangle.Fill); + Assert.AreEqual(Color.Black, triangle.Stroke); + Assert.AreEqual(point1, triangle.Points[0]); + Assert.AreEqual(point2, triangle.Points[1]); + Assert.AreEqual(point3, triangle.Points[2]); + Assert.AreEqual(point1, triangle.Lines[0].Point1 ); + Assert.AreEqual(point2, triangle.Lines[0].Point2 ); + Assert.AreEqual(point2, triangle.Lines[1].Point1 ); + Assert.AreEqual(point3, triangle.Lines[1].Point2 ); + Assert.AreEqual(point3, triangle.Lines[2].Point1 ); + Assert.AreEqual(point1, triangle.Lines[2].Point2 ); + + } + [Test] + public void TriangleXY() + { + var point1x = 0; + var point2x = 3; + var point3x = 0; + var point1y = 0; + var point2y = 0; + var point3y = 3; + + var triangle = new Triangle(point1x, point1y, point2x, point2y, point3x, point3y); + + Assert.AreEqual( Color.Empty, triangle.Fill); + triangle.Fill = Color.Aqua; + Assert.AreEqual(Color.Aqua, triangle.Fill); + Assert.AreEqual(Color.Black, triangle.Stroke); + Assert.AreEqual(point1x, triangle.Points[0].X); + Assert.AreEqual(point2x, triangle.Points[1].X); + Assert.AreEqual(point3x, triangle.Points[2].X); + Assert.AreEqual(point1x, triangle.Lines[0].Point1.X ); + Assert.AreEqual(point2x, triangle.Lines[0].Point2.X ); + Assert.AreEqual(point2x, triangle.Lines[1].Point1.X ); + Assert.AreEqual(point3x, triangle.Lines[1].Point2.X ); + Assert.AreEqual(point3x, triangle.Lines[2].Point1.X ); + Assert.AreEqual(point1x, triangle.Lines[2].Point2.X ); + Assert.AreEqual(point1y, triangle.Points[0].Y); + Assert.AreEqual(point2y, triangle.Points[1].Y); + Assert.AreEqual(point3y, triangle.Points[2].Y); + Assert.AreEqual(point1y, triangle.Lines[0].Point1.Y ); + Assert.AreEqual(point2y, triangle.Lines[0].Point2.Y ); + Assert.AreEqual(point2y, triangle.Lines[1].Point1.Y ); + Assert.AreEqual(point3y, triangle.Lines[1].Point2.Y ); + Assert.AreEqual(point3y, triangle.Lines[2].Point1.Y ); + Assert.AreEqual(point1y, triangle.Lines[2].Point2.Y ); + } } } \ No newline at end of file diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 9d2aafc..3676438 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.2