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

@@ -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);
}
}
}

View File

@@ -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<Point> point1;
private Mock<Stream> _mockFileStream;
private Mock<IFileIO> _mockFileIO;
[SetUp]
public void Setup()
{
point1 = new Mock<Point>();
_mockFileIO = new Mock<IFileIO>();
_mockFileStream = new Mock<Stream>();
}
[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<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);
}
}
}
}