예제 #1
0
        public static IAnyOfMatcher AnyOf(params IMatcher[] matchers)
        {
            var anyOfMatcher = (Matcher)Matcher.AnyOf(mergeIndices(matchers));

            setComponentNames(anyOfMatcher, matchers);
            return(anyOfMatcher);
        }
예제 #2
0
        public void NewListAllocatedFromPoolPerGroup()
        {
            var groupA   = _defaultContext.GetGroup(Matcher <MyTestEntity> .AllOf(MyTestComponentsLookup.ComponentA));
            var groupAB  = _defaultContext.GetGroup(Matcher <MyTestEntity> .AnyOf(MyTestComponentsLookup.ComponentA, MyTestComponentsLookup.ComponentB));
            var groupABC = _defaultContext.GetGroup(Matcher <MyTestEntity> .AnyOf(MyTestComponentsLookup.ComponentA, MyTestComponentsLookup.ComponentB, MyTestComponentsLookup.ComponentC));

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

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

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

            _defaultContext.CreateEntity().AddComponentA();

            Assert.AreEqual(3, _didDispatch);
        }
        public void AllOfDoesNotEqualAnyOfWithSameIndices()
        {
            var m1 = Matcher <TestEntity> .AllOf(CID.ComponentA);

            var m2 = Matcher <TestEntity> .AnyOf(CID.ComponentA);

            Assert.AreNotEqual(m2, m1);
            Assert.AreNotEqual(m2.GetHashCode(), m1.GetHashCode());
        }
예제 #4
0
 public void SetPool(Pool <IUIPool> typedPool)
 {
     _pool   = typedPool;
     _events = _pool.GetGroup(Matcher.AnyOf(
                                  typeof(PointerClick),
                                  typeof(PointerOut),
                                  typeof(PointerOver),
                                  typeof(PointerPress),
                                  typeof(PointerRelease)));
 }
        public void AnyOfMatcherHasAllIndicesWithoutDuplicates()
        {
            _anyOfMatcher = Matcher <MyTestEntity> .AnyOf(
                CID.ComponentA,
                CID.ComponentA,
                CID.ComponentB,
                CID.ComponentB);

            AssertIndicesContain(_anyOfMatcher.Indices, CID.ComponentA, CID.ComponentB);
            AssertIndicesContain(_anyOfMatcher.AnyOfIndices, CID.ComponentA, CID.ComponentB);
        }
        public void AnyOfMergesMatchersToNewMatcherWithoutDuplicates()
        {
            var m1 = Matcher <TestEntity> .AnyOf(CID.ComponentA);

            var m2 = Matcher <TestEntity> .AnyOf(CID.ComponentB);

            var m3 = Matcher <TestEntity> .AnyOf(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);
        }
        public void DifferentTypeMatchersWithSameIndicesAreNotEqual()
        {
            var m1 = Matcher <TestEntity> .AllOf(CID.ComponentA);

            var m2 = Matcher <TestEntity> .AllOf(CID.ComponentB);

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

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

            Assert.AreNotEqual(m4, m3);
            Assert.AreNotEqual(m4.GetHashCode(), m3.GetHashCode());
        }
예제 #8
0
        public void AnyOfMergesMatchersToNewMatcher()
        {
            var m1 = Matcher <TestEntity> .AnyOf(MyTestComponentsLookup.ComponentA);

            var m2 = Matcher <TestEntity> .AnyOf(MyTestComponentsLookup.ComponentB);

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

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

            AssertIndicesContain(mergedMatcher.Indices, MyTestComponentsLookup.ComponentA, MyTestComponentsLookup.ComponentB, MyTestComponentsLookup.ComponentC);
            AssertIndicesContain(mergedMatcher.AnyOfIndices, MyTestComponentsLookup.ComponentA, MyTestComponentsLookup.ComponentB, MyTestComponentsLookup.ComponentC);
        }
        public void CompoundMatchersCanEqual()
        {
            var m1 = Matcher <TestEntity> .AllOf(CID.ComponentA);

            var m2 = Matcher <TestEntity> .AnyOf(CID.ComponentB);

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

            var m4 = Matcher <TestEntity> .AnyOf(CID.ComponentD);

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

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

            Assert.AreEqual(mY, mX);
            Assert.AreEqual(mY.GetHashCode(), mX.GetHashCode());
        }
