Exemplo n.º 1
0
    public void Run()
    {
        var name = 9.ToString();

        for (int i = 0; i < n; i++)
        {
            _index.GetEntity(name);
        }
    }
    void when_throwing()
    {
        MyTestContext ctx    = null;
        TestEntity    entity = null;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                ctx.DestroyAllEntities();
            });

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

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

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

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

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

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

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

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

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

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

                ctx.CreateEntity().AddComponent(CID.ComponentA, nameAge);
                ctx.CreateEntity().AddComponent(CID.ComponentA, nameAge);
            });
        };
    }
Exemplo n.º 3
0
    void when_primary_index()
    {
        context["single key"] = () => {
            PrimaryEntityIndex <TestEntity, string> index = null;
            IContext <TestEntity> ctx   = null;
            IGroup <TestEntity>   group = null;

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

            context["when entity for key doesn't exist"] = () => {
                it["returns null when getting entity for unknown key"] = () => {
                    index.GetEntity("unknownKey").should_be_null();
                };
            };

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

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

                it["gets entity for key"] = () => {
                    index.GetEntity(name).should_be_same(entity);
                };

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

                it["has existing entity"] = () => {
                    var newIndex = new PrimaryEntityIndex <TestEntity, string>("TestIndex", group, (e, c) => {
                        var nameAge = c as NameAgeComponent;
                        return(nameAge != null
                            ? nameAge.name
                            : ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name);
                    });
                    newIndex.GetEntity(name).should_be_same(entity);
                };

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

                it["throws when adding an entity for the same key"] = expect <EntityIndexException>(() => {
                    var nameAgeComponent  = new NameAgeComponent();
                    nameAgeComponent.name = name;
                    entity = ctx.CreateEntity();
                    entity.AddComponent(CID.ComponentA, nameAgeComponent);
                });

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

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

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

                    it["doesn't add entities anymore"] = () => {
                        var nameAgeComponent = new NameAgeComponent();
                        nameAgeComponent.name = name;
                        ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);
                        index.GetEntity(name).should_be_null();
                    };

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

                        it["has existing entity"] = () => {
                            index.GetEntity(name).should_be_same(entity);
                        };

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

                            index.GetEntity("Jack").should_be_same(entity);
                        };
                    };
                };
            };
        };

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

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

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

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

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

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

                it["gets entity for key"] = () => {
                    index.GetEntity(name + "1").should_be_same(entity);
                    index.GetEntity(name + "2").should_be_same(entity);
                };

                it["releases and removes entity from index when component gets removed"] = () => {
                    entity.RemoveComponent(CID.ComponentA);
                    index.GetEntity(name + "1").should_be_null();
                    index.GetEntity(name + "2").should_be_null();
                    entity.retainCount.should_be(1);

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

                it["has existing entity"] = () => {
                    index.Deactivate();
                    index.Activate();
                    index.GetEntity(name + "1").should_be_same(entity);
                    index.GetEntity(name + "2").should_be_same(entity);
                };
            };
        };
    }
        public void PrimaryEntityIndexReturnsNullEntityForUnknownKey()
        {
            SetupActivatedPrimaryEntityIndex();

            Assert.IsNull(_primaryIndex.GetEntity("unknownKey"));
        }