I've created a simple composite pattern with a component defined as
public abstract class Component : IEnumerable<Component>
{
protected EntityComponent(int id)
{
Id = id;
}
public int Id { get; protected set; }
public abstract IEnumerator<EntityComponent> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
and its composite implementation as
public class Composite : Component
{
private readonly IList<Component> childComponents = new List<Component>();
public Composite(int id)
: base(id)
{
}
public int Count { get; set; }
public void Add(Component child)
{
childComponents.Add(child);
Count++;
}
public override IEnumerator<Component> GetEnumerator()
{
foreach (var childComponent in childComponents)
{
yield return childComponent;
foreach (var component in childComponent)
{
yield return component;
}
}
}
}
Now I'm setting up some tests using Fluent Assertions to assert whether two composite structures are equivalent, e.g.,
[Fact]
public void TestAssertions()
{
var a1 = new Composite(1);
var a2 = new Composite(2);
a1.Add(a2);
var b1 = new Composite(1);
var b2 = new Composite(2);
b1.Add(b2);
a1.ShouldBeEquivalentTo(b1);
}
This test passes as expected. But if I change one of the properties on one of the composites, i.e.,
[Fact]
public void TestAssertions()
{
var a1 = new Composite(1);
var a2 = new Composite(2);
a1.Add(a2);
var b1 = new Composite(101);
var b2 = new Composite(2);
b1.Add(b2);
a1.ShouldBeEquivalentTo(b1);
}
This test still passes, but shouldn't it fail?
Composite
doesn't expose any properties that FA can traverse, hence it will only look at theCount
.