using Chernobyl.DesignPatterns.Extension;
using Chernobyl.Mathematics.Vectors;
using Chernobyl.Measures;
using NUnit.Framework;
namespace Chernobyl.Mathematics.Movement
{
///
/// A test fixture base class that can be used to test types that implement
/// the interface.
///
public abstract class TransformTests : ExtensionTests
{
[Test, Description("A test that verifies that the " +
"ITransform.Translate(float, float) performs the expected translation.")]
public virtual void TranslateXy()
{
ITransform transform = CreateTransform();
Vector2 previousTranslation = transform.LocalMatrix.Translation.Xy;
transform.Translate(1, 2);
Vector2 expectedTranslation = new Vector2(1, 2) + previousTranslation;
Assert.AreEqual(expectedTranslation, transform.LocalMatrix.Translation.Xy,
"The translation component of the ITransform is not " +
"equal to (1, 2), even though they should be");
transform = CreateTransform();
previousTranslation = transform.LocalMatrix.Translation.Xy;
transform.Translate(new Vector2(3, 4));
expectedTranslation = new Vector2(3, 4) + previousTranslation;
Assert.AreEqual(expectedTranslation, transform.LocalMatrix.Translation.Xy,
"The translation component of the ITransform is not " +
"equal to (3, 4), even though they should be");
}
[Test, Description("A test that verifies that the " +
"ITransform.Translate(float, float, float) performs the expected " +
"translation.")]
public virtual void TranslateXyz()
{
ITransform transform = CreateTransform();
Vector3 previousTranslation = transform.LocalMatrix.Translation;
transform.Translate(1, 2, 3);
Vector3 expectedTranslation = new Vector3(1, 2, 3) + previousTranslation;
Assert.AreEqual(expectedTranslation, transform.LocalMatrix.Translation,
"The translation component of the ITransform is not " +
"equal to (1, 2, 3), even though they should be");
transform = CreateTransform();
previousTranslation = transform.LocalMatrix.Translation;
transform.Translate(new Vector3(4, 5, 6));
expectedTranslation = new Vector3(4, 5, 6) + previousTranslation;
Assert.AreEqual(expectedTranslation, transform.LocalMatrix.Translation,
"The translation component of the ITransform is not " +
"equal to (4, 5, 6), even though they should be");
}
[Test, Description("A test that verifies that the " +
"ITransform.TranslateX(float) performs the expected translation.")]
public virtual void TranslateX()
{
ITransform transform = CreateTransform();
float previousTranslation = transform.LocalMatrix.Translation.X;
transform.TranslateX(45);
float expectedTranslation = 45 + previousTranslation;
Assert.AreEqual(expectedTranslation, transform.LocalMatrix.Translation.X,
"The X translation component of the ITransform is not " +
"equal to 45, even though they should be");
}
[Test, Description("A test that verifies that the " +
"ITransform.TranslateY(float) performs the expected translation.")]
public virtual void TranslateY()
{
ITransform transform = CreateTransform();
float previousTranslation = transform.LocalMatrix.Translation.Y;
transform.TranslateY(45);
float expectedTranslation = 45 + previousTranslation;
Assert.AreEqual(expectedTranslation, transform.LocalMatrix.Translation.Y,
"The Y translation component of the ITransform is not " +
"equal to 45, even though they should be");
}
[Test, Description("A test that verifies that the " +
"ITransform.TranslateZ(float) performs the expected translation.")]
public virtual void TranslateZ()
{
ITransform transform = CreateTransform();
float previousTranslation = transform.LocalMatrix.Translation.Z;
transform.TranslateZ(45);
float expectedTranslation = 45 + previousTranslation;
Assert.AreEqual(expectedTranslation, transform.LocalMatrix.Translation.Z,
"The Z translation component of the ITransform is not " +
"equal to 45, even though they should be");
}
[Test, Description("A test that verifies that the " +
"ITransform.Rotate(Vector3, Radian) performs the expected rotation.")]
public virtual void Rotate()
{
ITransform transform = CreateTransform();
transform.Rotate(Vector3.UnitX, (Degree)90.0f);
Matrix4 expectedResult; Matrix4.Rotate(out expectedResult, Vector3.UnitX, (Degree)90.0f);
Assert.AreEqual(expectedResult, transform.LocalMatrix,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.Pitch(Radian) performs the expected rotation.")]
public virtual void Pitch()
{
ITransform transform = CreateTransform();
transform.Pitch((Degree)90.0f);
Matrix4 expectedResult; Matrix4.RotateX(out expectedResult, (Degree)90.0f);
Assert.AreEqual(expectedResult, transform.LocalMatrix,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.Yaw(Radian) performs the expected rotation.")]
public virtual void Yaw()
{
ITransform transform = CreateTransform();
transform.Yaw((Degree)90.0f);
Matrix4 expectedResult; Matrix4.RotateY(out expectedResult, (Degree)90.0f);
Assert.AreEqual(expectedResult, transform.LocalMatrix,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.Roll(Radian) performs the expected rotation.")]
public virtual void Roll()
{
ITransform transform = CreateTransform();
transform.Roll((Degree)90.0f);
Matrix4 expectedResult; Matrix4.RotateZ(out expectedResult, (Degree)90.0f);
Assert.AreEqual(expectedResult, transform.LocalMatrix,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.Scale(float) performs the expected scale.")]
public virtual void Scale()
{
ITransform transform = CreateTransform();
Matrix4 previousLocal = transform.LocalMatrix;
transform.Scale(2.4f);
Matrix4 scaleMatrix = Matrix4.Scale(2.4f);
Matrix4 expectedResult = previousLocal * scaleMatrix;
Assert.AreEqual(expectedResult, transform.LocalMatrix,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.Scale(float, float) performs the expected scale.")]
public virtual void ScaleXy()
{
ITransform transform = CreateTransform();
Matrix4 previousLocal = transform.LocalMatrix;
transform.Scale(2, 3);
Matrix4 scaleMatrix = Matrix4.Scale(2, 3, 1);
Matrix4 expectedResult = previousLocal * scaleMatrix;
Assert.That(transform.LocalMatrix == expectedResult,
"The rotation was not equal to the expected result of " +
"the rotation.");
transform = CreateTransform();
previousLocal = transform.LocalMatrix;
transform.Scale(new Vector2(3, 4));
scaleMatrix = Matrix4.Scale(3, 4, 1);
expectedResult = previousLocal * scaleMatrix;
Assert.That(transform.LocalMatrix == expectedResult,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.Scale(float, float, float) performs the expected scale.")]
public virtual void ScaleXyz()
{
ITransform transform = CreateTransform();
Matrix4 previousLocal = transform.LocalMatrix;
transform.Scale(2, 3, 4);
Matrix4 scaleMatrix = Matrix4.Scale(2, 3, 4);
Matrix4 expectedResult = previousLocal * scaleMatrix;
Assert.That(transform.LocalMatrix == expectedResult,
"The rotation was not equal to the expected result of " +
"the rotation.");
transform = CreateTransform();
previousLocal = transform.LocalMatrix;
transform.Scale(new Vector3(5, 6, 7));
scaleMatrix = Matrix4.Scale(5, 6, 7);
expectedResult = previousLocal * scaleMatrix;
Assert.That(transform.LocalMatrix == expectedResult,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.ScaleX(float) performs the expected scale.")]
public virtual void ScaleX()
{
ITransform transform = CreateTransform();
Matrix4 previousLocal = transform.LocalMatrix;
transform.ScaleX(2);
Matrix4 scaleMatrix = Matrix4.Scale(2, 1, 1);
Matrix4 expectedResult = previousLocal * scaleMatrix;
Assert.That(transform.LocalMatrix == expectedResult,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.ScaleY(float) performs the expected scale.")]
public virtual void ScaleY()
{
ITransform transform = CreateTransform();
Matrix4 previousLocal = transform.LocalMatrix;
transform.ScaleY(2);
Matrix4 scaleMatrix = Matrix4.Scale(1, 2, 1);
Matrix4 expectedResult = previousLocal * scaleMatrix;
Assert.That(transform.LocalMatrix == expectedResult,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.ScaleY(float) performs the expected scale.")]
public virtual void ScaleZ()
{
ITransform transform = CreateTransform();
Matrix4 previousLocal = transform.LocalMatrix;
transform.ScaleZ(2);
Matrix4 scaleMatrix = Matrix4.Scale(1, 1, 2);
Matrix4 expectedResult = previousLocal * scaleMatrix;
Assert.That(transform.LocalMatrix == expectedResult,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.SetTransformation(ref Matrix4) sets the expected Matrix.")]
public virtual void SetTransformation()
{
ITransform transform = CreateTransform();
Matrix4 translation = Matrix4.CreateTranslation(3.4f, 24.0f, 3.3f);
Matrix4 rotation; Matrix4.Rotate(out rotation, new Vector3(387.0f, 2.0f, 54.0f), (Degree) 35.76f);
Matrix4 scale = Matrix4.Scale(1, 1, 2);
Matrix4 expectedResult = translation * rotation * scale;
transform.SetTransformation(ref expectedResult);
Assert.That(transform.LocalMatrix == expectedResult,
"The rotation was not equal to the expected result of " +
"the rotation.");
}
[Test, Description("A test that verifies that the " +
"ITransform.TransformDirtied event properly fires.")]
public virtual void TransformDirtied()
{
ITransform transform = CreateTransform();
// just in case the transform is already dirty, we will clean it by
// accessing the WorldMatrix
Matrix4 worldMatrix = transform.WorldMatrix;
transform.TransformDirtied += (sender, e) =>
Assert.Pass("TransformDirtied event was invoked, test passes.");
transform.TranslateX(345.0f);
Assert.Fail("The ITransform.TransformDirtied event was NOT invoked " +
"even though it should have been.");
}
[Test, Description("A test that verifies that the " +
"ITransform can be transformed by transforming it's parent.")]
public virtual void IndirectTransformation()
{
ITransform transform = CreateTransform();
float previousValue = transform.WorldMatrix.Translation.X;
ITransform parent = new MatrixTransform();
Transform.MakeParentChild(parent, transform);
parent.TranslateX(345.0f);
float expectedValue = 345.0f + previousValue;
Assert.AreEqual(expectedValue, transform.WorldMatrix.Translation.X,
"The ITransform was not translated even though it's parent had been");
}
[Test, Description("A test that verifies that the " +
"ITransform.TransformDirtied event properly fires.")]
public virtual void SetIsUpdateNeeded()
{
ITransform transform = CreateTransform();
transform.IsUpdateNeeded = true;
Assert.That(transform.IsUpdateNeeded == true,
"The ITransform.IsUpdateNeeded was NOT true even though true " +
"was set on it.");
}
[Test, Description("A test that verifies that the " +
"ITransform.IsUpdateNeeded is true after a parent is attached to it.")]
public virtual void IsUpdateNeededIsTrueAfterAttachingParent()
{
ITransform transform = CreateTransform();
transform.TransformParent = new MatrixTransform();
Assert.That(transform.IsUpdateNeeded == true,
"The ITransform.IsUpdateNeeded was NOT true even though a " +
"parent was attached to the transform.");
}
///
/// Should create the instance that is being tested.
/// This method should always create a new instance and never reuse that
/// instance.
///
///
/// The new instance to be tested.
///
protected override IExtension CreateExtension()
{
return CreateTransform();
}
///
/// Creates the that is to be tested. This
/// method should always be create a new when
/// it is invoked and never reuse instances.
///
/// The instance that is to be tested.
protected abstract ITransform CreateTransform();
}
}