예제 #10
0
        public void pops_new_list_from_list_pool()
        {
            pool.DestroyAllEntities();

            var gA   = pool.GetGroup(Matcher.AllOf(typeof(TestComponentA)));
            var gAB  = pool.GetGroup(Matcher.AnyOf(typeof(TestComponentA), typeof(TestComponentB)));
            var gABC = pool.GetGroup(Matcher.AnyOf(typeof(TestComponentA), typeof(TestComponentB), typeof(TestComponentC)));

            var did = 0;

            gA.OnEntityAdded += (grp, e, type, comp) =>
            {
                did += 1;
                e.Remove <TestComponentA>();
            };

            gAB.OnEntityAdded  += delegate { did += 1; };
            gABC.OnEntityAdded += delegate { did += 1; };

            pool.CreateEntity().Add <TestComponentA>();

            Assert.AreEqual(3, did);
        }
        public void Setup4()
        {
            GroupAll  = Pool.GetGroup(Matcher.AllOf(typeof(NameTestComponent), typeof(IdTestComponent)));
            GroupAny  = Pool.GetGroup(Matcher.AnyOf(typeof(NameTestComponent), typeof(IdTestComponent)));
            GroupNone = Pool.GetGroup(Matcher.AllOf(typeof(IdTestComponent)).NoneOf(typeof(NameTestComponent)));

            Entity3 = Pool.CreateEntity().Add <IdTestComponent>(id => id.Id = Id1);
            Entity4 = Pool.CreateEntity().Add <NameTestComponent>(n => n.Name = Key2).Add <IdTestComponent>(id => id.Id = Id2);

            IndexAll = new EntityIndex <ITestSecondPool, string>(GroupAll, (e, c) => e.Get <NameTestComponent>().Name);
            IndexAny = new EntityIndex <ITestSecondPool, string>(GroupAny, (e, c) =>
            {
                if (e.Has <NameTestComponent>())
                {
                    return(e.Get <NameTestComponent>().Name);
                }
                else
                {
                    return(e.Get <IdTestComponent>().Id.ToString());
                }
            });

            IndexNone = new EntityIndex <ITestSecondPool, int>(GroupNone, (e, c) => e.Get <IdTestComponent>().Id);
        }
예제 #12
0
 public void SetPools(Pools pools)
 {
     _pools         = new [] { pools.core, pools.bullets };
     _groupObserver = _pools.CreateEntityCollector(Matcher.AnyOf(CoreMatcher.Destroy, CoreMatcher.OutOfScreen));
 }
예제 #13
0
 public static IAnyOfMatcher <Entity> AnyOf <T1, T2, T3, T4, T5, T6>() where T1 : IComponent where T2 : IComponent where T3 : IComponent where T4 : IComponent where T5 : IComponent where T6 : IComponent
 {
     return(Matcher.AnyOf(IDX <T1>(), IDX <T2>(), IDX <T3>(), IDX <T4>(), IDX <T5>(), IDX <T6>()));
 }
예제 #14
0
 public static IAnyOfMatcher <Entity> AnyOf <T1, T2, T3>() where T1 : IComponent where T2 : IComponent where T3 : IComponent
 {
     return(Matcher.AnyOf(IDX <T1>(), IDX <T2>(), IDX <T3>()));
 }
예제 #15
0
 public static IAnyOfMatcher <Entity> AnyOf <T1>() where T1 : IComponent
 {
     return(Matcher.AnyOf(IDX <T1>()));
 }
    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());
            };
        };
    }
