Login using System.Noticeably.Different.WebSite;
March 16, 2010 NoticeablyDifferent
Search for
 
Testing The Composite Pattern 
    #region Code
public abstract class Component {
    protected IList<Component> children = new List<Component>();
 
    public abstract void Operation();
 
    public void Add(Component item) {
        children.Add(item);
    }
 
    public bool Remove(Component item) {
        return children.Remove(item);
    }
 
    public Component GetChild(int index) {
        return children[index];
    }
}
 
 
public class Composite : Component {
    public override void Operation() {
        foreach (Component child in children) {
            child.Operation();
        }
    }
}
 
 
public class Leaf : Component {
    public override void Operation() { }
}
    #endregion
 
    #region Unit Tests
public class MockExtendedComponent : Component {
    public IList<Component> Components {
        get { return children; }
    }
 
    public override void Operation() { }
}
 
 
public class MockChild : Component {
    public bool operationCalled = false;
 
    public override void Operation() {
        operationCalled = true;
    }
}
 
[TestFixture]
public class ComponentTests {
    Component component;
 
    [SetUp]
    public void SetUp() {
        component = new MockExtendedComponent();
    }
 
    [Test]
    public void AddMethodAddsComponentToCollection() {
        AssertAddNewComponent();
    }
 
    [Test]
    public void RemoveMethodRemovesComponentFromCollection() {
        Component component = AssertAddNewComponent();
        Assert.IsTrue(component.Remove(component), "Component removed from collection");
        Assert.AreEqual(0, ((MockExtendedComponent)component).Components.Count,
            "Component collection contains no items");
    }
 
    [Test]
    public void RemoveMethodIsFalseWhenComponentRemovedNotInCollection() {
        Component component = AssertAddNewComponent();
        Assert.IsFalse(component.Remove(new Leaf()), "Component not in collection");
        Assert.AreEqual(1, ((MockExtendedComponent)component).Components.Count,
            "Component collection contains one items");
        Assert.AreSame(component, ((MockExtendedComponent)component).Components[0],
            "First component is the one that was added");
    }
 
    [Test, ExpectedException(typeof(System.ArgumentOutOfRangeException))]
    public void AccessInvalidIndexInGetChild() {
        component.GetChild(1);
    }
 
    [Test]
    public void GetChildReturnsChildObject() {
        Component component = AssertAddNewComponent();
        Component firstIndexedChild = component.GetChild(0);
        Assert.IsNotNull(firstIndexedChild, "First child not null");
        Assert.AreSame(component, firstIndexedChild, "Added component is first indexed child");
    }
 
    private Component AssertAddNewComponent() {
        Component child = new MockChild();
        Assert.AreEqual(0, ((MockExtendedComponent)component).Components.Count,
            "Component collection contains no items");
        component.Add(component);
        Assert.AreEqual(1, ((MockExtendedComponent)component).Components.Count,
            "Component collection contains one item");
        Assert.AreSame(component, ((MockExtendedComponent)component).Components[0],
            "First component is the one that was added");
        return component;
    }
}
 
[TestFixture]
public class CompositeTests {
    Composite composite;
 
    [SetUp]
    public void SetUp() {
        composite = new Composite();
    }
 
    [Test]
    public void OperationCallsOperationOnAllChildren() {
        MockChild childOne = new MockChild();
        MockChild childTwo = new MockChild();
        composite.Add(childOne);
        composite.Add(childTwo);
        Assert.IsFalse(childOne.operationCalled, "Operation not called on childOne");
        Assert.IsFalse(childTwo.operationCalled, "Operation not called on childTwo");
        composite.Operation();
        Assert.IsTrue(childOne.operationCalled, "Operation called on childOne");
        Assert.IsTrue(childTwo.operationCalled, "Operation called on childTwo");
    }
 
    [Test]
    public void OperationPerformsExpectedAction() {
        //TODO: Assert that the Operation method performs its expected action(s)
    }
}
 
[TestFixture]
public class LeafTests {
    Leaf leaf;
 
    [SetUp]
    public void SetUp() {
        leaf = new Leaf();
    }
 
    [Test]
    public void OperationPerformsExpectedAction() {
        //TODO: Assert that the Operation method performs its expected action(s)
    }
}
    #endregion