using System; using System.Collections.Generic; using Chernobyl.Creation; using Chernobyl.Event; using Chernobyl.Mathematics.Geometry; using Chernobyl.Mathematics.Vectors; using NUnit.Framework; namespace Chernobyl.Mathematics.Creation { [TestFixture, Description("Tests for the Rectangle.Factory type.")] public class RectangleFactoryTests : FactoryTests, IComparer { [Test, Description("A test to ensure the Rectangle.Factory(Vector2, float, float) " + "constructor properly throws exceptions if it is passed a negative " + "width or height.")] public void ConstructorVector2FloatFloatArgumentExceptionTest() { Assert.Throws(typeof (ArgumentException), () => new Rectangle(Vector2.Zero, -1, 1)); Assert.Throws(typeof (ArgumentException), () => new Rectangle(Vector2.Zero, 1, -1)); } [Test, Description("A test to ensure the Rectangle.Factory(Vector2, float, float) " + "constructor uses the values passed to it to construct Rectangles.")] public void ConstructorVector2FloatFloatTest() { Vector2 expectedOrigin = new Vector2(-5.7f, 45.8f); const float expectedWidth = 32.5f; const float expectedHeight = 184.3f; IFactory factory = new Rectangle.Factory(expectedOrigin, expectedWidth, expectedHeight); CheckProperties(factory, expectedOrigin, expectedWidth, expectedHeight); } [Test, Description("A test to ensure the Rectangle.Factory(float, float, float, float) " + "constructor properly throws exceptions if it is passed a negative " + "width or height.")] public void ConstructorFloatFloatFloatFloatArgumentExceptionTest() { Assert.Throws(typeof(ArgumentException), () => new Rectangle(0, 0, -1, 1)); Assert.Throws(typeof(ArgumentException), () => new Rectangle(0, 0, 1, -1)); } [Test, Description("A test to ensure the Rectangle.Factory(float, float, float, float) " + "constructor uses the values passed to it to construct Rectangles.")] public void ConstructorFloatFloatFloatFloatTest() { Vector2 expectedOrigin = new Vector2(-5.7f, 45.7f); const float expectedWidth = 32.5f; const float expectedHeight = 184.3f; IFactory factory = new Rectangle.Factory(expectedOrigin.X, expectedOrigin.Y, expectedWidth, expectedHeight); CheckProperties(factory, expectedOrigin, expectedWidth, expectedHeight); } [Test, Description("A test to ensure the Rectangle.Factory(float, float) " + "constructor properly throws exceptions if it is passed a negative " + "width or height.")] public void ConstructorFloatFloatArgumentExceptionTest() { Assert.Throws(typeof(ArgumentException), () => new Rectangle(-1, 1)); Assert.Throws(typeof(ArgumentException), () => new Rectangle(1, -1)); } [Test, Description("A test to ensure the Rectangle.Factory(float, float) " + "constructor uses the values passed to it to construct Rectangles.")] public void ConstructorFloatFloatTest() { const float expectedWidth = 32.5f; const float expectedHeight = 184.3f; IFactory factory = new Rectangle.Factory(expectedWidth, expectedHeight); CheckProperties(factory, Vector2.Zero, expectedWidth, expectedHeight); } [Test, Description("A test to ensure that the Rectangle.Factory creates " + "a Rectangle with the origin specified on Rectangle.Factory.Origin.")] public void OriginTest() { Vector2 expectedOrigin = new Vector2(-5.7f, 45.8f); const float expectedWidth = 32.5f; const float expectedHeight = 184.3f; Rectangle.Factory factory = new Rectangle.Factory(Vector2.Zero, expectedWidth, expectedHeight); factory.Origin = expectedOrigin; CheckProperties(factory, expectedOrigin, expectedWidth, expectedHeight); } [Test, Description("A test to ensure that Rectangle.Factory.Width throws " + "an ArgumentException when it is given a negative value.")] public void WidthExceptionTest() { Rectangle.Factory factory = new Rectangle.Factory(1, 1); Assert.Throws(() => factory.Width = -1); } [Test, Description("A test to ensure that the Rectangle.Factory creates " + "a Rectangle with the width specified on Rectangle.Factory.Width.")] public void WidthTest() { Vector2 expectedOrigin = new Vector2(-5.7f, 45.8f); const float expectedWidth = 32.5f; const float expectedHeight = 184.3f; Rectangle.Factory factory = new Rectangle.Factory(expectedOrigin, 1, expectedHeight); factory.Width = expectedWidth; CheckProperties(factory, expectedOrigin, expectedWidth, expectedHeight); } [Test, Description("A test to ensure that Rectangle.Factory.Height throws " + "an ArgumentException when it is given a negative value.")] public void HeightExceptionTest() { Rectangle.Factory factory = new Rectangle.Factory(1, 1); Assert.Throws(() => factory.Height = -1); } [Test, Description("A test to ensure that the Rectangle.Factory creates " + "a Rectangle with the height specified on Rectangle.Factory.Height.")] public void HeightTest() { Vector2 expectedOrigin = new Vector2(-5.7f, 45.8f); const float expectedWidth = 32.5f; const float expectedHeight = 184.3f; Rectangle.Factory factory = new Rectangle.Factory(expectedOrigin, expectedWidth, 1); factory.Height = expectedHeight; CheckProperties(factory, expectedOrigin, expectedWidth, expectedHeight); } protected override IFactory CreateSuccessfulFactory() { // The FactoryTests is also used here to test the // Rectangle.Factory(Rectangle) constructor. return new Rectangle.Factory(GetProperlyCreatedItem()); } protected override IFactory CreateFailureFactory() { // This doesn't have a point of failure at the moment. return null; } protected override Rectangle GetProperlyCreatedItem() { return new Rectangle(-5.7f, 45.8f, 32.5f, 184.3f); } protected override IComparer GetComparer() { return this; } public int Compare(Rectangle x, Rectangle y) { // Define the amount of tolerated variation in the compared values. const float floatTolerance = .0001f; int result = 1; if ((x.Origin.X - y.Origin.X) <= floatTolerance && (x.Origin.Y - y.Origin.Y) <= floatTolerance && (x.Width - y.Width) <= floatTolerance && (x.Height - y.Height) <= floatTolerance) result = 0; return result; } /// /// A method that is invokes /// on the passed in and checks the properties /// of the created instance to ensure they are correct. Additionally it /// ensures the callback passed to the /// /// method is invoked. /// /// The factory to invoke /// on. /// The expected value of created /// instance's . /// The expected value of created /// instance's . /// The expected value of created /// instance's . void CheckProperties(IFactory factory, Vector2 expectedOrigin, float expectedWidth, float expectedHeight) { // Define the amount of tolerated variation in the compared values. const float floatTolerance = .0001f; var result = factory.Create(); Assert.AreEqual(expectedOrigin.X, result.Origin.X, floatTolerance, "The Factory did not create a Rectangle with the expected Rectangle.Origin.X."); Assert.AreEqual(expectedOrigin.Y, result.Origin.Y, floatTolerance, "The Factory did not create a Rectangle with the expected Rectangle.Origin.Y."); Assert.AreEqual(expectedWidth, result.Width, floatTolerance, "The Factory did not create a Rectangle with the expected Rectangle.Width."); Assert.AreEqual(expectedHeight, result.Height, floatTolerance, "The Factory did not create a Rectangle with the expected Rectangle.Height."); Assert.Fail("The CreationCompletedEventHandler Callback was not invoked."); } } }