rectangle is tested

This commit is contained in:
2019-10-25 11:29:29 -06:00
parent 93b22ef2ec
commit ada7171a9d
6 changed files with 201 additions and 23 deletions

View File

@@ -14,6 +14,7 @@
<e p="CompositeShape.cs" t="Include" /> <e p="CompositeShape.cs" t="Include" />
<e p="FileIO.cs" t="Include" /> <e p="FileIO.cs" t="Include" />
<e p="GeometricShape.cs" t="Include" /> <e p="GeometricShape.cs" t="Include" />
<e p="IFileIO.cs" t="Include" />
<e p="Image.cs" t="Include" /> <e p="Image.cs" t="Include" />
<e p="ImageManager.cs" t="Include" /> <e p="ImageManager.cs" t="Include" />
<e p="Line.cs" t="Include" /> <e p="Line.cs" t="Include" />

View File

@@ -3,7 +3,7 @@ using System.Runtime.Serialization.Json;
namespace Shapes namespace Shapes
{ {
public class FileIO public class FileIO : IFileIO
{ {
public void SaveShape(Stream stream, Shape shape) public void SaveShape(Stream stream, Shape shape)
{ {

10
Shapes/IFileIO.cs Normal file
View File

@@ -0,0 +1,10 @@
using System.IO;
namespace Shapes
{
public interface IFileIO
{
void SaveShape(Stream stream, Shape shape);
T GetShapeFromFile<T>(Stream stream);
}
}

View File

@@ -3,14 +3,20 @@ using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Windows.Input;
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("UnitTests")]
namespace Shapes namespace Shapes
{ {
[DataContract] [DataContract]
public class Rectangle : GeometricShape public class Rectangle : GeometricShape
{ {
internal IFileIO _fileWriter;
public Rectangle(Point point1, Point point2, Point point3, Point point4) public Rectangle(Point point1, Point point2, Point point3, Point point4)
{ {
_fileWriter = new FileIO();
Stroke = Color.Black; Stroke = Color.Black;
Points = new List<Point>(); Points = new List<Point>();
Lines = new List<Line>(); Lines = new List<Line>();
@@ -34,8 +40,10 @@ namespace Shapes
public Rectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) public Rectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
{ {
_fileWriter = new FileIO();
Stroke = Color.Black; Stroke = Color.Black;
Points = new List<Point>(); Points = new List<Point>();
Lines = new List<Line>();
var point1 = new Point(x1, y1); var point1 = new Point(x1, y1);
var point2 = new Point(x2, y2); var point2 = new Point(x2, y2);
var point3 = new Point(x3, y3); var point3 = new Point(x3, y3);
@@ -44,6 +52,12 @@ namespace Shapes
Points.Add(point2); Points.Add(point2);
Points.Add(point3); Points.Add(point3);
Points.Add(point4); 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(); Height = new Line(point1, point2).ComputeLength();
Width = new Line(point1, point4).ComputeLength(); Width = new Line(point1, point4).ComputeLength();
ComputeCenter(); ComputeCenter();
@@ -53,8 +67,10 @@ namespace Shapes
/// <inheritdoc /> /// <inheritdoc />
public Rectangle(Point point, Size size) public Rectangle(Point point, Size size)
{ {
_fileWriter = new FileIO();
Stroke = Color.Black; Stroke = Color.Black;
Points = new List<Point>(); Points = new List<Point>();
Lines = new List<Line>();
var point1 = point; var point1 = point;
var point2 = new Point(point.X + size.Width, point.Y); var point2 = new Point(point.X + size.Width, point.Y);
var point3 = new Point(point.X + size.Width, point.Y + size.Height); var point3 = new Point(point.X + size.Width, point.Y + size.Height);
@@ -63,6 +79,12 @@ namespace Shapes
Points.Add(point2); Points.Add(point2);
Points.Add(point3); Points.Add(point3);
Points.Add(point4); 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(); Height = new Line(point1, point2).ComputeLength();
Width = new Line(point1, point4).ComputeLength(); Width = new Line(point1, point4).ComputeLength();
ComputeCenter(); ComputeCenter();
@@ -95,20 +117,11 @@ namespace Shapes
point.X *= scaleFactor; point.X *= scaleFactor;
point.Y *= 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) public override void Save(Stream stream)
{ {
var fileWriter = new FileIO(); _fileWriter.SaveShape(stream, this);
fileWriter.SaveShape(stream, this);
} }
public override void Draw(Stream stream) public override void Draw(Stream stream)
@@ -138,7 +151,6 @@ namespace Shapes
{ {
return new Line(Points[0], Points[1]).ComputeLength(); return new Line(Points[0], Points[1]).ComputeLength();
} }
internal override void ComputeCenter() internal override void ComputeCenter()
{ {
CenterPoint = new Point((Points[0].X + Width) / 2, (Points[0].Y + Height) / 2); CenterPoint = new Point((Points[0].X + Width) / 2, (Points[0].Y + Height) / 2);

View File

@@ -1,4 +1,6 @@
using System.Drawing;
using NUnit.Framework; using NUnit.Framework;
using Shapes;
namespace Tests namespace Tests
{ {
@@ -10,9 +12,17 @@ namespace Tests
} }
[Test] [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);
} }
} }
} }

View File

@@ -1,34 +1,179 @@
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using NUnit.Framework; using NUnit.Framework;
using Moq; using Moq;
using NUnit.Framework.Internal;
using Shapes; using Shapes;
using Point = Shapes.Point;
using Rectangle = Shapes.Rectangle;
namespace Tests namespace Tests
{ {
public class RectangleTests public class RectangleTests
{ {
private Mock<Point> point1; private Mock<Stream> _mockFileStream;
private Mock<IFileIO> _mockFileIO;
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
point1 = new Mock<Point>(); _mockFileIO = new Mock<IFileIO>();
_mockFileStream = new Mock<Stream>();
} }
[Test] [Test]
public void RectangleWithPoints() public void RectangleWithPoints()
{ {
var rectangle = new Rectangle( var rectangle = new Rectangle(
new Point(20,20), new Point(20, 20),
new Point(30,20), new Point(30, 20),
new Point(30,30), new Point(30, 30),
new Point(20,30)); new Point(20, 30));
Assert.IsTrue(rectangle.GetType() == typeof(Rectangle)); 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.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) foreach (var line in rectangle.Lines)
{ {
Assert.IsTrue(line.ComputeLength() == 10); 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<Rectangle> rectangles = new List<Rectangle>();
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<Stream>(), It.IsAny<Shape>()))
.Callback<Stream, Shape>((i, x) =>
{
createdObj = x;
} );
rectangle._fileWriter = _mockFileIO.Object;
rectangle.Save(_mockFileStream.Object);
Assert.AreEqual(rectangle, createdObj);
}
_mockFileIO.Verify(x => x.SaveShape(It.IsAny<Stream>(),
It.IsAny<Shape>()), Times.Exactly(3));
}
[Test]
public void ScaleTest()
{
List<Point> points = new List<Point>();
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);
}
}
} }
} }