using System.Collections.Generic;
using NUnit.Framework;
namespace Chernobyl.DesignPatterns.Extension
{
///
/// Tests types that implement the ,
/// , and/or
/// interfaces.
///
public abstract class ExtensionTests : NamedTests
{
///
/// 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 abstract IExtension CreateExtension();
///
/// 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 INamed CreateNamed()
{
return CreateExtension();
}
}
[TestFixture, Description("Tests types that implement the " +
"IExtension interface. This class also derives from " +
"ExtensionTest to handle the IExtension testing.")]
public abstract class GenericExtensionTests : ExtensionTests
{
///
/// Initializes a new instance of the
/// class that
/// uses the to test types.
///
protected GenericExtensionTests()
: this(EqualityComparer.Default)
{ }
///
/// Initializes a new instance of the
/// class.
///
/// The
/// used to compare types.
protected GenericExtensionTests(IEqualityComparer extendedComparer)
{
ExtendedComparer = extendedComparer;
}
[Test, Description("Tests that the extension can be set with a specified " +
"extendable and then that extendable is the same after it " +
"has been queried.")]
public void SettingAndQueryingExtension()
{
IExtension extension = CreateExtendedExtension();
TExtended extended = CreateExtendable();
extension.Extended = extended;
Assert.True(ExtendedComparer.Equals(extended, extension.Extended),
"The extended instance that was set on the " +
"IExtension.Extended did not equal the " +
"extended type that was set on the property.");
}
[Test, Description("Tests that the extension properly adds itself to " +
"the IExtendable.Extensions list when it is attached to the extended " +
"instance and also that it properly removes itself from the list when " +
"it is unattached. If the extended type does not implement IExtendable, " +
"then the test always passes.")]
public void ExtensionIsAddedToAndRemovedFromExtensionList()
{
TExtended extended = CreateExtendable();
if (extended is IExtendable)
{
IExtension extension = CreateExtendedExtension();
extension.Extended = extended;
Assert.True(((IExtendable)extended).Extensions.Contains(extension),
"The Extension is not properly contained within the IExtendable " +
"instances IExtendable.Extensions list even though we extended it.");
extension.Extended = default(TExtended);
Assert.False(((IExtendable)extended).Extensions.Contains(extension),
"The Extension is not properly removed within the IExtendable " +
"instance even though we unextended it.");
}
else
Assert.Pass("The extended type does not derive from IExtendable " +
"so this test does not need to be run.");
}
///
/// Should create the that
/// is to be tested. This method should always create a new type and
/// never reuse the instances it creates.
///
/// The to test.
protected abstract IExtension CreateExtendedExtension();
///
/// Should create a instance that can
/// be extended. This method should always create a new
/// instance and should never reuse
/// the instances it creates.
///
/// The instance that can be
/// extended.
protected abstract TExtended CreateExtendable();
protected override IExtension CreateExtension()
{
return CreateExtendedExtension();
}
///
/// The used to compare
/// types. By default, the
/// is used.
///
IEqualityComparer ExtendedComparer { get; set; }
}
}