Example #1
0
    void when_created()
    {
        IContext <TestEntity> ctx = null;

        before = () => {
            ctx = new MyTestContext();
        };

        it["increments creationIndex"] = () => {
            ctx.CreateEntity().creationIndex.should_be(0);
            ctx.CreateEntity().creationIndex.should_be(1);
        };

        it["starts with given creationIndex"] = () => {
            new MyTestContext(CID.TotalComponents, 42, null).CreateEntity().creationIndex.should_be(42);
        };

        it["has no entities when no entities were created"] = () => {
            ctx.GetEntities().should_be_empty();
        };

        it["gets total entity count"] = () => {
            ctx.count.should_be(0);
        };

        it["creates entity"] = () => {
            var e = ctx.CreateEntity();
            e.should_not_be_null();
            e.GetType().should_be(typeof(TestEntity));

            e.totalComponents.should_be(ctx.totalComponents);
            e.isEnabled.should_be_true();
        };

        it["has default ContextInfo"] = () => {
            ctx.contextInfo.name.should_be("Unnamed Context");
            ctx.contextInfo.componentNames.Length.should_be(CID.TotalComponents);
            for (int i = 0; i < ctx.contextInfo.componentNames.Length; i++)
            {
                ctx.contextInfo.componentNames[i].should_be("Index " + i);
            }
        };

        it["creates component pools"] = () => {
            ctx.componentPools.should_not_be_null();
            ctx.componentPools.Length.should_be(CID.TotalComponents);
        };

        it["creates entity with component pools"] = () => {
            var e = ctx.CreateEntity();
            e.componentPools.should_be_same(ctx.componentPools);
        };

        it["throws when destroying an entity which the context doesn't contain"] = expect <ContextDoesNotContainEntityException>(() => {
            var e = ctx.CreateEntity();
            ctx.DestroyEntity(e);
            ctx.DestroyEntity(e);
        });

        it["can ToString"] = () => {
            ctx.ToString().should_be("Unnamed Context");
        };

        context["when ContextInfo set"] = () => {
            ContextInfo contextInfo = null;

            before = () => {
                var componentNames = new [] { "Health", "Position", "View" };
                var componentTypes = new [] { typeof(ComponentA), typeof(ComponentB), typeof(ComponentC) };
                contextInfo = new ContextInfo("My Context", componentNames, componentTypes);
                ctx         = new MyTestContext(componentNames.Length, 0, contextInfo);
            };

            it["has custom ContextInfo"] = () => {
                ctx.contextInfo.should_be_same(contextInfo);
            };

            it["creates entity with same ContextInfo"] = () => {
                ctx.CreateEntity().contextInfo.should_be_same(contextInfo);
            };

            it["throws when componentNames is not same length as totalComponents"] = expect <ContextInfoException>(() => {
                new MyTestContext(contextInfo.componentNames.Length + 1, 0, contextInfo);
            });
        };

        context["when entity created"] = () => {
            TestEntity e = null;

            before = () => {
                e = ctx.CreateEntity();
                e.AddComponentA();
            };

            it["gets total entity count"] = () => {
                ctx.count.should_be(1);
            };

            it["has entities that were created with CreateEntity()"] = () => {
                ctx.HasEntity(e).should_be_true();
            };

            it["doesn't have entities that were not created with CreateEntity()"] = () => {
                ctx.HasEntity(this.CreateEntity()).should_be_false();
            };

            it["returns all created entities"] = () => {
                var e2       = ctx.CreateEntity();
                var entities = ctx.GetEntities();
                entities.Length.should_be(2);
                entities.should_contain(e);
                entities.should_contain(e2);
            };

            it["destroys entity and removes it"] = () => {
                ctx.DestroyEntity(e);
                ctx.HasEntity(e).should_be_false();
                ctx.count.should_be(0);
                ctx.GetEntities().should_be_empty();
            };

            it["destroys an entity and removes all its components"] = () => {
                ctx.DestroyEntity(e);
                e.GetComponents().should_be_empty();
            };

            it["destroys all entities"] = () => {
                ctx.CreateEntity();
                ctx.DestroyAllEntities();
                ctx.HasEntity(e).should_be_false();
                ctx.count.should_be(0);
                ctx.GetEntities().should_be_empty();
                e.GetComponents().should_be_empty();
            };

            it["ensures same deterministic order when getting entities after destroying all entities"] = () => {
                // This is a Unity specific problem. Run Unity Test Tools with in the Entitas.Unity project

                const int numEntities = 10;

                for (int i = 0; i < numEntities; i++)
                {
                    ctx.CreateEntity();
                }

                var order1    = new int[numEntities];
                var entities1 = ctx.GetEntities();
                for (int i = 0; i < numEntities; i++)
                {
                    order1[i] = entities1[i].creationIndex;
                }

                ctx.DestroyAllEntities();
                ctx.ResetCreationIndex();

                for (int i = 0; i < numEntities; i++)
                {
                    ctx.CreateEntity();
                }

                var order2    = new int[numEntities];
                var entities2 = ctx.GetEntities();
                for (int i = 0; i < numEntities; i++)
                {
                    order2[i] = entities2[i].creationIndex;
                }

                for (int i = 0; i < numEntities; i++)
                {
                    var index1 = order1[i];
                    var index2 = order2[i];
                    index1.should_be(index2);
                }
            };

            it["throws when destroying all entities and there are still entities retained"] = expect <ContextStillHasRetainedEntitiesException>(() => {
                ctx.CreateEntity().Retain(new object());
                ctx.DestroyAllEntities();
            });
        };

        context["internal caching"] = () => {
            it["caches entities"] = () => {
                var entities = ctx.GetEntities();
                ctx.GetEntities().should_be_same(entities);
            };

            it["updates entities cache when creating an entity"] = () => {
                var entities = ctx.GetEntities();
                ctx.CreateEntity();
                ctx.GetEntities().should_not_be_same(entities);
            };

            it["updates entities cache when destroying an entity"] = () => {
                var e        = ctx.CreateEntity();
                var entities = ctx.GetEntities();
                ctx.DestroyEntity(e);
                ctx.GetEntities().should_not_be_same(entities);
            };
        };

        context["events"] = () => {
            var didDispatch = 0;

            before = () => {
                didDispatch = 0;
            };

            it["dispatches OnEntityCreated when creating a new entity"] = () => {
                IEntity eventEntity = null;
                ctx.OnEntityCreated += (p, entity) => {
                    didDispatch += 1;
                    eventEntity  = entity;
                    p.should_be_same(p);
                };

                var e = ctx.CreateEntity();
                didDispatch.should_be(1);
                eventEntity.should_be_same(e);
            };

            it["dispatches OnEntityWillBeDestroyed when destroying an entity"] = () => {
                var e = ctx.CreateEntity();
                e.AddComponentA();
                ctx.OnEntityWillBeDestroyed += (c, entity) => {
                    didDispatch += 1;
                    c.should_be_same(ctx);
                    entity.should_be_same(e);
                    entity.HasComponentA().should_be_true();
                    entity.isEnabled.should_be_true();

                    ((IContext <TestEntity>)c).GetEntities().Length.should_be(0);
                };
                ctx.GetEntities();
                ctx.DestroyEntity(e);
                didDispatch.should_be(1);
            };

            it["dispatches OnEntityDestroyed when destroying an entity"] = () => {
                var e = ctx.CreateEntity();
                ctx.OnEntityDestroyed += (p, entity) => {
                    didDispatch += 1;
                    p.should_be_same(ctx);
                    entity.should_be_same(e);
                    entity.HasComponentA().should_be_false();
                    entity.isEnabled.should_be_false();
                };
                ctx.DestroyEntity(e);
                didDispatch.should_be(1);
            };

            it["entity is released after OnEntityDestroyed"] = () => {
                var e = ctx.CreateEntity();
                ctx.OnEntityDestroyed += (p, entity) => {
                    didDispatch += 1;
                    entity.retainCount.should_be(1);
                    var newEntity = ctx.CreateEntity();
                    newEntity.should_not_be_null();
                    newEntity.should_not_be_same(entity);
                };
                ctx.DestroyEntity(e);
                var reusedEntity = ctx.CreateEntity();
                reusedEntity.should_be_same(e);
                didDispatch.should_be(1);
            };

            it["throws if entity is released before it is destroyed"] = expect <EntityIsNotDestroyedException>(() => {
                var e = ctx.CreateEntity();
                e.Release(ctx);
            });

            it["dispatches OnGroupCreated when creating a new group"] = () => {
                IGroup eventGroup = null;
                ctx.OnGroupCreated += (p, g) => {
                    didDispatch += 1;
                    p.should_be_same(ctx);
                    eventGroup = g;
                };
                var group = ctx.GetGroup(Matcher <TestEntity> .AllOf(0));
                didDispatch.should_be(1);
                eventGroup.should_be_same(group);
            };

            it["doesn't dispatch OnGroupCreated when group alredy exists"] = () => {
                ctx.GetGroup(Matcher <TestEntity> .AllOf(0));
                ctx.OnGroupCreated += delegate { this.Fail(); };
                ctx.GetGroup(Matcher <TestEntity> .AllOf(0));
            };

            it["dispatches OnGroupCleared when clearing groups"] = () => {
                IGroup eventGroup = null;
                ctx.OnGroupCleared += (p, g) => {
                    didDispatch += 1;
                    p.should_be_same(ctx);
                    eventGroup = g;
                };
                ctx.GetGroup(Matcher <TestEntity> .AllOf(0));
                var group2 = ctx.GetGroup(Matcher <TestEntity> .AllOf(1));
                ctx.ClearGroups();

                didDispatch.should_be(2);
                eventGroup.should_be_same(group2);
            };

            it["removes all external delegates when destroying an entity"] = () => {
                var e = ctx.CreateEntity();
                e.OnComponentAdded    += delegate { this.Fail(); };
                e.OnComponentRemoved  += delegate { this.Fail(); };
                e.OnComponentReplaced += delegate { this.Fail(); };
                ctx.DestroyEntity(e);
                var e2 = ctx.CreateEntity();
                e2.should_be_same(e);
                e2.AddComponentA();
                e2.ReplaceComponentA(Component.A);
                e2.RemoveComponentA();
            };

            it["will not remove external delegates for OnEntityReleased"] = () => {
                var e          = ctx.CreateEntity();
                var didRelease = 0;
                e.OnEntityReleased += entity => didRelease += 1;
                ctx.DestroyEntity(e);
                didRelease.should_be(1);
            };

            it["removes all external delegates from OnEntityReleased when after being dispatched"] = () => {
                var e          = ctx.CreateEntity();
                var didRelease = 0;
                e.OnEntityReleased += entity => didRelease += 1;
                ctx.DestroyEntity(e);
                e.Retain(this);
                e.Release(this);
                didRelease.should_be(1);
            };

            it["removes all external delegates from OnEntityReleased after being dispatched (when delayed release)"] = () => {
                var e          = ctx.CreateEntity();
                var didRelease = 0;
                e.OnEntityReleased += entity => didRelease += 1;
                e.Retain(this);
                ctx.DestroyEntity(e);
                didRelease.should_be(0);
                e.Release(this);
                didRelease.should_be(1);

                e.Retain(this);
                e.Release(this);
                didRelease.should_be(1);
            };
        };

        context["entity pool"] = () => {
            it["gets entity from object pool"] = () => {
                var e = ctx.CreateEntity();
                e.should_not_be_null();
                e.GetType().should_be(typeof(TestEntity));
            };

            it["destroys entity when pushing back to object pool"] = () => {
                var e = ctx.CreateEntity();
                e.AddComponentA();
                ctx.DestroyEntity(e);
                e.HasComponent(CID.ComponentA).should_be_false();
            };

            it["returns pushed entity"] = () => {
                var e = ctx.CreateEntity();
                e.AddComponentA();
                ctx.DestroyEntity(e);
                var entity = ctx.CreateEntity();
                entity.HasComponent(CID.ComponentA).should_be_false();
                entity.should_be_same(e);
            };

            it["only returns released entities"] = () => {
                var e1 = ctx.CreateEntity();
                e1.Retain(this);
                ctx.DestroyEntity(e1);
                var e2 = ctx.CreateEntity();
                e2.should_not_be_same(e1);
                e1.Release(this);
                var e3 = ctx.CreateEntity();
                e3.should_be_same(e1);
            };

            it["returns new entity"] = () => {
                var e1 = ctx.CreateEntity();
                e1.AddComponentA();
                ctx.DestroyEntity(e1);
                ctx.CreateEntity();
                var e2 = ctx.CreateEntity();
                e2.HasComponent(CID.ComponentA).should_be_false();
                e2.should_not_be_same(e1);
            };

            it["sets up entity from pool"] = () => {
                var e             = ctx.CreateEntity();
                var creationIndex = e.creationIndex;
                ctx.DestroyEntity(e);
                var g = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA));

                e = ctx.CreateEntity();
                e.creationIndex.should_be(creationIndex + 1);
                e.isEnabled.should_be_true();

                e.AddComponentA();
                g.GetEntities().should_contain(e);
            };

            context["when entity gets destroyed"] = () => {
                TestEntity e = null;

                before = () => {
                    e = ctx.CreateEntity();
                    e.AddComponentA();
                    ctx.DestroyEntity(e);
                };

                it["throws when adding component"]              = expect <EntityIsNotEnabledException>(() => e.AddComponentA());
                it["throws when removing component"]            = expect <EntityIsNotEnabledException>(() => e.RemoveComponentA());
                it["throws when replacing component"]           = expect <EntityIsNotEnabledException>(() => e.ReplaceComponentA(new ComponentA()));
                it["throws when replacing component with null"] = expect <EntityIsNotEnabledException>(() => e.ReplaceComponentA(null));
            };
        };

        context["groups"] = () => {
            it["gets empty group for matcher when no entities were created"] = () => {
                var g = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA));
                g.should_not_be_null();
                g.GetEntities().should_be_empty();
            };

            context["when entities created"] = () => {
                TestEntity eAB1 = null;
                TestEntity eAB2 = null;
                TestEntity eA   = null;

                IMatcher <TestEntity> matcherAB = Matcher <TestEntity> .AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB
                });

                before = () => {
                    eAB1 = ctx.CreateEntity();
                    eAB1.AddComponentA();
                    eAB1.AddComponentB();

                    eAB2 = ctx.CreateEntity();
                    eAB2.AddComponentA();
                    eAB2.AddComponentB();

                    eA = ctx.CreateEntity();
                    eA.AddComponentA();
                };

                it["gets group with matching entities"] = () => {
                    var g = ctx.GetGroup(matcherAB).GetEntities();
                    g.Length.should_be(2);
                    g.should_contain(eAB1);
                    g.should_contain(eAB2);
                };

                it["gets cached group"] = () => {
                    ctx.GetGroup(matcherAB).should_be_same(ctx.GetGroup(matcherAB));
                };

                it["cached group contains newly created matching entity"] = () => {
                    var g = ctx.GetGroup(matcherAB);
                    eA.AddComponentB();
                    g.GetEntities().should_contain(eA);
                };

                it["cached group doesn't contain entity which are not matching anymore"] = () => {
                    var g = ctx.GetGroup(matcherAB);
                    eAB1.RemoveComponentA();
                    g.GetEntities().should_not_contain(eAB1);
                };

                it["removes destroyed entity"] = () => {
                    var g = ctx.GetGroup(matcherAB);
                    ctx.DestroyEntity(eAB1);
                    g.GetEntities().should_not_contain(eAB1);
                };

                it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => {
                    var g = ctx.GetGroup(matcherAB);
                    var didDispatchRemoved = 0;
                    var didDispatchAdded   = 0;
                    var componentA         = new ComponentA();
                    g.OnEntityRemoved += (group, entity, index, component) => {
                        group.should_be_same(g);
                        entity.should_be_same(eAB1);
                        index.should_be(CID.ComponentA);
                        component.should_be_same(Component.A);
                        didDispatchRemoved++;
                    };
                    g.OnEntityAdded += (group, entity, index, component) => {
                        group.should_be_same(g);
                        entity.should_be_same(eAB1);
                        index.should_be(CID.ComponentA);
                        component.should_be_same(componentA);
                        didDispatchAdded++;
                    };
                    eAB1.ReplaceComponentA(componentA);

                    didDispatchRemoved.should_be(1);
                    didDispatchAdded.should_be(1);
                };

                it["group dispatches OnEntityUpdated with previous and current component when replacing a component"] = () => {
                    var updated  = 0;
                    var prevComp = eA.GetComponent(CID.ComponentA);
                    var newComp  = new ComponentA();
                    var g        = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA));
                    g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => {
                        updated += 1;
                        group.should_be_same(g);
                        entity.should_be_same(eA);
                        index.should_be(CID.ComponentA);
                        previousComponent.should_be_same(prevComp);
                        newComponent.should_be_same(newComp);
                    };

                    eA.ReplaceComponent(CID.ComponentA, newComp);

                    updated.should_be(1);
                };

                it["group with matcher NoneOf doesn't dispatch OnEntityAdded when destroying entity"] = () => {
                    var e = ctx.CreateEntity()
                            .AddComponentA()
                            .AddComponentB();
                    var matcher = Matcher <TestEntity> .AllOf(CID.ComponentB).NoneOf(CID.ComponentA);

                    var g = ctx.GetGroup(matcher);
                    g.OnEntityAdded += delegate { this.Fail(); };
                    ctx.DestroyEntity(e);
                };

                context["event timing"] = () => {
                    before = () => {
                        ctx = new MyTestContext();
                    };

                    it["dispatches group.OnEntityAdded events after all groups are updated"] = () => {
                        var groupA = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA, CID.ComponentB));
                        var groupB = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentB));

                        groupA.OnEntityAdded += delegate {
                            groupB.count.should_be(1);
                        };

                        var entity = ctx.CreateEntity();
                        entity.AddComponentA();
                        entity.AddComponentB();
                    };

                    it["dispatches group.OnEntityRemoved events after all groups are updated"] = () => {
                        ctx = new MyTestContext();
                        var groupB  = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentB));
                        var groupAB = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA, CID.ComponentB));

                        groupB.OnEntityRemoved += delegate {
                            groupAB.count.should_be(0);
                        };

                        var entity = ctx.CreateEntity();
                        entity.AddComponentA();
                        entity.AddComponentB();

                        entity.RemoveComponentB();
                    };
                };
            };
        };

        context["EntityIndex"] = () => {
            it["throws when EntityIndex for key doesn't exist"] = expect <ContextEntityIndexDoesNotExistException>(() => {
                ctx.GetEntityIndex("unknown");
            });

            it["adds and EntityIndex"] = () => {
                const int componentIndex = 1;
                var       entityIndex    = new PrimaryEntityIndex <TestEntity, string>(ctx.GetGroup(Matcher <TestEntity> .AllOf(componentIndex)), (arg1, arg2) => string.Empty);
                ctx.AddEntityIndex(componentIndex.ToString(), entityIndex);
                ctx.GetEntityIndex(componentIndex.ToString()).should_be_same(entityIndex);
            };

            it["throws when adding an EntityIndex with same name"] = expect <ContextEntityIndexDoesAlreadyExistException>(() => {
                const int componentIndex = 1;
                var entityIndex          = new PrimaryEntityIndex <TestEntity, string>(ctx.GetGroup(Matcher <TestEntity> .AllOf(componentIndex)), (arg1, arg2) => string.Empty);
                ctx.AddEntityIndex(componentIndex.ToString(), entityIndex);
                ctx.AddEntityIndex(componentIndex.ToString(), entityIndex);
            });
        };

        context["reset"] = () => {
            context["groups"] = () => {
                it["resets and removes groups from context"] = () => {
                    var m = Matcher <TestEntity> .AllOf(CID.ComponentA);

                    var    groupsCreated = 0;
                    IGroup createdGroup  = null;
                    ctx.OnGroupCreated += (p, g) => {
                        groupsCreated += 1;
                        createdGroup   = g;
                    };

                    var initialGroup = ctx.GetGroup(m);

                    ctx.ClearGroups();

                    ctx.GetGroup(m);

                    ctx.CreateEntity().AddComponentA();

                    groupsCreated.should_be(2);
                    createdGroup.should_not_be_same(initialGroup);

                    initialGroup.count.should_be(0);
                    createdGroup.count.should_be(1);
                };

                it["removes all event handlers from groups"] = () => {
                    var m = Matcher <TestEntity> .AllOf(CID.ComponentA);

                    var group = ctx.GetGroup(m);

                    group.OnEntityAdded += delegate { this.Fail(); };

                    ctx.ClearGroups();

                    var e = ctx.CreateEntity();
                    e.AddComponentA();
                    group.HandleEntity(e, CID.ComponentA, Component.A);
                };

                it["releases entities in groups"] = () => {
                    var m = Matcher <TestEntity> .AllOf(CID.ComponentA);

                    ctx.GetGroup(m);
                    var entity = ctx.CreateEntity();
                    entity.AddComponentA();

                    ctx.ClearGroups();

                    entity.retainCount.should_be(1);
                };
            };

            context["context"] = () => {
                it["resets creation index"] = () => {
                    ctx.CreateEntity();

                    ctx.ResetCreationIndex();

                    ctx.CreateEntity().creationIndex.should_be(0);
                };


                context["removes all event handlers"] = () => {
                    it["removes OnEntityCreated"] = () => {
                        ctx.OnEntityCreated += delegate { this.Fail(); };
                        ctx.Reset();

                        ctx.CreateEntity();
                    };

                    it["removes OnEntityWillBeDestroyed"] = () => {
                        ctx.OnEntityWillBeDestroyed += delegate { this.Fail(); };
                        ctx.Reset();

                        ctx.DestroyEntity(ctx.CreateEntity());
                    };

                    it["removes OnEntityDestroyed"] = () => {
                        ctx.OnEntityDestroyed += delegate { this.Fail(); };
                        ctx.Reset();

                        ctx.DestroyEntity(ctx.CreateEntity());
                    };

                    it["removes OnGroupCreated"] = () => {
                        ctx.OnGroupCreated += delegate { this.Fail(); };
                        ctx.Reset();

                        ctx.GetGroup(Matcher <TestEntity> .AllOf(0));
                    };

                    it["removes OnGroupCleared"] = () => {
                        ctx.OnGroupCleared += delegate { this.Fail(); };
                        ctx.Reset();
                        ctx.GetGroup(Matcher <TestEntity> .AllOf(0));

                        ctx.ClearGroups();
                    };
                };
            };

            context["component pools"] = () => {
                before = () => {
                    var entity = ctx.CreateEntity();
                    entity.AddComponentA();
                    entity.AddComponentB();
                    entity.RemoveComponentA();
                    entity.RemoveComponentB();
                };

                it["clears all component pools"] = () => {
                    ctx.componentPools[CID.ComponentA].Count.should_be(1);
                    ctx.componentPools[CID.ComponentB].Count.should_be(1);

                    ctx.ClearComponentPools();

                    ctx.componentPools[CID.ComponentA].Count.should_be(0);
                    ctx.componentPools[CID.ComponentB].Count.should_be(0);
                };

                it["clears a specific component pool"] = () => {
                    ctx.ClearComponentPool(CID.ComponentB);

                    ctx.componentPools[CID.ComponentA].Count.should_be(1);
                    ctx.componentPools[CID.ComponentB].Count.should_be(0);
                };

                it["only clears existing component pool"] = () => {
                    ctx.ClearComponentPool(CID.ComponentC);
                };
            };

            context["EntityIndex"] = () => {
                PrimaryEntityIndex <TestEntity, string> entityIndex = null;

                before = () => {
                    entityIndex = new PrimaryEntityIndex <TestEntity, string>(ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA)),
                                                                              (e, c) => ((NameAgeComponent)(c)).name);
                    ctx.AddEntityIndex(CID.ComponentA.ToString(), entityIndex);
                };

                it["deactivates EntityIndex"] = () => {
                    var nameAgeComponent = new NameAgeComponent();
                    nameAgeComponent.name = "Max";

                    ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
                    entityIndex.HasEntity("Max").should_be_true();

                    ctx.DeactivateAndRemoveEntityIndices();

                    entityIndex.HasEntity("Max").should_be_false();
                };

                it["removes EntityIndex"] = expect <ContextEntityIndexDoesNotExistException>(() => {
                    ctx.DeactivateAndRemoveEntityIndices();
                    ctx.GetEntityIndex(CID.ComponentA.ToString());
                });
            };
        };

        context["EntitasCache"] = () => {
            it["pops new list from list pool"] = () => {
                var groupA   = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA));
                var groupAB  = ctx.GetGroup(Matcher <TestEntity> .AnyOf(CID.ComponentA, CID.ComponentB));
                var groupABC = ctx.GetGroup(Matcher <TestEntity> .AnyOf(CID.ComponentA, CID.ComponentB, CID.ComponentC));

                var didExecute = 0;

                groupA.OnEntityAdded += (g, entity, index, component) => {
                    didExecute += 1;
                    entity.RemoveComponentA();
                };

                groupAB.OnEntityAdded += (g, entity, index, component) => {
                    didExecute += 1;
                };

                groupABC.OnEntityAdded += (g, entity, index, component) => {
                    didExecute += 1;
                };

                ctx.CreateEntity().AddComponentA();

                didExecute.should_be(3);
            };
        };
    }
    void when_index()
    {
        context["single key"] = () => {
            EntityIndex <TestEntity, string> index = null;
            IContext <TestEntity>            ctx   = null;
            IGroup <TestEntity> group = null;

            before = () => {
                ctx   = new MyTestContext();
                group = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA));
                index = new EntityIndex <TestEntity, string>("TestIndex", group, (e, c) => {
                    var nameAge = c as NameAgeComponent;
                    return(nameAge != null
                        ? nameAge.name
                        : ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name);
                });
            };

            context["when entity for key doesn't exist"] = () => {
                it["has no entities"] = () => {
                    index.GetEntities("unknownKey").should_be_empty();
                };
            };

            context["when entity for key exists"] = () => {
                const string     name             = "Max";
                NameAgeComponent nameAgeComponent = null;
                TestEntity       entity1          = null;
                TestEntity       entity2          = null;

                before = () => {
                    nameAgeComponent      = new NameAgeComponent();
                    nameAgeComponent.name = name;
                    entity1 = ctx.CreateEntity();
                    entity1.AddComponent(CID.ComponentA, nameAgeComponent);
                    entity2 = ctx.CreateEntity();
                    entity2.AddComponent(CID.ComponentA, nameAgeComponent);
                };

                it["gets entities for key"] = () => {
                    var entities = index.GetEntities(name);
                    entities.Count.should_be(2);
                    entities.should_contain(entity1);
                    entities.should_contain(entity2);
                };

                it["retains entity"] = () => {
                    entity1.retainCount.should_be(3); // Context, Group, EntityIndex
                    entity2.retainCount.should_be(3); // Context, Group, EntityIndex
                };

                it["has existing entities"] = () => {
                    var newIndex = new EntityIndex <TestEntity, string>("TestIndex", group, (e, c) => {
                        var nameAge = c as NameAgeComponent;
                        return(nameAge != null
                            ? nameAge.name
                            : ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name);
                    });
                    newIndex.GetEntities(name).Count.should_be(2);
                };

                it["releases and removes entity from index when component gets removed"] = () => {
                    entity1.RemoveComponent(CID.ComponentA);
                    index.GetEntities(name).Count.should_be(1);
                    entity1.retainCount.should_be(1); // Context
                };

                it["can ToString"] = () => {
                    index.ToString().should_be("EntityIndex(TestIndex)");
                };

                context["when deactivated"] = () => {
                    before = () => {
                        index.Deactivate();
                    };

                    it["clears index and releases entity"] = () => {
                        index.GetEntities(name).should_be_empty();
                        entity1.retainCount.should_be(2); // Context, Group
                        entity2.retainCount.should_be(2); // Context, Group
                    };

                    it["doesn't add entities anymore"] = () => {
                        ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
                        index.GetEntities(name).should_be_empty();
                    };

                    context["when activated"] = () => {
                        before = () => {
                            index.Activate();
                        };

                        it["has existing entities"] = () => {
                            var entities = index.GetEntities(name);
                            entities.Count.should_be(2);
                            entities.should_contain(entity1);
                            entities.should_contain(entity2);
                        };

                        it["adds new entities"] = () => {
                            var entity3 = ctx.CreateEntity();
                            entity3.AddComponent(CID.ComponentA, nameAgeComponent);

                            var entities = index.GetEntities(name);
                            entities.Count.should_be(3);
                            entities.should_contain(entity1);
                            entities.should_contain(entity2);
                            entities.should_contain(entity3);
                        };
                    };
                };
            };
        };

        context["multiple keys"] = () => {
            EntityIndex <TestEntity, string> index = null;
            IContext <TestEntity>            ctx   = null;
            IGroup <TestEntity> group   = null;
            TestEntity          entity1 = null;
            TestEntity          entity2 = null;

            before = () => {
                ctx   = new MyTestContext();
                group = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA));
                index = new EntityIndex <TestEntity, string>("TestIndex", group, (e, c) => {
                    return(e == entity1
                        ? new [] { "1", "2" }
                        : new [] { "2", "3" });
                });
            };

            context["when entity for key exists"] = () => {
                before = () => {
                    entity1 = ctx.CreateEntity();
                    entity1.AddComponentA();
                    entity2 = ctx.CreateEntity();
                    entity2.AddComponentA();
                };

                it["retains entity"] = () => {
                    entity1.retainCount.should_be(3);
                    entity2.retainCount.should_be(3);

                    var safeAerc1 = entity1.aerc as SafeAERC;
                    if (safeAerc1 != null)
                    {
                        safeAerc1.owners.should_contain(index);
                    }

                    var safeAerc2 = entity1.aerc as SafeAERC;
                    if (safeAerc2 != null)
                    {
                        safeAerc2.owners.should_contain(index);
                    }
                };

                it["has entity"] = () => {
                    index.GetEntities("1").Count.should_be(1);
                    index.GetEntities("2").Count.should_be(2);
                    index.GetEntities("3").Count.should_be(1);
                };

                it["gets entity for key"] = () => {
                    index.GetEntities("1").First().should_be_same(entity1);
                    index.GetEntities("2").should_contain(entity1);
                    index.GetEntities("2").should_contain(entity2);
                    index.GetEntities("3").First().should_be_same(entity2);
                };

                it["releases and removes entity from index when component gets removed"] = () => {
                    entity1.RemoveComponent(CID.ComponentA);
                    index.GetEntities("1").Count.should_be(0);
                    index.GetEntities("2").Count.should_be(1);
                    index.GetEntities("3").Count.should_be(1);

                    entity1.retainCount.should_be(1);
                    entity2.retainCount.should_be(3);

                    var safeAerc1 = entity1.aerc as SafeAERC;
                    if (safeAerc1 != null)
                    {
                        safeAerc1.owners.should_not_contain(index);
                    }

                    var safeAerc2 = entity2.aerc as SafeAERC;
                    if (safeAerc2 != null)
                    {
                        safeAerc2.owners.should_contain(index);
                    }
                };

                it["has existing entities"] = () => {
                    index.Deactivate();
                    index.Activate();
                    index.GetEntities("1").First().should_be_same(entity1);
                    index.GetEntities("2").should_contain(entity1);
                    index.GetEntities("2").should_contain(entity2);
                    index.GetEntities("3").First().should_be_same(entity2);
                };
            };
        };
    }
    void when_creating_matcher()
    {
        TestEntity eA   = null;
        TestEntity eB   = null;
        TestEntity eC   = null;
        TestEntity eAB  = null;
        TestEntity eABC = null;

        before = () => {
            eA = this.CreateEntity();
            eA.AddComponentA();

            eB = this.CreateEntity();
            eB.AddComponentB();

            eC = this.CreateEntity();
            eC.AddComponentC();

            eAB = this.CreateEntity();
            eAB.AddComponentA();
            eAB.AddComponentB();

            eABC = this.CreateEntity();
            eABC.AddComponentA();
            eABC.AddComponentB();
            eABC.AddComponentC();
        };

        context["allOf"] = () => {
            IAllOfMatcher <TestEntity> m = null;

            before = () => m = Matcher <TestEntity> .AllOf(CID.ComponentA, CID.ComponentB);

            it["has all indices"] = () => {
                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.allOfIndices, CID.ComponentA, CID.ComponentB);
            };

            it["has all indices without duplicates"] = () => {
                m = Matcher <TestEntity> .AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentB
                });

                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.allOfIndices, CID.ComponentA, CID.ComponentB);
            };

            it["caches indices"] = () => m.indices.should_be_same(m.indices);
            it["doesn't match"]  = () => m.Matches(eA).should_be_false();
            it["matches"]        = () => {
                m.Matches(eAB).should_be_true();
                m.Matches(eABC).should_be_true();
            };

            it["merges matchers to new matcher"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentB });

                var m3 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentC });

                var mergedMatcher = Matcher <TestEntity> .AllOf(m1, m2, m3);

                assertIndicesContain(mergedMatcher.indices, CID.ComponentA, CID.ComponentB, CID.ComponentC);
                assertIndicesContain(mergedMatcher.allOfIndices, CID.ComponentA, CID.ComponentB, CID.ComponentC);
            };

            it["merges matchers to new matcher without duplicates"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m3 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentB });

                var mergedMatcher = Matcher <TestEntity> .AllOf(m1, m2, m3);

                assertIndicesContain(mergedMatcher.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(mergedMatcher.allOfIndices, CID.ComponentA, CID.ComponentB);
            };

            it["throws when merging matcher with more than one index"] = expect <MatcherException>(() => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA, CID.ComponentB });
                Matcher <TestEntity> .AllOf(m1);
            });

            it["can ToString"] = () => m.ToString().should_be("AllOf(1, 2)");

            it["uses componentNames when set"] = () => {
                var matcher = (Matcher <TestEntity>)m;
                matcher.componentNames = new [] { "one", "two", "three" };
                matcher.ToString().should_be("AllOf(two, three)");
            };

            it["uses componentNames when merged matcher ToString"] = () => {
                var m1 = (Matcher <TestEntity>) Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = (Matcher <TestEntity>) Matcher <TestEntity> .AllOf(new [] { CID.ComponentB });

                var m3 = (Matcher <TestEntity>) Matcher <TestEntity> .AllOf(new [] { CID.ComponentC });

                m2.componentNames = new [] { "m_0", "m_1", "m_2", "m_3" };

                var mergedMatcher = Matcher <TestEntity> .AllOf(m1, m2, m3);

                mergedMatcher.ToString().should_be("AllOf(m_1, m_2, m_3)");
            };
        };

        context["anyOf"] = () => {
            IAnyOfMatcher <TestEntity> m = null;

            before = () => m = Matcher <TestEntity> .AnyOf(new [] {
                CID.ComponentA,
                CID.ComponentB
            });

            it["has all indices"] = () => {
                m = Matcher <TestEntity> .AnyOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB
                });

                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.anyOfIndices, CID.ComponentA, CID.ComponentB);
            };

            it["has all indices without duplicates"] = () => {
                m = Matcher <TestEntity> .AnyOf(new [] {
                    CID.ComponentA,
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentB
                });

                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.anyOfIndices, CID.ComponentA, CID.ComponentB);
            };

            it["caches indices"] = () => m.indices.should_be_same(m.indices);
            it["doesn't match"]  = () => m.Matches(eC).should_be_false();
            it["matches"]        = () => {
                m.Matches(eA).should_be_true();
                m.Matches(eB).should_be_true();
                m.Matches(eABC).should_be_true();
            };

            it["merges matchers to new matcher"] = () => {
                var m1 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentB });

                var m3 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentC });

                var mergedMatcher = Matcher <TestEntity> .AnyOf(m1, m2, m3);

                assertIndicesContain(mergedMatcher.indices, CID.ComponentA, CID.ComponentB, CID.ComponentC);
                assertIndicesContain(mergedMatcher.anyOfIndices, CID.ComponentA, CID.ComponentB, CID.ComponentC);
            };

            it["merges matchers to new matcher without duplicates"] = () => {
                var m1 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentB });

                var m3 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentB });

                var mergedMatcher = Matcher <TestEntity> .AnyOf(m1, m2, m3);

                assertIndicesContain(mergedMatcher.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(mergedMatcher.anyOfIndices, CID.ComponentA, CID.ComponentB);
            };

            it["throws when merging matcher with more than one index"] = expect <MatcherException>(() => {
                var m1 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentA, CID.ComponentB });
                Matcher <TestEntity> .AnyOf(m1);
            });

            it["can ToString"] = () => m.ToString().should_be("AnyOf(1, 2)");
        };

        context["allOf.noneOf"] = () => {
            ICompoundMatcher <TestEntity> m = null;

            before = () => m = Matcher <TestEntity> .AllOf(new [] {
                CID.ComponentA,
                CID.ComponentB
            }).NoneOf(CID.ComponentC, CID.ComponentD);

            it["has all indices"] = () => {
                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB, CID.ComponentC, CID.ComponentD);
                assertIndicesContain(m.allOfIndices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.noneOfIndices, CID.ComponentC, CID.ComponentD);
            };

            it["has all indices without duplicates"] = () => {
                m = Matcher <TestEntity> .AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentA,
                    CID.ComponentB
                }).NoneOf(CID.ComponentB, CID.ComponentC, CID.ComponentC);

                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB, CID.ComponentC);
                assertIndicesContain(m.allOfIndices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.noneOfIndices, CID.ComponentB, CID.ComponentC);
            };

            it["caches indices"] = () => m.indices.should_be_same(m.indices);
            it["doesn't match"]  = () => m.Matches(eABC).should_be_false();
            it["matches"]        = () => m.Matches(eAB).should_be_true();

            it["mutates existing matcher"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = m1.NoneOf(new [] { CID.ComponentB });
                m1.should_be_same(m2);
                assertIndicesContain(m1.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m1.allOfIndices, CID.ComponentA);
                assertIndicesContain(m1.noneOfIndices, CID.ComponentB);
            };

            it["mutates existing merged matcher"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentB });

                var m3 = Matcher <TestEntity> .AllOf(m1);

                var m4 = m3.NoneOf(m2);
                m3.should_be_same(m4);
                assertIndicesContain(m3.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m3.allOfIndices, CID.ComponentA);
                assertIndicesContain(m3.noneOfIndices, CID.ComponentB);
            };

            it["can ToString"] = () => m.ToString().should_be("AllOf(1, 2).NoneOf(3, 4)");

            it["uses componentNames when componentNames set"] = () => {
                var matcher = (Matcher <TestEntity>)m;
                matcher.componentNames = new [] { "one", "two", "three", "four", "five" };
                matcher.ToString().should_be("AllOf(two, three).NoneOf(four, five)");
            };
        };

        context["anyOf.noneOf"] = () => {
            ICompoundMatcher <TestEntity> m = null;

            before = () => m = Matcher <TestEntity> .AnyOf(new [] {
                CID.ComponentA,
                CID.ComponentB
            }).NoneOf(CID.ComponentC, CID.ComponentD);

            it["has all indices"] = () => {
                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB, CID.ComponentC, CID.ComponentD);
                assertIndicesContain(m.anyOfIndices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.noneOfIndices, CID.ComponentC, CID.ComponentD);
            };

            it["has all indices without duplicates"] = () => {
                m = Matcher <TestEntity> .AnyOf(new [] {
                    CID.ComponentA,
                    CID.ComponentA,
                    CID.ComponentB
                }).NoneOf(CID.ComponentB, CID.ComponentC, CID.ComponentC);

                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB, CID.ComponentC);
                assertIndicesContain(m.anyOfIndices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.noneOfIndices, CID.ComponentB, CID.ComponentC);
            };

            it["caches indices"] = () => m.indices.should_be_same(m.indices);
            it["doesn't match"]  = () => m.Matches(eABC).should_be_false();
            it["matches"]        = () => m.Matches(eA).should_be_true();
            it["matches"]        = () => m.Matches(eB).should_be_true();

            it["mutates existing matcher"] = () => {
                var m1 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentA });

                var m2 = m1.NoneOf(new [] { CID.ComponentB });
                m1.should_be_same(m2);
                assertIndicesContain(m1.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m1.anyOfIndices, CID.ComponentA);
                assertIndicesContain(m1.noneOfIndices, CID.ComponentB);
            };

            it["mutates existing merged matcher"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentB });

                var m3 = Matcher <TestEntity> .AnyOf(m1);

                var m4 = m3.NoneOf(m2);
                m3.should_be_same(m4);
                assertIndicesContain(m3.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m3.anyOfIndices, CID.ComponentA);
                assertIndicesContain(m3.noneOfIndices, CID.ComponentB);
            };

            it["can ToString"] = () => m.ToString().should_be("AnyOf(1, 2).NoneOf(3, 4)");
        };

        context["allOf.anyOf"] = () => {
            ICompoundMatcher <TestEntity> m = null;

            before = () => m = Matcher <TestEntity> .AllOf(new [] {
                CID.ComponentA,
                CID.ComponentB
            }).AnyOf(CID.ComponentC, CID.ComponentD);

            it["has all indices"] = () => {
                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB, CID.ComponentC, CID.ComponentD);
                assertIndicesContain(m.allOfIndices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.anyOfIndices, CID.ComponentC, CID.ComponentD);
            };

            it["has all indices without duplicates"] = () => {
                m = Matcher <TestEntity> .AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentA,
                    CID.ComponentB
                }).AnyOf(CID.ComponentB, CID.ComponentC, CID.ComponentC);

                assertIndicesContain(m.indices, CID.ComponentA, CID.ComponentB, CID.ComponentC);
                assertIndicesContain(m.allOfIndices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m.anyOfIndices, CID.ComponentB, CID.ComponentC);
            };

            it["caches indices"] = () => m.indices.should_be_same(m.indices);
            it["doesn't match"]  = () => m.Matches(eAB).should_be_false();
            it["matches"]        = () => m.Matches(eABC).should_be_true();

            it["mutates existing matcher"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = m1.AnyOf(new [] { CID.ComponentB });
                m1.should_be_same(m2);
                assertIndicesContain(m1.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m1.allOfIndices, CID.ComponentA);
                assertIndicesContain(m1.anyOfIndices, CID.ComponentB);
            };

            it["mutates existing merged matcher"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentB });

                var m3 = Matcher <TestEntity> .AllOf(m1);

                var m4 = m3.AnyOf(m2);
                m3.should_be_same(m4);
                assertIndicesContain(m3.indices, CID.ComponentA, CID.ComponentB);
                assertIndicesContain(m3.allOfIndices, CID.ComponentA);
                assertIndicesContain(m3.anyOfIndices, CID.ComponentB);
            };

            it["can ToString"] = () => m.ToString().should_be("AllOf(1, 2).AnyOf(3, 4)");
        };

        context["indices cache"] = () => {
            it["updates cache when calling AnyOf"] = () => {
                var m = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var cache = m.indices;
                m.AnyOf(new [] { CID.ComponentB });
                m.indices.should_not_be_same(cache);
            };

            it["updates cache when calling NoneOf"] = () => {
                var m = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var cache = m.indices;
                m.NoneOf(new [] { CID.ComponentB });
                m.indices.should_not_be_same(cache);
            };
        };

        context["equals"] = () => {
            it["updates hash when changed with anyOf"] = () => {
                var m1   = allOfAB();
                var hash = m1.GetHashCode();

                m1.AnyOf(42).GetHashCode().should_not_be(hash);
            };

            it["updates hash when changed with noneOf"] = () => {
                var m1   = allOfAB();
                var hash = m1.GetHashCode();

                m1.NoneOf(42).GetHashCode().should_not_be(hash);
            };

            it["equals equal AllOfMatcher"] = () => {
                var m1 = allOfAB();
                var m2 = allOfAB();
                m1.should_not_be_same(m2);
                m1.Equals(m2).should_be_true();
                m1.GetHashCode().should_be(m2.GetHashCode());
            };

            it["equals equal AllOfMatcher independent of the order of indices"] = () => {
                var m1 = allOfAB();
                var m2 = allOfBA();

                m1.Equals(m2).should_be_true();
                m1.GetHashCode().should_be(m2.GetHashCode());
            };

            it["equals merged matcher"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentB });

                var m3 = allOfBA();

                var mergedMatcher = Matcher <TestEntity> .AllOf(m1, m2);

                mergedMatcher.Equals(m3).should_be_true();
                mergedMatcher.GetHashCode().should_be(m3.GetHashCode());
            };

            it["doesn't equal different AllOfMatcher"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] {
                    CID.ComponentA
                });

                var m2 = allOfAB();

                m1.Equals(m2).should_be_false();
                m1.GetHashCode().should_not_be(m2.GetHashCode());
            };

            it["allOf doesn't equal anyOf with same indices"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentA });

                m1.Equals(m2).should_be_false();
                m1.GetHashCode().should_not_be(m2.GetHashCode());
            };

            it["doesn't equal differnt type matchers with same indices"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentB });

                var m3 = Matcher <TestEntity> .AllOf(m1, m2);

                var m4 = Matcher <TestEntity> .AnyOf(m1, m2);

                m3.Equals(m4).should_be_false();
                m3.GetHashCode().should_not_be(m4.GetHashCode());
            };

            it["equals compound matcher"] = () => {
                var m1 = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA });

                var m2 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentB });

                var m3 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentC });

                var m4 = Matcher <TestEntity> .AnyOf(new [] { CID.ComponentD });

                var mX = Matcher <TestEntity> .AllOf(m1, m2).AnyOf(m3, m4);

                var mY = Matcher <TestEntity> .AllOf(m1, m2).AnyOf(m3, m4);

                mX.Equals(mY).should_be_true();
                mX.GetHashCode().should_be(mY.GetHashCode());
            };
        };
    }
    void when_extending()
    {
        context["when copying components"] = () => {
            IContext <TestEntity> ctx     = null;
            TestEntity            entity  = null;
            TestEntity            target  = null;
            NameAgeComponent      nameAge = null;

            before = () => {
                ctx     = new MyTestContext();
                entity  = ctx.CreateEntity();
                target  = ctx.CreateEntity();
                nameAge = new NameAgeComponent {
                    name = "Max", age = 42
                };
            };

            it["doesn't change entity if original doesn't have any components"] = () => {
                entity.CopyTo(target);

                entity.creationIndex.should_be(0);
                target.creationIndex.should_be(1);

                target.GetComponents().Length.should_be(0);
            };

            it["adds copies of all components to target entity"] = () => {
                entity.AddComponentA();
                entity.AddComponent(CID.ComponentB, nameAge);

                entity.CopyTo(target);

                target.GetComponents().Length.should_be(2);
                target.HasComponentA().should_be_true();
                target.HasComponentB().should_be_true();
                target.GetComponentA().should_not_be_same(Component.A);
                target.GetComponent(CID.ComponentB).should_not_be_same(nameAge);

                var clonedComponent = (NameAgeComponent)target.GetComponent(CID.ComponentB);

                clonedComponent.name.should_be(nameAge.name);
                clonedComponent.age.should_be(nameAge.age);
            };

            it["throws when target already has a component at index"] = base.expect <EntityAlreadyHasComponentException>(() => {
                entity.AddComponentA();
                entity.AddComponent(CID.ComponentB, nameAge);
                var component = new NameAgeComponent();
                target.AddComponent(CID.ComponentB, component);

                entity.CopyTo(target);
            });

            it["replaces existing components when overwrite is set"] = () => {
                entity.AddComponentA();
                entity.AddComponent(CID.ComponentB, nameAge);
                var component = new NameAgeComponent();
                target.AddComponent(CID.ComponentB, component);

                entity.CopyTo(target, true);

                var copy = target.GetComponent(CID.ComponentB);
                copy.should_not_be_same(nameAge);
                copy.should_not_be_same(component);
                ((NameAgeComponent)copy).name.should_be(nameAge.name);
                ((NameAgeComponent)copy).age.should_be(nameAge.age);
            };

            it["only adds copies of specified components to target entity"] = () => {
                entity.AddComponentA();
                entity.AddComponentB();
                entity.AddComponentC();

                entity.CopyTo(target, false, CID.ComponentB, CID.ComponentC);

                target.GetComponents().Length.should_be(2);
                target.HasComponentB().should_be_true();
                target.HasComponentC().should_be_true();
            };

            it["uses component pool"] = () => {
                entity.AddComponentA();

                var component = new ComponentA();
                target.GetComponentPool(CID.ComponentA).Push(component);

                entity.CopyTo(target);

                target.GetComponentA().should_be_same(component);
            };
        };
    }