예제 #17
0
    void when_created()
    {
        it["doesn't have entites which haven't been added"] = () => {
            _group.GetEntities().should_be_empty();
        };

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

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

            it["adds matching entity"] = () => _group.GetEntities().should_contain(_eA1);
            it["isn't empty"]          = () => _group.Count.should_be(1);
            it["contains entity"]      = () => _group.ContainsEntity(_eA1).should_be_true();

            it["doesn't add same entity twice"] = () => {
                handle(_eA1);
                _group.GetEntities().should_contain(_eA1);
                _group.GetEntities().Length.should_be(1);
            };
        };

        context["when entity doesn't match"] = () => {
            before = () => {
                handle(_eA1);
                _eA1.RemoveComponentA();
                handle(_eA1);
            };

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

            it["removes entity"]          = () => _group.GetEntities().should_be_empty();
            it["doesn't contains entity"] = () => _group.ContainsEntity(_eA1).should_be_false();
        };

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

        it["gets single entity"] = () => {
            handle(_eA1);
            _group.GetSingleEntity().should_be_same(_eA1);
        };

        it["throws when attempting to get single entity and multiple matching entities exist"] = expect <SingleEntityException>(() => {
            handle(_eA1);
            handle(_eA2);
            _group.GetSingleEntity();
        });

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

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

            it["dispatches OnEntityAdded when matching entity added"] = () => {
                _group.OnEntityAdded += (group, entity) => {
                    didDispatch++;
                    group.should_be_same(_group);
                    entity.should_be_same(_eA1);
                };

                handle(_eA1);
                didDispatch.should_be(1);
            };

            it["doesn't dispatches OnEntityAdded when matching entity already has been added"] = () => {
                _group.OnEntityAdded += (group, entity) => {
                    didDispatch++;
                    group.should_be_same(_group);
                    entity.should_be_same(_eA1);
                };

                handle(_eA1);
                handle(_eA1);
                didDispatch.should_be(1);
            };

            it["dispatches OnEntityRemoved when entity got removed"] = () => {
                _group.OnEntityRemoved += (group, entity) => {
                    didDispatch++;
                    group.should_be_same(_group);
                    entity.should_be_same(_eA1);
                };

                handle(_eA1);
                _eA1.RemoveComponentA();
                handle(_eA1);

                didDispatch.should_be(1);
            };

            it["doesn't dispatch OnEntityRemoved when entity didn't get removed"] = () => {
                _group.OnEntityRemoved += (group, entity) => this.Fail();
                _eA1.RemoveComponentA();
                handle(_eA1);
            };

            it["dispatches OnEntityWillBeRemoved when entity will be removed"] = () => {
                _group.OnEntityWillBeRemoved += (group, entity) => {
                    didDispatch++;
                    group.should_be_same(_group);
                    entity.should_be_same(_eA1);
                };

                handle(_eA1);
                willRemoveEA1();
                didDispatch.should_be(1);
            };

            it["doesn't dispatch OnEntityWillBeRemoved when group doesn't contain entity"] = () => {
                _group.OnEntityWillBeRemoved += (group, entity) => this.Fail();
                handle(_eA1);
                _group.WillRemoveEntity(new Entity(0));
            };

            it["dispatches OnEntityRemoved and OnEntityAdded when updating"] = () => {
                handle(_eA1);
                var removed = 0;
                var added   = 0;
                _group.OnEntityRemoved       += (group, entity) => removed++;
                _group.OnEntityWillBeRemoved += (group, entity) => this.Fail();
                _group.OnEntityAdded         += (group, entity) => added++;

                _group.UpdateEntity(_eA1);

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

            it["doesn't dispatch OnEntityRemoved and OnEntityAdded when updating when group doesn't contain entity"] = () => {
                _group.OnEntityRemoved       += (group, entity) => this.Fail();
                _group.OnEntityWillBeRemoved += (group, entity) => this.Fail();
                _group.OnEntityAdded         += (group, entity) => this.Fail();
                _group.UpdateEntity(_eA1);
            };
        };

        context["internal caching"] = () => {
            it["gets cached entities"] = () => {
                handle(_eA1);
                _group.GetEntities().should_be_same(_group.GetEntities());
            };

            it["updates cache when adding a new matching entity"] = () => {
                handle(_eA1);
                var g = _group.GetEntities();
                handle(_eA2);
                g.should_not_be_same(_group.GetEntities());
            };

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

            it["updates cache when removing an entity"] = () => {
                handle(_eA1);
                var g = _group.GetEntities();
                _eA1.RemoveComponentA();
                handle(_eA1);
                g.should_not_be_same(_group.GetEntities());
            };

            it["doesn't update cache when attempting to remove an entity that wasn't added before"] = () => {
                var g = _group.GetEntities();
                _eA1.RemoveComponentA();
                handle(_eA1);
                g.should_be_same(_group.GetEntities());
            };

            it["gets cached singleEntities"] = () => {
                handle(_eA1);
                _group.GetSingleEntity().should_be_same(_group.GetSingleEntity());
            };

            it["updates cache when new single entity was added"] = () => {
                handle(_eA1);
                var s = _group.GetSingleEntity();
                _eA1.RemoveComponentA();
                handle(_eA1);
                handle(_eA2);
                s.should_not_be_same(_group.GetSingleEntity());
            };

            it["updates cache when single entity is removed"] = () => {
                handle(_eA1);
                var s = _group.GetSingleEntity();
                _eA1.RemoveComponentA();
                handle(_eA1);
                s.should_not_be_same(_group.GetSingleEntity());
            };
        };

        it["can ToString"] = () => {
            var m     = Matcher.NoneOf(Matcher.AllOf(0), Matcher.AnyOf(1));
            var group = new Group(m);
            group.ToString().should_be("Group(NoneOf(AllOf(0), AnyOf(1)))");
        };
    }
 private void SetupAnyOfMatcher()
 {
     _anyOfMatcher = Matcher <MyTestEntity> .AnyOf(CID.ComponentA, CID.ComponentB);
 }
