1

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?

James B
  • 8,975
  • 13
  • 45
  • 83

1 Answers1

0

Composite doesn't expose any properties that FA can traverse, hence it will only look at the Count.

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • Shouldn't it be able to traverse composite since it implements IEnumerable? Or should I be setting this up differently? – James B Sep 13 '17 at 14:29
  • Your example doesn't compile, so I can't try it myself. BTW, which version of FA are you using? – Dennis Doomen Sep 13 '17 at 18:07
  • Updated composite class as to why it may not compile (removed string parameter in constructor). Hopefully will compile now, but can't test as no PC available to me at present. From memory, I was using the latest stable release of FA – James B Sep 18 '17 at 08:47