using System; using System.Collections.Generic; using System.Linq; using NUnit.Framework; namespace Chernobyl.Reflection.Template { /// /// A helper base testing class for testing types that implement the /// interface. /// /// Note to implementors: the instance created by the /// method must be named "test_name" /// so that the test method may pass. /// public abstract class InstanceTests : ComponentTests { [Test, Description("Tests the IInstance to see if it can contain an " + "IComponent that is a constructor.")] public void InstanceWithConstructorTest() { IInstance instance = CreateInstanceWithConstructor(); IMember constructor = instance.ComponentChildren.OfType().First(); Assert.IsNotNull(constructor, "A constructor was not created"); } [Test, Description("Tests the IInstance to see if it can contain an " + "IComponent that is a method.")] public void InstanceWithMethodTest() { IInstance instance = CreateInstanceWithMethod(); IMember method = instance.ComponentChildren.OfType().First(); Assert.IsNotNull(method, "A method was not created"); } [Test, Description("Tests the IInstance to see if it can contain an " + "IComponent that is a property.")] public void InstanceWithPropertyTest() { IInstance instance = CreateInstanceWithProperty(); IMember property = instance.ComponentChildren.OfType().First(); Assert.IsNotNull(property, "A property was not created"); } [Test, Description("Tests the IInstance to see if it can contain an " + "IComponent that is a field.")] public void InstanceWithFieldTest() { IInstance instance = CreateInstanceWithField(); IMember field = instance.ComponentChildren.OfType().First(); Assert.IsNotNull(field, "A property was not created"); } [Test, Description("Tests the IInstance to see if it can contain an " + "IComponent that is a event.")] public void InstanceWithEventTest() { IInstance instance = CreateInstanceWithEvent(); IMember eventMember = instance.ComponentChildren.OfType().First(); Assert.IsNotNull(eventMember, "An eventMember was not created"); } [Test, Description("Tests to see if the instance return by " + "CreateLinkedInstance() is equal to the instance created by " + "CreateLinkedToInstance().")] public void LinkedInstanceTest() { IInstance linkedInstance = CreateLinkedInstance(); IInstance actualLinkedToInstance = CreateLinkedToInstance(); Assert.AreEqual(actualLinkedToInstance.TheInstance, linkedInstance.TheInstance); } [Test, Description("Tests to see if the instance return by " + "CreateTypedInstance() is an instance of type " + "KeyValuePair.")] public void TypedInstanceTest() { IInstance instance = CreateTypedInstance(); Assert.AreEqual(typeof(Dictionary>), instance.Type); } /// /// Creates an that contains an /// that has a constructor. This method should /// not return the same more than once. /// /// The IInstance to be tested. protected abstract IInstance CreateInstanceWithConstructor(); /// /// Creates an that contains an /// that has a method. This method should not /// return the same more than once. /// /// The IInstance to be tested. protected abstract IInstance CreateInstanceWithMethod(); /// /// Creates an that contains an /// that has a property. This method should not /// return the same more than once. /// /// The IInstance to be tested. protected abstract IInstance CreateInstanceWithProperty(); /// /// Creates an that contains an /// that has a field. This method should not /// return the same more than once. /// /// The IInstance to be tested. protected abstract IInstance CreateInstanceWithField(); /// /// Creates an that contains an /// that has a event. This method should not /// return the same more than once. /// /// The IInstance to be tested. protected abstract IInstance CreateInstanceWithEvent(); /// /// Creates an that is to be tested to ensure it /// properly links to the created by /// . This method will always create /// a new and never return the same instance /// twice. The created should have a name that /// is different from the created by /// . /// /// The instance that is to be tested. protected abstract IInstance CreateLinkedInstance(); /// /// Creates an that is to be tested to ensure /// it properly links from the created by /// . This method will always create /// a new and never return the same instance /// twice. /// /// The instance that is to be tested. protected abstract IInstance CreateLinkedToInstance(); /// /// Creates an that is to be tested to ensure /// it is of type where the /// key type is of and the value type is of /// . The 's type /// parameter should be of type . This method will /// always create a new and never return the /// same instance twice. /// /// The instance that is to be tested. protected abstract IInstance CreateTypedInstance(); /// /// The name that is expected by the /// test method. If the value /// returned is an empty string, then the test is ignored. /// protected override string ExpectedName { get { return "test_name"; } } } /// /// A type used to test some functionality of s. /// public class TestType { public TestType() { } public TestType(string y) : this(0.0f, y) { } public TestType(float x, string y) { X = x; Y = y; } public event EventHandler SomeEvent; public void SomeEventHandler(object sender, EventArgs e) { X = 0; } public float X; public void SetChild(TestType child) { Child = child; } public TestType Child { get { return ChildField; } set { ChildField = value; } } public TestType ChildField; public string Y { get; set; } public int SomeProperty { get; set; } public void SomeMethod(float x) { X = x; if (SomeEvent != null) SomeEvent(this, EventArgs.Empty); } } }