Example #5
0
    void when_created()
    {
        TestEntity eA1 = null;
        TestEntity eA2 = null;

        before = () => {
            _groupA = new Group <TestEntity>(Matcher <TestEntity> .AllOf(CID.ComponentA));
            eA1     = this.CreateEntity().AddComponentA();
            eA2     = this.CreateEntity().AddComponentA();
        };

        context["initial state"] = () => {
            it["doesn't have entities which haven't been added"] = () => {
                _groupA.GetEntities().should_be_empty();
            };

            it["is empty"] = () => {
                _groupA.count.should_be(0);
            };

            it["doesn't contain entity"] = () => {
                _groupA.ContainsEntity(eA1).should_be_false();
            };
        };

        context["when entity is matching"] = () => {
            before = () => {
                handleSilently(eA1);
            };

            it["adds matching entity"] = () => {
                assertContains(eA1);
            };

            it["doesn't add same entity twice"] = () => {
                handleSilently(eA1);
                assertContains(eA1);
            };

            context["when entity doesn't match anymore"] = () => {
                it["removes entity"] = () => {
                    eA1.RemoveComponentA();
                    handleSilently(eA1);

                    assertContainsNot(eA1);
                };
            };
        };

        context["when entity is not enabled"] = () => {
            it["doesn't add entity"] = () => {
                eA1.InternalDestroy();
                handleSilently(eA1);
                assertContainsNot(eA1);
            };
        };

        it["doesn't add entity when not matching"] = () => {
            var e = this.CreateEntity().AddComponentB();
            handleSilently(e);
            assertContainsNot(e);
        };

        it["gets null when single entity does not exist"] = () => {
            _groupA.GetSingleEntity().should_be_null();
        };

        it["gets single entity"] = () => {
            handleSilently(eA1);
            _groupA.GetSingleEntity().should_be_same(eA1);
        };

        it["throws when attempting to get single entity and multiple matching entities exist"] = expect <GroupSingleEntityException <TestEntity> >(() => {
            handleSilently(eA1);
            handleSilently(eA2);
            _groupA.GetSingleEntity();
        });

        context["events"] = () => {
            var didDispatch = 0;

            before = () => {
                didDispatch = 0;
            };

            it["dispatches OnEntityAdded when matching entity added"] = () => {
                _groupA.OnEntityAdded += (group, entity, index, component) => {
                    didDispatch++;
                    group.should_be_same(_groupA);
                    entity.should_be_same(eA1);
                    index.should_be(CID.ComponentA);
                    component.should_be_same(Component.A);
                };
                _groupA.OnEntityRemoved += delegate { this.Fail(); };
                _groupA.OnEntityUpdated += delegate { this.Fail(); };

                handleAddEA(eA1);
                didDispatch.should_be(1);
            };

            it["doesn't dispatches OnEntityAdded when matching entity already has been added"] = () => {
                handleAddEA(eA1);
                _groupA.OnEntityAdded   += delegate { this.Fail(); };
                _groupA.OnEntityRemoved += delegate { this.Fail(); };
                _groupA.OnEntityUpdated += delegate { this.Fail(); };
                handleAddEA(eA1);
                didDispatch.should_be(0);
            };

            it["doesn't dispatches OnEntityAdded when entity is not matching"] = () => {
                var e = this.CreateEntity().AddComponentB();
                _groupA.OnEntityAdded   += delegate { this.Fail(); };
                _groupA.OnEntityRemoved += delegate { this.Fail(); };
                _groupA.OnEntityUpdated += delegate { this.Fail(); };
                handleAddEB(e);
            };

            it["dispatches OnEntityRemoved when entity got removed"] = () => {
                handleSilently(eA1);
                _groupA.OnEntityRemoved += (group, entity, index, component) => {
                    didDispatch++;
                    group.should_be_same(_groupA);
                    entity.should_be_same(eA1);
                    index.should_be(CID.ComponentA);
                    component.should_be_same(Component.A);
                };
                _groupA.OnEntityAdded   += delegate { this.Fail(); };
                _groupA.OnEntityUpdated += delegate { this.Fail(); };

                eA1.RemoveComponentA();
                handleRemoveEA(eA1, Component.A);

                didDispatch.should_be(1);
            };

            it["doesn't dispatch OnEntityRemoved when entity didn't get removed"] = () => {
                _groupA.OnEntityRemoved += delegate { this.Fail(); };
                eA1.RemoveComponentA();
                handleRemoveEA(eA1, Component.A);
            };

            it["dispatches OnEntityRemoved, OnEntityAdded and OnEntityUpdated when updating"] = () => {
                handleSilently(eA1);

                var removed       = 0;
                var added         = 0;
                var updated       = 0;
                var newComponentA = new ComponentA();

                _groupA.OnEntityRemoved += (group, entity, index, component) => {
                    removed += 1;
                    group.should_be(_groupA);
                    entity.should_be(eA1);
                    index.should_be(CID.ComponentA);
                    component.should_be_same(Component.A);
                };
                _groupA.OnEntityAdded += (group, entity, index, component) => {
                    added += 1;
                    group.should_be(_groupA);
                    entity.should_be(eA1);
                    index.should_be(CID.ComponentA);
                    component.should_be_same(newComponentA);
                };
                _groupA.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => {
                    updated += 1;
                    group.should_be(_groupA);
                    entity.should_be(eA1);
                    index.should_be(CID.ComponentA);
                    previousComponent.should_be_same(Component.A);
                    newComponent.should_be_same(newComponentA);
                };

                updateEA(eA1, newComponentA);

                removed.should_be(1);
                added.should_be(1);
                updated.should_be(1);
            };

            it["doesn't dispatch OnEntityRemoved and OnEntityAdded when updating when group doesn't contain entity"] = () => {
                _groupA.OnEntityRemoved += delegate { this.Fail(); };
                _groupA.OnEntityAdded   += delegate { this.Fail(); };
                _groupA.OnEntityUpdated += delegate { this.Fail(); };
                updateEA(eA1, new ComponentA());
            };

            it["removes all event handlers"] = () => {
                _groupA.OnEntityAdded   += delegate { this.Fail(); };
                _groupA.OnEntityRemoved += delegate { this.Fail(); };
                _groupA.OnEntityUpdated += delegate { this.Fail(); };

                _groupA.RemoveAllEventHandlers();

                handleAddEA(eA1);

                var cA = eA1.GetComponentA();
                eA1.RemoveComponentA();
                handleRemoveEA(eA1, cA);

                eA1.AddComponentA();
                handleAddEA(eA1);
                updateEA(eA1, Component.A);
            };
        };

        context["internal caching"] = () => {
            context["GetEntities()"] = () => {
                IEntity[] cache = null;

                before = () => {
                    handleSilently(eA1);
                    cache = _groupA.GetEntities();
                };

                it["gets cached entities"] = () => {
                    _groupA.GetEntities().should_be_same(cache);
                };

                it["updates cache when adding a new matching entity"] = () => {
                    handleSilently(eA2);
                    _groupA.GetEntities().should_not_be_same(cache);
                };

                it["doesn't update cache when attempting to add a not matching entity"] = () => {
                    var e = this.CreateEntity();
                    handleSilently(e);
                    _groupA.GetEntities().should_be_same(cache);
                };

                it["updates cache when removing an entity"] = () => {
                    eA1.RemoveComponentA();
                    handleSilently(eA1);
                    _groupA.GetEntities().should_not_be_same(cache);
                };

                it["doesn't update cache when attempting to remove an entity that wasn't added before"] = () => {
                    eA2.RemoveComponentA();
                    handleSilently(eA2);
                    _groupA.GetEntities().should_be_same(cache);
                };

                it["doesn't update cache when updating an entity"] = () => {
                    updateEA(eA1, new ComponentA());
                    _groupA.GetEntities().should_be_same(cache);
                };
            };

            context["SingleEntity()"] = () => {
                IEntity cache = null;

                before = () => {
                    handleSilently(eA1);
                    cache = _groupA.GetSingleEntity();
                };

                it["gets cached singleEntities"] = () => {
                    _groupA.GetSingleEntity().should_be_same(cache);
                };

                it["updates cache when new single entity was added"] = () => {
                    eA1.RemoveComponentA();
                    handleSilently(eA1);
                    handleSilently(eA2);
                    _groupA.GetSingleEntity().should_not_be_same(cache);
                };

                it["updates cache when single entity is removed"] = () => {
                    eA1.RemoveComponentA();
                    handleSilently(eA1);
                    _groupA.GetSingleEntity().should_not_be_same(cache);
                };

                it["doesn't update cache when single entity is updated"] = () => {
                    updateEA(eA1, new ComponentA());
                    _groupA.GetSingleEntity().should_be_same(cache);
                };
            };
        };

        context["reference counting"] = () => {
            it["retains matched entity"] = () => {
                eA1.retainCount.should_be(0);
                handleSilently(eA1);
                eA1.retainCount.should_be(1);
            };

            it["releases removed entity"] = () => {
                handleSilently(eA1);
                eA1.RemoveComponentA();
                handleSilently(eA1);
                eA1.retainCount.should_be(0);
            };

            it["invalidates entitiesCache (silent mode)"] = () => {
                var didExecute = 0;
                eA1.OnEntityReleased += entity => {
                    didExecute += 1;
                    _groupA.GetEntities().Length.should_be(0);
                };
                handleSilently(eA1);
                _groupA.GetEntities();
                eA1.RemoveComponentA();
                handleSilently(eA1);
                didExecute.should_be(1);
            };

            it["invalidates entitiesCache"] = () => {
                var didExecute = 0;
                eA1.OnEntityReleased += entity => {
                    didExecute += 1;
                    _groupA.GetEntities().Length.should_be(0);
                };
                handleAddEA(eA1);
                _groupA.GetEntities();
                eA1.RemoveComponentA();
                handleRemoveEA(eA1, Component.A);
                didExecute.should_be(1);
            };

            it["invalidates singleEntityCache (silent mode)"] = () => {
                var didExecute = 0;
                eA1.OnEntityReleased += entity => {
                    didExecute += 1;
                    _groupA.GetSingleEntity().should_be_null();
                };
                handleSilently(eA1);
                _groupA.GetSingleEntity();
                eA1.RemoveComponentA();
                handleSilently(eA1);
                didExecute.should_be(1);
            };

            it["invalidates singleEntityCache"] = () => {
                var didExecute = 0;
                eA1.OnEntityReleased += entity => {
                    didExecute += 1;
                    _groupA.GetSingleEntity().should_be_null();
                };
                handleAddEA(eA1);
                _groupA.GetSingleEntity();
                eA1.RemoveComponentA();
                handleRemoveEA(eA1, Component.A);
                didExecute.should_be(1);
            };

            it["retains entity until after event handlers were called"] = () => {
                handleAddEA(eA1);
                var didDispatch = 0;
                _groupA.OnEntityRemoved += (group, entity, index, component) => {
                    didDispatch += 1;
                    entity.retainCount.should_be(1);
                };
                eA1.RemoveComponentA();
                handleRemoveEA(eA1, Component.A);

                didDispatch.should_be(1);
                eA1.retainCount.should_be(0);
            };
        };

        it["can ToString"] = () => {
            var m = Matcher <TestEntity> .AllOf(Matcher <TestEntity> .AllOf(0), Matcher <TestEntity> .AllOf(1));

            var group = new Group <TestEntity>(m);
            group.ToString().should_be("Group(AllOf(0, 1))");
        };
    }
