using System;
using System.Collections.Generic;
using System.Reflection;
using Chernobyl.Event;
using NUnit.Framework;
namespace Chernobyl.Creation
{
///
/// Tests for implementations of the type where
/// type parameter is the type of the instances
/// generated by the being tested.
///
/// The type of instances generated by the
/// being tested.
public abstract class FactoryTests
{
[Test, Description("A test that ensures the " +
"IFactory.Create(CreationCompletedEventHandler) method " +
"creates the instance properly.")]
public void CreateSuccessTest()
{
IFactory factory = CreateSuccessfulFactory();
var result = factory.Create();
Assert.True(GetComparer().Compare(GetProperlyCreatedItem(), result) == 0,
"The Factory did not create the item that was expected.");
}
[Test, Description("A test that ensures the " +
"IFactory.Create(CreationCompletedEventHandler) method sets " +
"the CreationEventArgs.Error properly and throws a " +
"TargetInvocationException when it has failed to create the instance.")]
public void CreateExceptionTest()
{
IFactory factory = CreateFailureFactory();
if (factory == null)
Assert.Ignore("This test cannot be run because the IFactory " +
"being tested cannot fail.");
else
{
Assert.Throws(() => factory.Create());
}
}
[Test, Description("A test to ensure the " +
"IFactory.Created event is raised and it's " +
"CreationEventArgs.Error is correct when the " +
"IFactory.Create(CreationCompletedEventHandler) method " +
"is invoked.")]
public void CreatedEventFailureTest()
{
IFactory factory = CreateFailureFactory();
if (factory == null)
Assert.Ignore("This test cannot be run because the IFactory " +
"being tested cannot fail.");
else
{
factory.Created += (sender, e) =>
{
Assert.AreNotEqual(null, e.Error,
"The CreationEventArgs.Error " +
"was not equal to the value expected.");
Assert.Throws(
() => Console.WriteLine("EventFactoryTests.CreatedEventFailureTest: " +
e.CreatedInstance));
Assert.Pass("EventFactory.Created event was raised.");
};
factory.Create();
// We should not get to this point.
Assert.Fail("EventFactory.Created event was not raised.");
}
}
///
/// Generates an instance that can successfully
/// generate instances of type .
///
/// The type to test.
protected abstract IFactory CreateSuccessfulFactory();
///
/// Generates an instance that fails to
/// generate instances of type or returns null
/// if the does not fail to create instances.
///
/// The type to test or null if
/// that can fail cannot be generated.
protected abstract IFactory CreateFailureFactory();
///
/// Returns an instance of type that is
/// "properly created" for the being tested.
/// This instance will be compared against for some of the tests.
///
/// The instance to compare the creation results of the
/// against.
protected abstract T GetProperlyCreatedItem();
///
/// The used to compare two
/// types. By default,
/// is returned.
///
/// The to use when making
/// comparisons.
protected virtual IComparer GetComparer()
{
return Comparer.Default;
}
}
}