<commit_before>﻿using NSpec;
using Entitas;

class describe_Pool : nspec {
    Pool _pool;

    void before_each() {
        _pool = new Pool(CID.NumComponents);
    }

    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 entities that were not created with CreateEntity()"] = () => {
            _pool.HasEntity(this.CreateEntity()).should_be_false();
        };

        it["has entities 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 entities"] = () => {
            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 OnGroupCreated 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);
            };<commit_msg>Ignores "entity object pool returns pushed entity" test.<commit_after>﻿using NSpec;
using Entitas;

class describe_Pool : nspec {
    Pool _pool;

    void before_each() {
        _pool = new Pool(CID.NumComponents);
    }

    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 entities that were not created with CreateEntity()"] = () => {
            _pool.HasEntity(this.CreateEntity()).should_be_false();
        };

        it["has entities 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 entities"] = () => {
            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 OnGroupCreated 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();
            };

            xit["returns pushed entity"] = () => {

                // #if (ENTITAS_ENTITY_OBJECT_POOL)

                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);
            };
{'fraction_non_alphanumeric': 0.12204951065054692, 'fraction_numerical': 0.0028785261945883708, 'mean_word_length': 1.5368747718145308, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3813826', 'n_tokens_mistral': 3759, 'n_tokens_neox': 3564, 'n_words': 643}