using System.Collections.Generic; using System.Linq; using NUnit.Framework; namespace Chernobyl.Reflection.Template { /// /// A helper base test class for testing types that implement the /// interface. /// public abstract class MemberTests : ComponentTests { [Test, Description("A test that ensures the arguments of the IMember " + "are correct.")] public void MemberWithArgumentsTest() { IMember member = CreateMemberWithArguments(); IEnumerable arguments = member.ComponentChildren.OfType(); int argumentCount = arguments.Count(); Assert.AreEqual(1, argumentCount, "There must be at least one " + "argument in the member returned by CreateMemberWithArguments()."); Assert.AreEqual(Arguments.Length, argumentCount, "The number of arguments does not match what was expected."); for (int i = 0; i < argumentCount; ++i) { Assert.AreEqual(Arguments[i], arguments.ElementAt(i).TheInstance, "The arguments at position " + i + " are not equal."); } } [Test, Description("A test that ensures the argument of the IMember " + "properly links to another instance.")] public void LinkedMemberTest() { IMember memberWithLink = CreateLinkedMember(); IInstance actualLinkedToInstance = CreateLinkedToInstance(); IEnumerable arguments = memberWithLink.ComponentChildren.OfType(); if(arguments.Any() == true) { IInstance linkedInstance = memberWithLink.ComponentChildren.OfType().First(); Assert.IsInstanceOf(linkedInstance); Assert.AreEqual(((LinkedInstance)linkedInstance).Linked.Type, actualLinkedToInstance.Type); } else Assert.AreSame(((TestType)memberWithLink.Instance.TheInstance).Child, actualLinkedToInstance.TheInstance); } /// /// Creates an which contains at least one /// argument that matches with the arguments in the /// property. s created by /// this method should not be used return more than once. /// /// The to test. protected abstract IMember CreateMemberWithArguments(); /// /// Creates an that is to be tested to ensure it /// properly contains an argument that links to 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 IMember CreateLinkedMember(); /// /// Creates an that is to be tested to ensure /// it is equal to the argument contained within /// the created by /// . This method will always create /// a new and never return the same instance /// twice. /// /// The instance that is to be tested for equality against the /// argument contained within the created by /// . protected abstract IInstance CreateLinkedToInstance(); /// /// s which are equal to the arguments in the /// returned by /// . /// protected abstract object[] Arguments { get; } /// /// 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 CreateMemberWithArguments(); } } }