From ada7171a9dcc668ba8e137995317abb097677b8f Mon Sep 17 00:00:00 2001 From: bbod Date: Fri, 25 Oct 2019 11:29:29 -0600 Subject: [PATCH] rectangle is tested --- .idea/.idea.Shapes/.idea/contentModel.xml | 1 + Shapes/FileIO.cs | 2 +- Shapes/IFileIO.cs | 10 ++ Shapes/Rectangle.cs | 36 +++-- UnitTests/CircleTests.cs | 14 +- UnitTests/RectangleTests.cs | 161 ++++++++++++++++++++-- 6 files changed, 201 insertions(+), 23 deletions(-) create mode 100644 Shapes/IFileIO.cs diff --git a/.idea/.idea.Shapes/.idea/contentModel.xml b/.idea/.idea.Shapes/.idea/contentModel.xml index 9bacea3..0b9b503 100644 --- a/.idea/.idea.Shapes/.idea/contentModel.xml +++ b/.idea/.idea.Shapes/.idea/contentModel.xml @@ -14,6 +14,7 @@ + diff --git a/Shapes/FileIO.cs b/Shapes/FileIO.cs index 78a1959..3c60f10 100644 --- a/Shapes/FileIO.cs +++ b/Shapes/FileIO.cs @@ -3,7 +3,7 @@ using System.Runtime.Serialization.Json; namespace Shapes { - public class FileIO + public class FileIO : IFileIO { public void SaveShape(Stream stream, Shape shape) { diff --git a/Shapes/IFileIO.cs b/Shapes/IFileIO.cs new file mode 100644 index 0000000..412d889 --- /dev/null +++ b/Shapes/IFileIO.cs @@ -0,0 +1,10 @@ +using System.IO; + +namespace Shapes +{ + public interface IFileIO + { + void SaveShape(Stream stream, Shape shape); + T GetShapeFromFile(Stream stream); + } +} \ No newline at end of file diff --git a/Shapes/Rectangle.cs b/Shapes/Rectangle.cs index 57b3b1d..0ce7aac 100644 --- a/Shapes/Rectangle.cs +++ b/Shapes/Rectangle.cs @@ -3,14 +3,20 @@ using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.Serialization; +using System.Windows.Input; +using System.Runtime.CompilerServices; +[assembly:InternalsVisibleTo("UnitTests")] namespace Shapes { [DataContract] public class Rectangle : GeometricShape { + internal IFileIO _fileWriter; + public Rectangle(Point point1, Point point2, Point point3, Point point4) { + _fileWriter = new FileIO(); Stroke = Color.Black; Points = new List(); Lines = new List(); @@ -34,8 +40,10 @@ namespace Shapes public Rectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { + _fileWriter = new FileIO(); Stroke = Color.Black; Points = new List(); + Lines = new List(); var point1 = new Point(x1, y1); var point2 = new Point(x2, y2); var point3 = new Point(x3, y3); @@ -44,6 +52,12 @@ namespace Shapes Points.Add(point2); Points.Add(point3); Points.Add(point4); + + Lines.Add(new Line(point1, point2)); + Lines.Add(new Line(point2, point3)); + Lines.Add(new Line(point3, point4)); + Lines.Add(new Line(point4, point1)); + Height = new Line(point1, point2).ComputeLength(); Width = new Line(point1, point4).ComputeLength(); ComputeCenter(); @@ -53,8 +67,10 @@ namespace Shapes /// public Rectangle(Point point, Size size) { + _fileWriter = new FileIO(); Stroke = Color.Black; Points = new List(); + Lines = new List(); var point1 = point; var point2 = new Point(point.X + size.Width, point.Y); var point3 = new Point(point.X + size.Width, point.Y + size.Height); @@ -63,6 +79,12 @@ namespace Shapes Points.Add(point2); Points.Add(point3); Points.Add(point4); + + Lines.Add(new Line(point1, point2)); + Lines.Add(new Line(point2, point3)); + Lines.Add(new Line(point3, point4)); + Lines.Add(new Line(point4, point1)); + Height = new Line(point1, point2).ComputeLength(); Width = new Line(point1, point4).ComputeLength(); ComputeCenter(); @@ -95,20 +117,11 @@ namespace Shapes 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) { - var fileWriter = new FileIO(); - fileWriter.SaveShape(stream, this); + _fileWriter.SaveShape(stream, this); } public override void Draw(Stream stream) @@ -138,7 +151,6 @@ namespace Shapes { return new Line(Points[0], Points[1]).ComputeLength(); } - internal override void ComputeCenter() { CenterPoint = new Point((Points[0].X + Width) / 2, (Points[0].Y + Height) / 2); diff --git a/UnitTests/CircleTests.cs b/UnitTests/CircleTests.cs index 82f3a2b..374f3dd 100644 --- a/UnitTests/CircleTests.cs +++ b/UnitTests/CircleTests.cs @@ -1,4 +1,6 @@ +using System.Drawing; using NUnit.Framework; +using Shapes; namespace Tests { @@ -10,9 +12,17 @@ namespace Tests } [Test] - public void Test1() + public void CircleXY() { - Assert.Pass(); + 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.Width); + Assert.AreEqual(Color.Empty, circle.Fill); + Assert.AreEqual(Color.Black, circle.Stroke); + Assert.IsNull(circle.Points); + Assert.AreEqual(1, circle.Radius); } } } \ No newline at end of file diff --git a/UnitTests/RectangleTests.cs b/UnitTests/RectangleTests.cs index 86be801..aa19939 100644 --- a/UnitTests/RectangleTests.cs +++ b/UnitTests/RectangleTests.cs @@ -1,34 +1,179 @@ +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Text; +using System.IO; using NUnit.Framework; using Moq; +using NUnit.Framework.Internal; using Shapes; +using Point = Shapes.Point; +using Rectangle = Shapes.Rectangle; namespace Tests { public class RectangleTests { - private Mock point1; + private Mock _mockFileStream; + private Mock _mockFileIO; + [SetUp] public void Setup() { - point1 = new Mock(); - + _mockFileIO = new Mock(); + _mockFileStream = new Mock(); } [Test] public void RectangleWithPoints() { var rectangle = new Rectangle( - new Point(20,20), - new Point(30,20), - new Point(30,30), - new Point(20,30)); + new Point(20, 20), + new Point(30, 20), + new Point(30, 30), + new Point(20, 30)); Assert.IsTrue(rectangle.GetType() == typeof(Rectangle)); - Assert.IsTrue(rectangle.Height == 10 && rectangle.Width == 10 ); + Assert.IsTrue(rectangle.Height == 10 && rectangle.Width == 10); Assert.IsTrue(rectangle.Lines.Count == 4); + Assert.IsTrue(rectangle.CenterPoint.X == 25 && rectangle.CenterPoint.Y == 25); + Assert.AreEqual(rectangle.Fill, Color.Empty); + Assert.AreEqual(rectangle.Stroke, Color.Black); + Assert.AreEqual(rectangle.ComputeArea(), 100); + Assert.AreEqual(rectangle.CalculateHeight(), 10); + Assert.AreEqual(rectangle.CalculateWidth(), 10); foreach (var line in rectangle.Lines) { Assert.IsTrue(line.ComputeLength() == 10); } } + + [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)}"); + } + + [Test] + public void RectangleWithSinglePoint() + { + var rectangle = new Rectangle(new Point(20,10), new Size(10,10) ); + Assert.IsTrue(rectangle.GetType() == typeof(Rectangle)); + Assert.AreEqual(10, rectangle.Height); + Assert.AreEqual( 10, rectangle.Width); + Assert.IsTrue(rectangle.Lines.Count == 4); + Assert.AreEqual(15, rectangle.CenterPoint.X); + Assert.AreEqual(10, rectangle.CenterPoint.Y); + Assert.AreEqual(rectangle.Fill, Color.Empty); + Assert.AreEqual(rectangle.Stroke, Color.Black); + Assert.AreEqual(100, rectangle.ComputeArea()); + Assert.AreEqual(10, rectangle.CalculateHeight()); + Assert.AreEqual(10, rectangle.CalculateWidth()); + foreach (var line in rectangle.Lines) + { + Assert.IsTrue(line.ComputeLength() == 10); + } + } + + [Test] + public void RectangleWithXY() + { + var rectangle = new Rectangle( + 20, 20, + 30, 20, + 30, 30, + 20, 30); + Assert.IsTrue(rectangle.GetType() == typeof(Rectangle)); + Assert.IsTrue(rectangle.Height == 10 && rectangle.Width == 10); + Assert.IsTrue(rectangle.Lines.Count == 4); + Assert.AreEqual(15, rectangle.CenterPoint.X); + Assert.AreEqual(15, rectangle.CenterPoint.Y); Assert.AreEqual(rectangle.Fill, Color.Empty); + Assert.AreEqual(rectangle.Stroke, Color.Black); + Assert.AreEqual(100, rectangle.ComputeArea()); + Assert.AreEqual(10, rectangle.CalculateHeight()); + Assert.AreEqual(10, rectangle.CalculateWidth()); + foreach (var line in rectangle.Lines) + { + Assert.IsTrue(line.ComputeLength() == 10); + } + } + + [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)}"); + } + + [Test] + public void SaveStreamTest() + { + Shape createdObj = null; + List rectangles = new List(); + rectangles.Add(new Rectangle( + new Point(20, 20), + new Point(30, 20), + new Point(30, 30), + new Point(20, 30))); + rectangles.Add(new Rectangle( + 20, 20, + 30, 20, + 30, 30, + 20, 30)); + rectangles.Add(new Rectangle(new Point(20,10), new Size(10,10))); + + foreach (var rectangle in rectangles) + { + _mockFileIO.Setup(x => x.SaveShape(It.IsAny(), It.IsAny())) + .Callback((i, x) => + { + createdObj = x; + } ); + + rectangle._fileWriter = _mockFileIO.Object; + + rectangle.Save(_mockFileStream.Object); + Assert.AreEqual(rectangle, createdObj); + } + _mockFileIO.Verify(x => x.SaveShape(It.IsAny(), + It.IsAny()), Times.Exactly(3)); + } + + [Test] + public void ScaleTest() + { + List points = new List(); + + var rectangle = new Rectangle( + new Point(20, 20), + new Point(30, 20), + new Point(30, 30), + new Point(20, 30)); + points.Add(new Point(20, 20)); + points.Add(new Point(30, 20)); + points.Add(new Point(30, 30)); + points.Add( new Point(20, 30)); + rectangle.Scale(10); + for (int i = 0; i < rectangle.Points.Count; i++) + { + points[i].X *= 10; + points[i].Y *= 10; + Assert.AreEqual(points[i].X, rectangle.Points[i].X); + Assert.AreEqual(points[i].Y, rectangle.Points[i].Y); + + } + } } + } \ No newline at end of file