Addind more Unit Test

This commit is contained in:
Brady Bodily
2019-10-25 15:39:00 -06:00
parent ada7171a9d
commit 557ed0a03c
10 changed files with 284 additions and 40 deletions

View File

@@ -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<Stream> _mockFileStream;
private Mock<IFileIO> _mockFileIO;
[SetUp]
public void Setup()
{
_mockFileIO = new Mock<IFileIO>();
_mockFileStream = new Mock<Stream>();
}
[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<ShapeException>()
.With.Message.EqualTo("Invalid radius"));
}
[Test]
public void InvalidCirclePointRadiusTest()
{
Assert.That(() => new Circle(new Shapes.Point(10, 10), -1), Throws.TypeOf<ShapeException>()
.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<ShapeException>()
.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<Stream>(), It.IsAny<Shape>()))
.Callback<Stream, Shape>((i, x) => { createdObj = x; });
circle._fileWriter = _mockFileIO.Object;
circle.Save(_mockFileStream.Object);
Assert.AreEqual(circle, createdObj);
_mockFileIO.Verify(x => x.SaveShape(It.IsAny<Stream>(),
It.IsAny<Shape>()), Times.Once);
}
}
}

75
UnitTests/LineTests.cs Normal file
View File

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

View File

@@ -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<ShapeException>()
.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<ShapeException>()
.With.Message.EqualTo($"Attempted to create an invalid shape {typeof(Rectangle)}"));
}
[Test]
@@ -174,6 +176,20 @@ namespace Tests
}
}
[Test]
public void BadScaleTest()
{
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));
Assert.That(()=> rectangle.Scale(-20), Throws.TypeOf<ShapeException>()
.With.Message.EqualTo("Invalid scale factor"));
}
}
}

View File

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

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>