Example #6
0
    void when_creating()
    {
        IContext <TestEntity> ctx    = null;
        TestEntity            entity = null;

        before = () => {
            ctx    = new MyTestContext();
            entity = ctx.CreateEntity();
        };

        context["ComponentBlueprint"] = () => {
            it["creates a component blueprint from a component without members"] = () => {
                var component = new ComponentA();

                const int index = 42;

                var componentBlueprint = new ComponentBlueprint(index, component);
                componentBlueprint.index.should_be(index);
                componentBlueprint.fullTypeName.should_be(component.GetType().FullName);
                componentBlueprint.members.Length.should_be(0);
            };

            it["throws when unknown type"] = expect <ComponentBlueprintException>(() => {
                var componentBlueprint          = new ComponentBlueprint();
                componentBlueprint.fullTypeName = "UnknownType";
                componentBlueprint.CreateComponent(null);
            });

            it["throws when type doesn't implement IComponent"] = expect <ComponentBlueprintException>(() => {
                var componentBlueprint          = new ComponentBlueprint();
                componentBlueprint.fullTypeName = "string";
                componentBlueprint.CreateComponent(null);
            });

            it["creates a component blueprint from a component with members"] = () => {
                var component = new NameAgeComponent();
                component.name = "Max";
                component.age  = 42;

                const int index = 24;

                var componentBlueprint = new ComponentBlueprint(index, component);
                componentBlueprint.index.should_be(index);
                componentBlueprint.fullTypeName.should_be(component.GetType().FullName);
                componentBlueprint.members.Length.should_be(2);

                componentBlueprint.members[0].name.should_be("name");
                componentBlueprint.members[0].value.should_be(component.name);

                componentBlueprint.members[1].name.should_be("age");
                componentBlueprint.members[1].value.should_be(component.age);
            };

            it["creates a component and sets members values"] = () => {
                var componentBlueprint = new ComponentBlueprint();
                componentBlueprint.fullTypeName = typeof(ComponentWithFieldsAndProperties).FullName;
                componentBlueprint.index        = CID.ComponentB;
                componentBlueprint.members      = new [] {
                    new SerializableMember("publicField", "publicFieldValue"),
                    new SerializableMember("publicProperty", "publicPropertyValue")
                };

                var component = (ComponentWithFieldsAndProperties)componentBlueprint.CreateComponent(entity);
                component.publicField.should_be("publicFieldValue");
                component.publicProperty.should_be("publicPropertyValue");
            };

            it["ignores invalid member name"] = () => {
                var componentBlueprint = new ComponentBlueprint();
                componentBlueprint.index        = 0;
                componentBlueprint.fullTypeName = typeof(NameAgeComponent).FullName;
                componentBlueprint.members      = new [] {
                    new SerializableMember("xxx", "publicFieldValue"),
                    new SerializableMember("publicProperty", "publicPropertyValue")
                };
                componentBlueprint.CreateComponent(entity);
            };
        };

        context["Blueprint"] = () => {
            it["creates an empty blueprint from a null entity"] = () => {
                var blueprint = new Blueprint("My Context", "Hero", null);
                blueprint.contextIdentifier.should_be("My Context");
                blueprint.name.should_be("Hero");
                blueprint.components.Length.should_be(0);
            };

            it["creates a blueprint from an entity"] = () => {
                entity.AddComponentA();

                var component = new NameAgeComponent();
                component.name = "Max";
                component.age  = 42;

                entity.AddComponent(CID.ComponentB, component);

                var blueprint = new Blueprint("My Context", "Hero", entity);
                blueprint.contextIdentifier.should_be("My Context");
                blueprint.name.should_be("Hero");
                blueprint.components.Length.should_be(2);

                blueprint.components[0].index.should_be(CID.ComponentA);
                blueprint.components[0].fullTypeName.should_be(Component.A.GetType().FullName);

                blueprint.components[1].index.should_be(CID.ComponentB);
                blueprint.components[1].fullTypeName.should_be(component.GetType().FullName);
            };

            context["when applying blueprint"] = () => {
                Blueprint blueprint = null;

                before = () => {
                    var component1 = new ComponentBlueprint();
                    component1.index        = CID.ComponentA;
                    component1.fullTypeName = typeof(ComponentA).FullName;
                    component1.members      = new SerializableMember[0];

                    var component2 = new ComponentBlueprint();
                    component2.index        = CID.ComponentB;
                    component2.fullTypeName = typeof(NameAgeComponent).FullName;
                    component2.members      = new [] {
                        new SerializableMember("name", "Max"),
                        new SerializableMember("age", 42)
                    };

                    blueprint            = new Blueprint();
                    blueprint.name       = "Hero";
                    blueprint.components = new [] { component1, component2 };
                };

                it["applies blueprint to entity"] = () => {
                    entity.ApplyBlueprint(blueprint);
                    entity.GetComponents().Length.should_be(2);

                    entity.GetComponent(CID.ComponentA).GetType().should_be(typeof(ComponentA));

                    var nameAgeComponent = (NameAgeComponent)entity.GetComponent(CID.ComponentB);
                    nameAgeComponent.GetType().should_be(typeof(NameAgeComponent));
                    nameAgeComponent.name.should_be("Max");
                    nameAgeComponent.age.should_be(42);
                };

                it["throws when entity already has a component which should be added from blueprint"] = expect <EntityAlreadyHasComponentException>(() => {
                    entity.AddComponentA();
                    entity.ApplyBlueprint(blueprint);
                });

                it["can overwrite existing components"] = () => {
                    var nameAgeComponent = new NameAgeComponent();
                    nameAgeComponent.name = "Jack";
                    nameAgeComponent.age  = 24;
                    entity.AddComponent(CID.ComponentB, nameAgeComponent);

                    entity.ApplyBlueprint(blueprint, true);
                };

                it["uses component from componentPool"] = () => {
                    var component = new ComponentBlueprint();
                    component.index        = CID.ComponentA;
                    component.fullTypeName = typeof(ComponentA).FullName;
                    component.members      = new SerializableMember[0];

                    blueprint            = new Blueprint();
                    blueprint.name       = "Hero";
                    blueprint.components = new [] { component };

                    var componentA = entity.CreateComponent <ComponentA>(CID.ComponentA);
                    entity.AddComponent(CID.ComponentA, componentA);
                    entity.RemoveComponentA();

                    entity.ApplyBlueprint(blueprint);

                    entity.GetComponentA().should_be_same(componentA);
                };
            };
        };
    }
    void when_throwing()
    {
        MyTestContext ctx    = null;
        TestEntity    entity = null;

        before = () => {
            var componentNames = new [] { "Health", "Position", "View" };
            var contextInfo    = new ContextInfo("My Context", componentNames, null);
            ctx    = new MyTestContext(componentNames.Length, 42, contextInfo);
            entity = ctx.CreateEntity();
        };

        context["Entity"] = () => {
            context["when not enabled"] = () => {
                before = () => {
                    entity.Destroy();
                };

                it["add a component"]     = () => printErrorMessage(() => entity.AddComponentA());
                it["remove a component"]  = () => printErrorMessage(() => entity.RemoveComponentA());
                it["replace a component"] = () => printErrorMessage(() => entity.ReplaceComponentA(Component.A));
            };

            context["when enabled"] = () => {
                it["add a component twice"] = () => printErrorMessage(() => {
                    entity.AddComponentA();
                    entity.AddComponentA();
                });

                it["remove a component that doesn't exist"] = () => printErrorMessage(() => {
                    entity.RemoveComponentA();
                });

                it["get a component that doesn't exist"] = () => printErrorMessage(() => {
                    entity.GetComponentA();
                });

                it["retain an entity twice"] = () => printErrorMessage(() => {
                    var owner = new object();
                    entity.Retain(owner);
                    entity.Retain(owner);
                });

                it["release an entity with wrong owner"] = () => printErrorMessage(() => {
                    var owner = new object();
                    entity.Release(owner);
                });
            };
        };

        context["Group"] = () => {
            it["get single entity when multiple exist"] = () => printErrorMessage(() => {
                ctx.CreateEntity().AddComponentA();
                ctx.CreateEntity().AddComponentA();
                var matcher            = (Matcher <TestEntity>)Matcher <TestEntity> .AllOf(CID.ComponentA);
                matcher.componentNames = ctx.contextInfo.componentNames;
                var group = ctx.GetGroup(matcher);
                group.GetSingleEntity();
            });
        };

        context["Collector<TestEntity>"] = () => {
            it["unbalanced goups"] = () => printErrorMessage(() => {
                var g1 = new Group <TestEntity>(Matcher <TestEntity> .AllOf(CID.ComponentA));
                var g2 = new Group <TestEntity>(Matcher <TestEntity> .AllOf(CID.ComponentB));
                var e1 = GroupEvent.Added;

                new Collector <TestEntity>(new [] { g1, g2 }, new [] { e1 });
            });
        };

        context["Context"] = () => {
            it["wrong ContextInfo componentNames count"] = () => printErrorMessage(() => {
                var componentNames = new [] { "Health", "Position", "View" };
                var contextInfo    = new ContextInfo("My Context", componentNames, null);
                new MyTestContext(1, 0, contextInfo);
            });

            it["destroy retained entities"] = () => printErrorMessage(() => {
                var e = ctx.CreateEntity();
                e.Retain(this);
                e.Retain(new object());

                e = ctx.CreateEntity();
                e.Retain(this);
                e.Retain(new object());

                ctx.DestroyAllEntities();
            });

            it["releases entity before destroy"] = () => printErrorMessage(() => {
                entity.Release(ctx);
            });

            it["unknown entityIndex"] = () => printErrorMessage(() => {
                ctx.GetEntityIndex("unknown");
            });

            it["duplicate entityIndex"] = () => printErrorMessage(() => {
                var groupA = ctx.GetGroup((Matcher <TestEntity>)Matcher <TestEntity> .AllOf(CID.ComponentA));
                var index  = new PrimaryEntityIndex <TestEntity, string>("TestIndex", groupA, (arg1, arg2) => string.Empty);
                ctx.AddEntityIndex(index);
                ctx.AddEntityIndex(index);
            });
        };

        context["CollectionExtension"] = () => {
            it["get single entity when more than one exist"] = () => printErrorMessage(() => {
                new IEntity[2].SingleEntity();
            });
        };

        context["ComponentBlueprint"] = () => {
            it["type doesn't implement IComponent"] = () => printErrorMessage(() => {
                var componentBlueprint          = new ComponentBlueprint();
                componentBlueprint.fullTypeName = "string";
                componentBlueprint.CreateComponent(entity);
            });

            it["type doesn't exist"] = () => printErrorMessage(() => {
                var componentBlueprint          = new ComponentBlueprint();
                componentBlueprint.fullTypeName = "UnknownType";
                componentBlueprint.CreateComponent(entity);
            });

            it["invalid field name"] = () => printErrorMessage(() => {
                var componentBlueprint          = new ComponentBlueprint();
                componentBlueprint.index        = 0;
                componentBlueprint.fullTypeName = typeof(NameAgeComponent).FullName;
                componentBlueprint.members      = new [] {
                    new SerializableMember("xxx", "publicFieldValue"),
                    new SerializableMember("publicProperty", "publicPropertyValue")
                };
                componentBlueprint.CreateComponent(entity);
            });
        };

        context["EntityIndex"] = () => {
            it["no entity with key"] = () => printErrorMessage(() => {
                var groupA = ctx.GetGroup((Matcher <TestEntity>)Matcher <TestEntity> .AllOf(CID.ComponentA));
                var index  = new PrimaryEntityIndex <TestEntity, string>("TestIndex", groupA, (e, c) => ((NameAgeComponent)c).name);
                index.GetEntity("unknownKey");
            });

            it["multiple entities for primary key"] = () => printErrorMessage(() => {
                var groupA = ctx.GetGroup((Matcher <TestEntity>)Matcher <TestEntity> .AllOf(CID.ComponentA));
                new PrimaryEntityIndex <TestEntity, string>("TestIndex", groupA, (e, c) => ((NameAgeComponent)c).name);

                var nameAge  = new NameAgeComponent();
                nameAge.name = "Max";
                nameAge.age  = 42;

                ctx.CreateEntity().AddComponent(CID.ComponentA, nameAge);
                ctx.CreateEntity().AddComponent(CID.ComponentA, nameAge);
            });
        };
    }
    void when_created()
    {
        IGroup <TestEntity>    groupA     = null;
        Collector <TestEntity> collectorA = null;
        IMatcher <TestEntity>  matcherA   = Matcher <TestEntity> .AllOf(CID.ComponentA);

        before = () => {
            _context = new MyTestContext();
            groupA   = _context.GetGroup(matcherA);
        };

        context["when observing with GroupEvent.Added"] = () => {
            before = () => {
                collectorA = new Collector <TestEntity>(groupA, GroupEvent.Added);
            };

            it["is empty when nothing happend"] = () => {
                collectorA.collectedEntities.should_be_empty();
            };

            context["when entity collected"] = () => {
                TestEntity e = null;

                before = () => {
                    e = createEA();
                };

                it["returns collected entities"] = () => {
                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(e);
                };

                it["only collects matching entities"] = () => {
                    createEB();

                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(e);
                };

                it["collects entities only once"] = () => {
                    e.RemoveComponentA();
                    e.AddComponentA();

                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(e);
                };

                it["clears collected entities"] = () => {
                    collectorA.ClearCollectedEntities();
                    collectorA.collectedEntities.should_be_empty();
                };

                it["clears collected entities on deactivation"] = () => {
                    collectorA.Deactivate();
                    collectorA.collectedEntities.should_be_empty();
                };

                it["doesn't collect entities when deactivated"] = () => {
                    collectorA.Deactivate();
                    createEA();
                    collectorA.collectedEntities.should_be_empty();
                };

                it["continues collecting when activated"] = () => {
                    collectorA.Deactivate();
                    createEA();

                    collectorA.Activate();

                    var e2 = createEA();

                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(e2);
                };

                it["can ToString"] = () => {
                    collectorA.ToString().should_be("Collector(Group(AllOf(1)))");
                };
            };

            context["reference counting"] = () => {
                TestEntity e = null;

                before = () => {
                    e = createEA();
                };

                it["retains entity even after destroy"] = () => {
                    var didExecute = 0;
                    e.OnEntityReleased += delegate { didExecute += 1; };
                    _context.DestroyEntity(e);
                    e.retainCount.should_be(1);
                    #if !ENTITAS_FAST_AND_UNSAFE
                    e.owners.should_contain(collectorA);
                    #endif
                    didExecute.should_be(0);
                };

                it["releases entity when clearing collected entities"] = () => {
                    _context.DestroyEntity(e);
                    collectorA.ClearCollectedEntities();
                    e.retainCount.should_be(0);
                };

                it["retains entities only once"] = () => {
                    e.ReplaceComponentA(new ComponentA());
                    _context.DestroyEntity(e);
                    e.retainCount.should_be(1);
                };
            };
        };

        context["when observing with GroupEvent.Removed"] = () => {
            before = () => {
                collectorA = new Collector <TestEntity>(groupA, GroupEvent.Removed);
            };

            it["returns collected entities"] = () => {
                var e = createEA();
                collectorA.collectedEntities.should_be_empty();

                e.RemoveComponentA();
                var entities = collectorA.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
            };
        };

        context["when observing with GroupEvent.AddedOrRemoved"] = () => {
            before = () => {
                collectorA = new Collector <TestEntity>(groupA, GroupEvent.AddedOrRemoved);
            };

            it["returns collected entities"] = () => {
                var e        = createEA();
                var entities = collectorA.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
                collectorA.ClearCollectedEntities();

                e.RemoveComponentA();
                entities = collectorA.collectedEntities;
                entities.Count.should_be(1);
                entities.should_contain(e);
            };
        };

        context["when observing multiple groups"] = () => {
            IGroup <TestEntity> groupB = null;

            before = () => {
                groupB = _context.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentB));
            };

            it["throws when group count != groupEvent count"] = expect <CollectorException>(() => {
                collectorA = new Collector <TestEntity>(
                    new [] { groupA },
                    new [] {
                    GroupEvent.Added,
                    GroupEvent.Added
                }
                    );
            });

            context["when observing with GroupEvent.Added"] = () => {
                before = () => {
                    collectorA = new Collector <TestEntity>(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEvent.Added,
                        GroupEvent.Added
                    }
                        );
                };

                it["returns collected entities"] = () => {
                    var eA = createEA();
                    var eB = createEB();

                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                };

                it["can ToString"] = () => {
                    collectorA.ToString().should_be("Collector(Group(AllOf(1)), Group(AllOf(2)))");
                };
            };

            context["when observing with GroupEvent.Removed"] = () => {
                before = () => {
                    collectorA = new Collector <TestEntity>(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEvent.Removed,
                        GroupEvent.Removed
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA = createEA();
                    var eB = createEB();
                    collectorA.collectedEntities.should_be_empty();

                    eA.RemoveComponentA();
                    eB.RemoveComponentB();
                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                };
            };

            context["when observing with GroupEvent.AddedOrRemoved"] = () => {
                before = () => {
                    collectorA = new Collector <TestEntity>(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEvent.AddedOrRemoved,
                        GroupEvent.AddedOrRemoved
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA       = createEA();
                    var eB       = createEB();
                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                    collectorA.ClearCollectedEntities();

                    eA.RemoveComponentA();
                    eB.RemoveComponentB();
                    entities = collectorA.collectedEntities;
                    entities.Count.should_be(2);
                    entities.should_contain(eA);
                    entities.should_contain(eB);
                };
            };

            context["when observing with mixed groupEvents"] = () => {
                before = () => {
                    collectorA = new Collector <TestEntity>(
                        new [] { groupA, groupB },
                        new [] {
                        GroupEvent.Added,
                        GroupEvent.Removed
                    }
                        );
                };
                it["returns collected entities"] = () => {
                    var eA       = createEA();
                    var eB       = createEB();
                    var entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eA);
                    collectorA.ClearCollectedEntities();

                    eA.RemoveComponentA();
                    eB.RemoveComponentB();
                    entities = collectorA.collectedEntities;
                    entities.Count.should_be(1);
                    entities.should_contain(eB);
                };
            };
        };
    }