Exemplo n.º 1
0
    void when_creating()
    {
        IContext <TestEntity> ctx    = null;
        TestEntity            entity = null;

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

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

                const int index = 42;

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

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

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

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

                const int index = 24;

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

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

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

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

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

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

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

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

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

                entity.AddComponent(CID.ComponentB, component);

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

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

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

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

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

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

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

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

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

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

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

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

                    entity.ApplyBlueprint(blueprint, true);
                };

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

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

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

                    entity.ApplyBlueprint(blueprint);

                    entity.GetComponentA().should_be_same(componentA);
                };
            };
        };
    }
    void when_throwing()
    {
        before = () => {
            var componentNames = new [] { "Health", "Position", "View" };
            var metaData       = new PoolMetaData("My Pool", componentNames, null);
            _pool   = new Pool(componentNames.Length, 42, metaData);
            _entity = createEntity();
        };

        it["creates exception with hint separated by newLine"] = () => {
            // given
            const string msg  = "Message";
            const string hint = "Hint";
            var          ex   = new EntitasException(msg, hint);

            // then
            ex.Message.should_be(msg + "\n" + hint);
        };

        it["ignores hint when null"] = () => {
            // given
            const string msg  = "Message";
            string       hint = null;
            var          ex   = new EntitasException(msg, hint);

            // then
            ex.Message.should_be(msg);
        };

        context["Entity"] = () => {
            context["when not enabled"] = () => {
                before = () => {
                    _pool.DestroyEntity(_entity);
                };

                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(() => {
                createEntityA();
                createEntityA();
                var matcher            = createMatcherA();
                matcher.componentNames = _pool.metaData.componentNames;
                var group = _pool.GetGroup(matcher);
                group.GetSingleEntity();
            });
        };

        context["GroupObserver"] = () => {
            it["unbalanced goups"] = () => printErrorMessage(() => {
                var g1 = new Group(Matcher.AllOf(CID.ComponentA));
                var g2 = new Group(Matcher.AllOf(CID.ComponentB));
                var e1 = GroupEventType.OnEntityAdded;

                new GroupObserver(new [] { g1, g2 }, new [] { e1 });
            });
        };

        context["Pool"] = () => {
            it["wrong PoolMetaData componentNames count"] = () => printErrorMessage(() => {
                var componentNames = new [] { "Health", "Position", "View" };
                var metaData       = new PoolMetaData("My Pool", componentNames, null);
                new Pool(1, 0, metaData);
            });

            it["destroy entity which is not in pool"] = () => printErrorMessage(() => {
                _pool.DestroyEntity(new Entity(0, null));
            });

            it["destroy retained entities"] = () => printErrorMessage(() => {
                createEntity().Retain(this);
                _pool.DestroyAllEntities();
            });

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

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

            it["duplicate entityIndex"] = () => printErrorMessage(() => {
                var index = new PrimaryEntityIndex <string>(getGroupA(), null);
                _pool.AddEntityIndex("duplicate", index);
                _pool.AddEntityIndex("duplicate", index);
            });
        };

        context["CollectionExtension"] = () => {
            it["get single entity when more than one exist"] = () => printErrorMessage(() => {
                new Entity[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(() => {
                createPrimaryIndex().GetEntity("unknownKey");
            });

            it["multiple entities for primary key"] = () => printErrorMessage(() => {
                createPrimaryIndex();
                var nameAge = createNameAge();
                _pool.CreateEntity().AddComponent(CID.ComponentA, nameAge);
                _pool.CreateEntity().AddComponent(CID.ComponentA, nameAge);
            });
        };
    }
    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);
            });
        };
    }