예제 #19
0
    void when_compounding_matchers()
    {
        context["allOf"] = () => {
            AllOfMatcher         allAB    = null;
            AllOfMatcher         allBC    = null;
            AnyOfMatcher         anyAB    = null;
            AnyOfMatcher         anyBC    = null;
            AllOfCompoundMatcher compound = null;
            before = () => {
                allAB = Matcher.AllOf(new[] {
                    CID.ComponentB,
                    CID.ComponentA
                });
                allBC = Matcher.AllOf(new[] {
                    CID.ComponentC,
                    CID.ComponentB
                });
                anyAB = Matcher.AnyOf(new[] {
                    CID.ComponentB,
                    CID.ComponentA
                });
                anyBC = Matcher.AnyOf(new[] {
                    CID.ComponentC,
                    CID.ComponentB
                });
            };

            it["has all indices in order"] = () => {
                compound = Matcher.AllOf(allAB, allBC);
                compound.indices.should_be(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC
                });
            };

            it["has all indices in order (mixed)"] = () => {
                compound = Matcher.AllOf(allAB, anyBC);
                compound.indices.should_be(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC
                });
            };

            it["matches"] = () => {
                compound = Matcher.AllOf(allAB, allBC);
                var e = this.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                e.AddComponentC();
                compound.Matches(e).should_be_true();
            };

            it["matches (mixed)"] = () => {
                compound = Matcher.AllOf(allAB, anyBC);
                var e = this.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                compound.Matches(e).should_be_true();
            };

            it["doesn't match"] = () => {
                compound = Matcher.AllOf(allAB, allBC);
                var e = this.CreateEntity();
                e.AddComponentB();
                e.AddComponentC();
                compound.Matches(e).should_be_false();
            };

            it["doesn't match (mixed)"] = () => {
                compound = Matcher.AllOf(anyAB, anyBC);
                var e = this.CreateEntity();
                e.AddComponentC();
                compound.Matches(e).should_be_false();
            };
        };

        context["anyOf"] = () => {
            AllOfMatcher         allAB    = null;
            AllOfMatcher         allBC    = null;
            AnyOfMatcher         anyBC    = null;
            AnyOfCompoundMatcher compound = null;
            before = () => {
                allAB = Matcher.AllOf(new[] {
                    CID.ComponentB,
                    CID.ComponentA
                });
                allBC = Matcher.AllOf(new[] {
                    CID.ComponentC,
                    CID.ComponentB
                });
                anyBC = Matcher.AnyOf(new[] {
                    CID.ComponentC,
                    CID.ComponentB
                });
            };

            it["has all indices in order"] = () => {
                compound = Matcher.AnyOf(allAB, allBC);
                compound.indices.should_be(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC
                });
            };

            it["has all indices in order (mixed)"] = () => {
                compound = Matcher.AnyOf(allAB, anyBC);
                compound.indices.should_be(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC
                });
            };

            it["matches"] = () => {
                compound = Matcher.AnyOf(allBC, allAB);
                var e = this.CreateEntity();
                e.AddComponentB();
                e.AddComponentC();
                compound.Matches(e).should_be_true();
            };

            it["matches (mixed)"] = () => {
                compound = Matcher.AnyOf(allAB, anyBC);
                var e = this.CreateEntity();
                e.AddComponentC();
                compound.Matches(e).should_be_true();
            };

            it["doesn't match"] = () => {
                compound = Matcher.AnyOf(allAB, allBC);
                var e = this.CreateEntity();
                e.AddComponentA();
                e.AddComponentC();
                compound.Matches(e).should_be_false();
            };
        };

        context["noneOf"] = () => {
            AllOfMatcher          allAB    = null;
            AllOfMatcher          allBC    = null;
            AllOfMatcher          allAC    = null;
            AnyOfMatcher          anyBC    = null;
            NoneOfCompoundMatcher compound = null;
            before = () => {
                allAB = Matcher.AllOf(new[] {
                    CID.ComponentB,
                    CID.ComponentA
                });
                allBC = Matcher.AllOf(new[] {
                    CID.ComponentC,
                    CID.ComponentB
                });
                allAC = Matcher.AllOf(new[] {
                    CID.ComponentC,
                    CID.ComponentA
                });
                anyBC = Matcher.AnyOf(new[] {
                    CID.ComponentC,
                    CID.ComponentB
                });
            };

            it["has all indices in order"] = () => {
                compound = Matcher.NoneOf(allAB, allBC);
                compound.indices.should_be(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC
                });
            };

            it["matches"] = () => {
                compound = Matcher.NoneOf(allAB, allAC);
                var e = this.CreateEntity();
                e.AddComponentB();
                e.AddComponentC();
                compound.Matches(e).should_be_true();
            };

            it["matches (mixed)"] = () => {
                compound = Matcher.NoneOf(allAB, anyBC);
                var e = this.CreateEntity();
                e.AddComponentA();
                compound.Matches(e).should_be_true();
            };

            it["doesn't match"] = () => {
                compound = Matcher.NoneOf(allAB, anyBC);
                var e = this.CreateEntity();
                e.AddComponentC();
                compound.Matches(e).should_be_false();
            };
        };

        context["equals"] = () => {
            it["doesn't equal when only indices are same"] = () => {
                var all1 = Matcher.AllOf(0, 1);
                var all2 = Matcher.AllOf(2, 3);
                var c1   = Matcher.AllOf(all1, all2);

                var any1 = Matcher.AnyOf(0, 1);
                var any2 = Matcher.AnyOf(2, 3);
                var c2   = Matcher.AllOf(any1, any2);

                c1.Equals(c2).should_be_false();
            };

            it["doesn't equal when not same type"] = () => {
                var all1 = Matcher.AllOf(0, 1);
                var all2 = Matcher.AllOf(2, 3);
                var c1   = Matcher.AllOf(all1, all2);
                var c2   = Matcher.AnyOf(all1, all2);

                c1.Equals(c2).should_be_false();
            };

            it["equals when equal"] = () => {
                var all1 = Matcher.AllOf(0, 1);
                var all2 = Matcher.AllOf(2, 3);
                var c1   = Matcher.AllOf(all1, all2);

                var all3 = Matcher.AllOf(0, 1);
                var all4 = Matcher.AllOf(2, 3);
                var c2   = Matcher.AllOf(all3, all4);

                c1.Equals(c2).should_be_true();
            };
        };

        context["nested"] = () => {
            it["works like a charme"] = () => {
                var allAB = Matcher.AllOf(CID.ComponentA, CID.ComponentB);
                var allCD = Matcher.AllOf(CID.ComponentC, CID.ComponentD);
                var allEF = Matcher.AllOf(CID.ComponentE, CID.ComponentF);
                var anyEF = Matcher.AnyOf(CID.ComponentE, CID.ComponentF);

                var c1 = Matcher.AllOf(allAB, allCD, anyEF);
                var c2 = Matcher.AllOf(allAB, allCD, allEF);
                var c3 = Matcher.AnyOf(allAB, allCD, allEF);

                var e = this.CreateEntity();
                e.AddComponentA();
                e.AddComponentB();
                e.AddComponentC();
                e.AddComponentD();
                e.AddComponentE();

                c1.Matches(e).should_be_true();
                c2.Matches(e).should_be_false();
                c3.Matches(e).should_be_true();

                var nested1 = Matcher.AllOf(c1, c2);
                var nested2 = Matcher.AnyOf(c1, c2);
                nested1.Matches(e).should_be_false();
                nested2.Matches(e).should_be_true();

                nested1.indices.should_be(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC,
                    CID.ComponentD,
                    CID.ComponentE,
                    CID.ComponentF
                });
                nested2.indices.should_be(new [] {
                    CID.ComponentA,
                    CID.ComponentB,
                    CID.ComponentC,
                    CID.ComponentD,
                    CID.ComponentE,
                    CID.ComponentF
                });

                var nestedAll = Matcher.AllOf(nested1, nested2);
                var nestedAny = Matcher.AnyOf(nested1, nested2);
                nestedAll.Matches(e).should_be_false();
                nestedAny.Matches(e).should_be_true();

                Matcher.NoneOf(nestedAll, nestedAny).Matches(e).should_be_false();
            };
        };

        context["can ToString"] = () => {
            AllOfMatcher  allOf  = null;
            AnyOfMatcher  anyOf  = null;
            NoneOfMatcher noneOf = null;
            before = () => {
                allOf  = Matcher.AllOf(CID.ComponentA, CID.ComponentB);
                anyOf  = Matcher.AnyOf(CID.ComponentC, CID.ComponentD);
                noneOf = Matcher.NoneOf(CID.ComponentE, CID.ComponentF);
            };

            it["AllOfCompoundMatcher"] = () => {
                var m = Matcher.AllOf(allOf, anyOf, noneOf);
                m.ToString().should_be("AllOf(AllOf(1, 2), AnyOf(3, 4), NoneOf(5, 6))");
            };

            it["AnyOfCompoundMatcher"] = () => {
                var m = Matcher.AnyOf(allOf, anyOf, noneOf);
                m.ToString().should_be("AnyOf(AllOf(1, 2), AnyOf(3, 4), NoneOf(5, 6))");
            };

            it["NoneOfCompoundMatcher"] = () => {
                var m = Matcher.NoneOf(allOf, anyOf, noneOf);
                m.ToString().should_be("NoneOf(AllOf(1, 2), AnyOf(3, 4), NoneOf(5, 6))");
            };
        };
    }
