コード例 #1
0
    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());
            };
        };
    }
コード例 #2
0
    void when_extending()
    {
        context["when copying components"] = () => {
            IContext <TestEntity> ctx     = null;
            TestEntity            entity  = null;
            TestEntity            target  = null;
            NameAgeComponent      nameAge = null;

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

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

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

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

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

                entity.CopyTo(target);

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

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

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

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

                entity.CopyTo(target);
            });

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

                entity.CopyTo(target, true);

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

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

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

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

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

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

                entity.CopyTo(target);

                target.GetComponentA().should_be_same(component);
            };
        };
    }