using NUnit.Framework;
namespace System
{
///
/// A base helper class for test fixtures that can be used to test
/// types. Specifically, this class tests the
/// , ,
/// and methods. Note that, you generally
/// don't need to test the methods unless you are
/// overriding one or more of the previously mentioned methods.
///
public abstract class ObjectTests
{
[Test, Description("Tests the Object.Equals(object) method to see if it " +
"returns true for objects that are equal to each other.")]
public void ObjectsAreEqual()
{
object first = CreateObject();
object second = CreateSameObject();
EnsureDifferentInstances(first, second);
Assert.That(first.Equals(second), "The object.Equals(object) method " +
"is returning false, even though both objects should be equal.");
}
[Test, Description("Tests the Object.Equals(object) method to see if it " +
"returns false for objects that are NOT equal to each other.")]
public void ObjectsAreNotEqual()
{
object first = CreateObject();
object second = CreateDifferentObject();
EnsureDifferentInstances(first, second);
Assert.That(first.Equals(second) == false, "The object.Equals(object) " +
" method is returning true, even though both objects should NOT " +
"be equal.");
}
[Test, Description("Tests the Object.Equals(object) method to see if it " +
"returns false when a null value is passed to it.")]
public void NullIsNotEqual()
{
Assert.That(CreateObject().Equals(null) == false, "The object.Equals(object) " +
"method is returning true, even though null was passed as a parameter. ");
}
[Test, Description("Tests the Object.GetHashCode() method to see if it " +
"returns the same hash code as another object that is equal to it.")]
public void HashCodesAreEqual()
{
object first = CreateObject();
object second = CreateSameObject();
EnsureDifferentInstances(first, second);
Assert.That(first.GetHashCode() == second.GetHashCode(), "The " +
"object.GetHashCode() method is returning a different value " +
"for two objects that are equal.");
}
[Test, Description("Tests the Object.GetHashCode() method to see if it " +
"returns a different hash code as another object that is NOT equal to it.")]
public void HashCodesAreNotEqual()
{
object first = CreateObject();
object second = CreateDifferentObject();
EnsureDifferentInstances(first, second);
Assert.That(first.GetHashCode() != second.GetHashCode(), "The " +
"object.GetHashCode() method is returning the same value " +
"for two objects that are NOT equal.");
}
[Test, Description("Tests the Object.ToString() method to see if it " +
"returns the expected string value.")]
public void ToStringReturnsExpectedResult()
{
string expectedString = ExpectedToStringResult;
string objectString = CreateObject().ToString();
Assert.AreEqual(expectedString, objectString, "The string returned " +
"by the Object.ToString() method of the tested object is not " +
"the string value that was expected. Expected: \"" + expectedString +
"\". Actual: \"" + objectString + "\".");
}
///
/// Ensures two s are the not the same instance, i.e.
/// returns false
/// when and are used as
/// the parameters.
///
/// The first to compare against
/// the .
/// The second to compare
/// against the .
static void EnsureDifferentInstances(Object first, Object second)
{
Assert.That(ReferenceEquals(first, second) == false, "Both objects " +
"are the exact same instance. They must be different instances " +
"to allow them to be tested properly.");
}
///
/// Creates an to use for testing. This method
/// should always return a new object and never reuse values.
///
/// The to be tested
protected abstract object CreateObject();
///
/// Creates an to use for testing. This method
/// should create an object that is the same as the
/// created by but are not the same
/// instance (i.e.
/// should not return true). This method should always return a new
/// object and never reuse values.
///
/// The to be tested
protected abstract object CreateSameObject();
///
/// Creates an to use for testing. This method
/// should create an object that is different from the
/// created by . This method should always
/// return a new object and never reuse values.
///
/// The to be tested
protected abstract object CreateDifferentObject();
///
/// Returns the value that is expected as a result
/// from the method of the
/// created by the method.
///
protected abstract string ExpectedToStringResult { get; }
}
[TestFixture, Description("A test fixture that tests the ObjectTest base " +
"helper class and provides an example of how to use the ObjectTest class. ")]
public class IntObjectTest : ObjectTests
{
///
/// Creates an to use for testing. This method
/// should always return a new object and never reuse values.
///
/// The to be tested
protected override object CreateObject()
{
return 1;
}
///
/// Creates an to use for testing. This method
/// should create an object that is the same as the
/// created by but are not the same
/// instance (i.e.
/// should not return true). This method should always return a new
/// object and never reuse values.
///
/// The to be tested
protected override object CreateSameObject()
{
return 1;
}
///
/// Creates an to use for testing. This method
/// should create an object that is different from the
/// created by . This method should always
/// return a new object and never reuse values.
///
/// The to be tested
protected override object CreateDifferentObject()
{
return 2;
}
///
/// Returns the value that is expected as a result
/// from the method of the
/// created by the method.
///
///
protected override string ExpectedToStringResult
{
get { return "1"; }
}
}
}