예제 #20
0
 public NotifySelectedListeners(Pool observedPool)
 {
     selectedListeners = observedPool.GetGroup(Matcher.AnyOf(ViewMatcher.SelectedListener, UIMatcher.SelectedListener));
 }
        public void AnyOfThrowsWhenMergingMatcherWithMoreThanOneIndex()
        {
            var m1 = Matcher <TestEntity> .AnyOf(CID.ComponentA, CID.ComponentB);

            Assert.Throws <MatcherException>(() => Matcher <TestEntity> .AnyOf(m1));
        }
예제 #22
0
 public static IAnyOfMatcher AnyOf(params IMatcher[] matchers)
 {
     return(Matcher.AnyOf(mergeIndices(matchers)));
 }
예제 #23
0
 public TeardownBattleSystem(GameContext context)
 {
     battleEntities =
         context.GetGroup(Matcher <GameEntity> .AnyOf(GameMatcher.BattleAction));
     this.context = context;
 }
예제 #24
0
    void when_creating_matcher()
    {
        Entity eA   = null;
        Entity eC   = null;
        Entity eAB  = null;
        Entity eABC = null;

        before = () => {
            eA   = this.CreateEntity();
            eC   = this.CreateEntity();
            eAB  = this.CreateEntity();
            eABC = this.CreateEntity();
            eA.AddComponentA();
            eC.AddComponentC();
            eAB.AddComponentA();
            eAB.AddComponentB();
            eABC.AddComponentA();
            eABC.AddComponentB();
            eABC.AddComponentC();
        };

        context["allOf"] = () => {
            IMatcher m = null;
            before = () => m = Matcher.AllOf(new [] {
                CID.ComponentA,
                CID.ComponentA,
                CID.ComponentB
            });

            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["gets triggering types without duplicates"] = () => {
                m.indices.Length.should_be(2);
                m.indices.should_contain(CID.ComponentA);
                m.indices.should_contain(CID.ComponentB);
            };
        };

        context["anyOf"] = () => {
            IMatcher m = null;
            before = () => m = Matcher.AnyOf(new [] {
                CID.ComponentA,
                CID.ComponentA,
                CID.ComponentB
            });

            it["doesn't match"] = () => {
                m.Matches(eC).should_be_false();
            };

            it["matches"] = () => {
                m.Matches(eA).should_be_true();
                m.Matches(eAB).should_be_true();
                m.Matches(eABC).should_be_true();
            };
        };

        context["noneOf"] = () => {
            IMatcher m = null;
            before = () => m = Matcher.NoneOf(new [] {
                CID.ComponentA,
                CID.ComponentB
            });

            it["doesn't match"] = () => {
                m.Matches(eA).should_be_false();
                m.Matches(eAB).should_be_false();
            };

            it["matches"] = () => {
                m.Matches(eC).should_be_true();
                m.Matches(this.CreateEntity()).should_be_true();
            };
        };

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

            it["equals equal AllOfMatcher independent from the order of indices"] = () => {
                var m1 = allOfAB();
                var m2 = Matcher.AllOf(new [] {
                    CID.ComponentB,
                    CID.ComponentA
                });

                m1.should_not_be_same(m2);
                m1.Equals(m2).should_be_true();
            };

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

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

            it["generates same hash for equal AllOfMatcher"] = () => {
                var m1 = allOfAB();
                var m2 = allOfAB();
                m1.GetHashCode().should_be(m2.GetHashCode());
            };

            it["generates same hash independent from the order of indices"] = () => {
                var m1 = Matcher.AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB
                });
                var m2 = Matcher.AllOf(new [] {
                    CID.ComponentB,
                    CID.ComponentA
                });
                m1.GetHashCode().should_be(m2.GetHashCode());
            };

            it["AllOfMatcher doesn't equal AnyOfMatcher with same indices"] = () => {
                var m1 = Matcher.AllOf(new [] {
                    CID.ComponentA, CID.ComponentB
                });
                var m2 = Matcher.AnyOf(new [] {
                    CID.ComponentA, CID.ComponentB
                });

                m1.Equals(m2).should_be_false();
            };
        };
    }
