using System.Collections.Generic; using Chernobyl.Values; using NUnit.Framework; using System.Utility; namespace Chernobyl.DesignPatterns.Extension { [TestFixture, Description("Tests for the Attach type.")] public class AttachTests { [Test, Description("Ensures AttachExtensions.Attach(IValue, IValue>) " + "attaches the IExtension to the IExtendable immediately.")] public void AttachAttachesImmediately() { IContainer extendable = new DynamicContainer(new Extendable()); IContainer> extension = new DynamicContainer>(new Extension()); extendable.Attach(extension); extension.Value.Extended.IsEqualTo(extendable.Value, "IExtension.Extended."); } [Test, Description("Ensures AttachExtensions.Attach(IValue, IValue>) " + "attaches/detaches the IExtension to/from the IExtendable if the " + "IExtendable changes.")] public void AttachAttachesAndDetachesIfExtendableChanges() { IExtendable previousExtendable = new Extendable(); IDynamicContainer extendable = new DynamicContainer(previousExtendable); IContainer> extension = new DynamicContainer>(new Extension()); extendable.Attach(extension); extension.Value.Extended.IsEqualTo(extendable.Value, "IExtension.Extended."); IExtendable newExtendable = new Extendable(); extendable.Value = newExtendable; extension.Value.Extended.IsEqualTo(newExtendable, "IExtension.Extended."); } [Test, Description("Ensures AttachExtensions.Attach(IValue, IValue>) " + "attaches/detaches the IExtension to/from the IExtendable if the " + "IExtension changes.")] public void AttachAttachesAndDetachesIfExtensionChanges() { IExtension previousExtension = new Extension(); IContainer extendable = new DynamicContainer(new Extendable()); IDynamicContainer> extension = new DynamicContainer>(previousExtension); extendable.Attach(extension); previousExtension.Extended.IsEqualTo(extendable.Value, "IExtension.Extended."); IExtension newExtension = new Extension(); extension.Value = newExtension; previousExtension.Extended.IsEqualTo(null, "IExtension.Extended."); newExtension.Extended.IsEqualTo(extendable.Value, "IExtension.Extended."); } /// /// A helper type used for testing purposes. /// public class Extendable : IExtendable { public IList Extensions { get { return _extensions; } } IList _extensions = new List(); } /// /// A helper type used for testing purposes. /// public class Extension : Extension { protected override void AttachTo(IExtendable extended) { extended.Extensions.Add(this); } protected override void DetachFrom(IExtendable extended) { extended.Extensions.Remove(this); } } } }