[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), );
Assert.AreEqual(0, ((MockExtendedComponent)component).Components.Count,
);
}
[Test]
public void RemoveMethodIsFalseWhenComponentRemovedNotInCollection() {
Component component = AssertAddNewComponent();
Assert.IsFalse(component.Remove(new Leaf()), );
Assert.AreEqual(1, ((MockExtendedComponent)component).Components.Count,
);
Assert.AreSame(component, ((MockExtendedComponent)component).Components[0],
);
}
[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, );
Assert.AreSame(component, firstIndexedChild, );
}
private Component AssertAddNewComponent() {
Component child = new MockChild();
Assert.AreEqual(0, ((MockExtendedComponent)component).Components.Count,
);
component.Add(component);
Assert.AreEqual(1, ((MockExtendedComponent)component).Components.Count,
);
Assert.AreSame(component, ((MockExtendedComponent)component).Components[0],
);
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, );
Assert.IsFalse(childTwo.operationCalled, );
composite.Operation();
Assert.IsTrue(childOne.operationCalled, );
Assert.IsTrue(childTwo.operationCalled, );
}
[Test]
public void OperationPerformsExpectedAction() {
}
}