예제 #25
0
    void when_created()
    {
        it["increments creationIndex"] = () => {
            _pool.CreateEntity().creationIndex.should_be(0);
            _pool.CreateEntity().creationIndex.should_be(1);
        };

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

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

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

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

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

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

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

        it["destroys entity and removes it"] = () => {
            var e = _pool.CreateEntity();
            _pool.DestroyEntity(e);
            _pool.HasEntity(e).should_be_false();
        };

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

        it["destroys all entites"] = () => {
            var e = _pool.CreateEntity();
            e.AddComponentA();
            _pool.CreateEntity();
            _pool.DestroyAllEntities();
            _pool.GetEntities().should_be_empty();
            e.GetComponents().should_be_empty();
        };

        it["caches entities"] = () => {
            _pool.CreateEntity();
            var entities1 = _pool.GetEntities();
            var entities2 = _pool.GetEntities();
            entities1.should_be_same(entities2);
            _pool.DestroyEntity(_pool.CreateEntity());
            _pool.GetEntities().should_not_be_same(entities1);
        };

        context["events"] = () => {
            it["dispatches OnEntityCreated when creating a new entity"] = () => {
                Pool   eventPool   = null;
                Entity eventEntity = null;
                _pool.OnEntityCreated += (pool, entity) => {
                    eventPool   = pool;
                    eventEntity = entity;
                };

                var e = _pool.CreateEntity();
                eventPool.should_be_same(_pool);
                eventEntity.should_be_same(e);
            };

            it["dispatches OnEntityWillBeDestroyed when destroying a new entity"] = () => {
                var e = _pool.CreateEntity();
                e.AddComponentA();
                Pool   eventPool   = null;
                Entity eventEntity = null;
                _pool.OnEntityWillBeDestroyed += (pool, entity) => {
                    eventPool   = pool;
                    eventEntity = entity;
                    entity.HasComponentA().should_be_true();
                };
                _pool.DestroyEntity(e);
                eventPool.should_be_same(_pool);
                eventEntity.should_be_same(e);
            };

            it["dispatches OnEntityDestroyed when destroying a new entity"] = () => {
                var    e           = _pool.CreateEntity();
                Pool   eventPool   = null;
                Entity eventEntity = null;
                _pool.OnEntityDestroyed += (pool, entity) => {
                    eventPool   = pool;
                    eventEntity = entity;
                    entity.HasComponentA().should_be_false();
                };
                _pool.DestroyEntity(e);
                eventPool.should_be_same(_pool);
                eventEntity.should_be_same(e);
            };

            it["dispatches OnGroupCreated when creating a new group"] = () => {
                Pool  eventPool  = null;
                Group eventGroup = null;
                _pool.OnGroupCreated += (pool, g) => {
                    eventPool  = pool;
                    eventGroup = g;
                };
                var group = _pool.GetGroup(Matcher.AllOf(0));
                eventPool.should_be_same(_pool);
                eventGroup.should_be_same(group);
            };

            it["doesn't dispatch OnGroupCreeated when group alredy exists"] = () => {
                _pool.GetGroup(Matcher.AllOf(0));
                _pool.OnGroupCreated += (pool, g) => this.Fail();
                _pool.GetGroup(Matcher.AllOf(0));
            };
        };

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

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

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

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

            it["sets up entity from pool"] = () => {
                _pool.DestroyEntity(_pool.CreateEntity());
                var g = _pool.GetGroup(Matcher.AllOf(new [] { CID.ComponentA }));
                var e = _pool.CreateEntity();
                e.AddComponentA();
                g.GetEntities().should_contain(e);
            };
        };

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

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

                IMatcher matcher = Matcher.AllOf(new [] {
                    CID.ComponentA,
                    CID.ComponentB
                });

                before = () => {
                    eAB1 = _pool.CreateEntity();
                    eAB1.AddComponentA();
                    eAB1.AddComponentB();
                    eAB2 = _pool.CreateEntity();
                    eAB2.AddComponentA();
                    eAB2.AddComponentB();
                    eA = _pool.CreateEntity();
                    eA.AddComponentA();
                };

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

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

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

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

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

                it["ignores adding components to destroyed entity"] = () => {
                    var g = _pool.GetGroup(matcher);
                    _pool.DestroyEntity(eA);
                    eA.AddComponentA();
                    eA.AddComponentB();
                    g.GetEntities().should_not_contain(eA);
                };

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

                it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => {
                    var g = _pool.GetGroup(matcher);
                    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        = _pool.GetGroup(Matcher.AllOf(new [] { 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);
                };
            };
        };

        context["getGroup"] = () => {
            context["AnyOfCompoundMatcher"] = () => {
                AllOfMatcher         allOfA   = null;
                AllOfMatcher         allOfB   = null;
                AnyOfCompoundMatcher compound = null;
                Group  group = null;
                Entity e     = null;
                before = () => {
                    allOfA   = Matcher.AllOf(CID.ComponentA);
                    allOfB   = Matcher.AllOf(CID.ComponentB);
                    compound = Matcher.AnyOf(allOfA, allOfB);
                    group    = _pool.GetGroup(compound);
                    e        = _pool.CreateEntity();
                };

                it["adds entity when matching"] = () => {
                    e.AddComponentA();
                    compound.Matches(e).should_be_true();
                    group.Count.should_be(1);
                };

                it["doesn't add entity when not matching"] = () => {
                    e.AddComponentC();
                    compound.Matches(e).should_be_false();
                    group.Count.should_be(0);
                };

                it["removes entity when not matching anymore"] = () => {
                    e.AddComponentA();
                    e.RemoveComponentA();
                    group.Count.should_be(0);
                };

                it["doesn't remove entity when still matching"] = () => {
                    e.AddComponentA();
                    e.AddComponentB();
                    e.RemoveComponentB();
                    group.Count.should_be(1);
                };
            };

            context["AllOfOfCompoundMatcher containing a NoneOfMatcher"] = () => {
                AllOfMatcher         allOfAB  = null;
                NoneOfMatcher        noneOfC  = null;
                AllOfCompoundMatcher compound = null;
                Group  group = null;
                Entity e     = null;
                before = () => {
                    allOfAB  = Matcher.AllOf(CID.ComponentA, CID.ComponentB);
                    noneOfC  = Matcher.NoneOf(CID.ComponentC);
                    compound = Matcher.AllOf(allOfAB, noneOfC);
                    group    = _pool.GetGroup(compound);
                    e        = _pool.CreateEntity();
                };

                it["adds entity when matching"] = () => {
                    e.AddComponentA();
                    e.AddComponentB();
                    compound.Matches(e).should_be_true();
                    group.Count.should_be(1);
                };

                it["doesn't add entity when not matching"] = () => {
                    e.AddComponentA();
                    e.AddComponentB();
                    e.AddComponentC();
                    compound.Matches(e).should_be_false();
                    group.Count.should_be(0);
                };

                it["removes entity when not matching anymore"] = () => {
                    e.AddComponentA();
                    e.AddComponentB();
                    e.RemoveComponentB();
                    group.Count.should_be(0);
                };

                it["doesn't remove entity when still matching"] = () => {
                    e.AddComponentA();
                    e.AddComponentB();
                    e.AddComponentC();
                    e.RemoveComponentC();
                    group.Count.should_be(1);
                };
            };
        };
    }
