using System.Collections.Generic; using System.Linq; using NUnit.Framework; namespace Chernobyl.Mathematics.Collision { /// /// A reusable test fixture base class that tests /// types. /// /// The type of /// contained within the /// to be /// tested. /// The type of /// that can collide with the /// s contained in the /// to be /// tested. public abstract class CollidableEnumerableTests where TContainedCollidable : IExtendedCollidable { [Test, Description("Tests the " + "ICollidableEnumerable.GetCollidables(TCollider collidable) " + "method to see if it returns the proper collidable when " + "passed a collider that should collide with that collidable.")] public void FindsCollidable() { ICollidableEnumerable collidableCollection = CreateAndCheckCollisionEnumerable(); TCollider collider = CreateCollider(); IEnumerable collidables = collidableCollection.GetCollidables(collider); TContainedCollidable collided = GetCollided(); int collidablesCount = collidables.Count(); Assert.True(collidablesCount == 1, "There should have only been one " + "collidable that was collided with. But \"" + collidablesCount + "\" were collided against."); Assert.AreEqual(collided, collidables.First(), "The collidable that " + "was collided against was not the collidable that was expected."); } [Test, Description("Tests the " + "ICollidableEnumerable.GetCollidables(TCollider collidable) " + "method to see if it properly returns no collidable when " + "passed a collider that should not collide with anything in the " + "ICollidableEnumerable.")] public void FindsNoCollidables() { ICollidableEnumerable collidableCollection = CreateAndCheckCollisionEnumerable(); TCollider nonCollider = CreateNonCollider(); IEnumerable collidables = collidableCollection.GetCollidables(nonCollider); int collidablesCount = collidables.Count(); Assert.True(collidablesCount == 0, "The CreateNonCollider() method " + "should have returned a collider that did not collide with any " + "of the collidables contained within the " + "ICollidableEnumerable created by the " + "method CreateCollidableEnumerable(), but \"" + collidablesCount + "\" were collided against."); } /// /// The number of that must /// be contained within the /// that /// are created by the method. The /// value of this /// public const uint CollidableEnumerableCount = 7; /// /// Creates an /// that is to be tested. This collision group create should have at least /// the number of items specified by /// of s contained within it. /// /// The /// that is to be tested. protected abstract ICollidableEnumerable CreateCollidableEnumerable(); /// /// Creates a to test for collisions /// against the /// created by . The /// must intersect with ONLY the /// created by /// . This method must create a new /// every time it is called and not /// reuse instances. /// /// The to test with the /// . protected abstract TCollider CreateCollider(); /// /// Retrieves a that is /// contained within the /// /// created by . The /// created must also /// collide with the that is created /// by the method . /// /// The that is /// being collided against the created /// by . protected abstract TContainedCollidable GetCollided(); /// /// Creates a that does not collide /// with any contained within /// the /// created by . This method must /// create a new every time it is /// called and not reuse instances. /// /// The to test with the /// . protected abstract TCollider CreateNonCollider(); /// /// Creates the /// using and then checks it to /// ensure it is setup properly. /// /// The created and verified /// . ICollidableEnumerable CreateAndCheckCollisionEnumerable() { ICollidableEnumerable collidableEnumerable = CreateCollidableEnumerable(); Assert.That(collidableEnumerable.Count() == CollidableEnumerableCount, "The ICollidableEnumerable created by " + "CreateCollidableEnumerable() must contain \"" + CollidableEnumerableCount + "\" " + "collidables. IT currently contains: \"" + collidableEnumerable.Count() + "\"."); return collidableEnumerable; } } }