예제 #26
0
 public static IAnyOfMatcher <Entity> AnyOf(params int[] indices)
 {
     return(Matcher <Entity> .AnyOf(indices));
 }
예제 #27
0
 public TeardownCharacterSystem(GameContext context)
 {
     characterEntities = context.GetGroup(Matcher <GameEntity> .AnyOf(GameMatcher.Enemy, GameMatcher.Player));
 }
예제 #28
0
 public static IAnyOfMatcher <Entity> AnyOf(params IMatcher <Entity>[] matchers)
 {
     return(Matcher <Entity> .AnyOf(matchers));
 }
예제 #29
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);
            };
        };
    }
예제 #30
0
    void when_creating_matcher()
    {
        Entity eA   = null;
        Entity eB   = null;
        Entity eC   = null;
        Entity eAB  = null;
        Entity 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 m = null;

            before = () => m = Matcher.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.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.AllOf(new [] { CID.ComponentA });
                var m2            = Matcher.AllOf(new [] { CID.ComponentB });
                var m3            = Matcher.AllOf(new [] { CID.ComponentC });
                var mergedMatcher = Matcher.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.AllOf(new [] { CID.ComponentA });
                var m2            = Matcher.AllOf(new [] { CID.ComponentA });
                var m3            = Matcher.AllOf(new [] { CID.ComponentB });
                var mergedMatcher = Matcher.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.AllOf(new [] { CID.ComponentA, CID.ComponentB });
                Matcher.AllOf(m1);
            });

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

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

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

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

                var mergedMatcher = Matcher.AllOf(m1, m2, m3);
                mergedMatcher.ToString().should_be("AllOf(m_1, m_2, m_3)");
            };
        };

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

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

            it["has all indices"] = () => {
                m = Matcher.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.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.AnyOf(new [] { CID.ComponentA });
                var m2            = Matcher.AnyOf(new [] { CID.ComponentB });
                var m3            = Matcher.AnyOf(new [] { CID.ComponentC });
                var mergedMatcher = Matcher.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.AnyOf(new [] { CID.ComponentA });
                var m2            = Matcher.AnyOf(new [] { CID.ComponentB });
                var m3            = Matcher.AnyOf(new [] { CID.ComponentB });
                var mergedMatcher = Matcher.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.AnyOf(new [] { CID.ComponentA, CID.ComponentB });
                Matcher.AnyOf(m1);
            });

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

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

            before = () => m = Matcher.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.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.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.AllOf(new [] { CID.ComponentA });
                var m2 = Matcher.AllOf(new [] { CID.ComponentB });
                var m3 = Matcher.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)m;
                matcher.componentNames = new [] { "one", "two", "three", "four", "five" };
                matcher.ToString().should_be("AllOf(two, three).NoneOf(four, five)");
            };
        };

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

            before = () => m = Matcher.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.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.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.AllOf(new [] { CID.ComponentA });
                var m2 = Matcher.AllOf(new [] { CID.ComponentB });
                var m3 = Matcher.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 m = null;

            before = () => m = Matcher.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.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.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.AllOf(new [] { CID.ComponentA });
                var m2 = Matcher.AllOf(new [] { CID.ComponentB });
                var m3 = Matcher.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.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.AllOf(new [] { CID.ComponentA });
                var cache = m.indices;
                m.NoneOf(new [] { CID.ComponentB });
                m.indices.should_not_be_same(cache);
            };
        };

        context["equals"] = () => {
            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.AllOf(new [] { CID.ComponentA });
                var m2 = Matcher.AllOf(new [] { CID.ComponentB });
                var m3 = allOfBA();

                var mergedMatcher = Matcher.AllOf(m1, m2);
                mergedMatcher.Equals(m3).should_be_true();
                mergedMatcher.GetHashCode().should_be(m3.GetHashCode());
            };

            it["doesn't equal different AllOfMatcher"] = () => {
                var m1 = Matcher.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.AllOf(new [] { CID.ComponentA });
                var m2 = Matcher.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.AllOf(new [] { CID.ComponentA });
                var m2 = Matcher.AllOf(new [] { CID.ComponentB });

                var m3 = Matcher.AllOf(m1, m2);
                var m4 = Matcher.AnyOf(m1, m2);
                m3.Equals(m4).should_be_false();
                m3.GetHashCode().should_not_be(m4.GetHashCode());
            };

            it["equals compound matcher"] = () => {
                var m1 = Matcher.AllOf(new [] { CID.ComponentA });
                var m2 = Matcher.AnyOf(new [] { CID.ComponentB });
                var m3 = Matcher.AnyOf(new [] { CID.ComponentC });
                var m4 = Matcher.AnyOf(new [] { CID.ComponentD });

                var mX = Matcher.AllOf(m1, m2).AnyOf(m3, m4);
                var mY = Matcher.AllOf(m1, m2).AnyOf(m3, m4);

                mX.Equals(mY).should_be_true();
                mX.GetHashCode().should_be(mY.GetHashCode());
            };
        };

        context["when filtering"] = () => {
            IMatcher m = null;

            before = () => m = Matcher.AllOf(CID.ComponentD).Where(entity => {
                var component = (NameAgeComponent)entity.GetComponent(CID.ComponentD);
                return(component.age > 30);
            });

            it["only contains entities passing the filter condition"] = () => {
                var e1       = this.CreateEntity();
                var nameAge1 = new NameAgeComponent {
                    name = "Max", age = 42
                };
                e1.AddComponent(CID.ComponentD, nameAge1);

                var e2       = this.CreateEntity();
                var nameAge2 = new NameAgeComponent {
                    name = "Jack", age = 24
                };
                e2.AddComponent(CID.ComponentD, nameAge2);

                m.Matches(e1).should_be_true();
                m.Matches(e2).should_be_false();
            };

            it["doesn't attempt to filter entites not matching the matcher"] = () => {
                var e = this.CreateEntity();
                m.Matches(e).should_be_false();
            };
        };
    }