<commit_before>package ktx.ashley

import com.badlogic.ashley.core.Component
import com.badlogic.ashley.core.Engine
import com.badlogic.ashley.core.Entity
import com.badlogic.ashley.systems.IntervalIteratingSystem
import com.badlogic.ashley.systems.IteratingSystem
import com.badlogic.ashley.systems.SortedIteratingSystem
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import java.util.Comparator

object ListenersSpec : Spek({
  describe("utilities for entity listeners") {
    val engine = Engine()

    it("should invoke an addition entity listener") {
      var invoked = false
      val listener = object : EntityAdditionListener {
        override fun entityAdded(entity: Entity) {
          invoked = true
        }
      }
      engine.addEntityListener(listener)
      val entity = engine.entity()
      engine.removeEntity(entity)
      assertThat(invoked).isTrue()
      engine.removeEntityListener(listener)
    }

    it("should invoke a removal entity listener") {
      var invoked = false
      val listener = object : EntityRemovalListener {
        override fun entityRemoved(entity: Entity) {
          invoked = true
        }
      }
      val entity = engine.entity()
      engine.addEntityListener(listener)
      engine.removeEntity(entity)
      assertThat(invoked).isTrue()
      engine.removeEntityListener(listener)
    }

    it("should invoke an addition entity listener made with a lambda") {
      var invoked = false
      val listener = engine.onEntityAdded {
        invoked = true
      }
      val entity = engine.entity()
      assertThat(invoked).isTrue()
      engine.removeEntity(entity)
      engine.removeEntityListener(listener)
    }

    it("should invoke a removal entity listener made with a lambda") {
      var invoked = false
      val entity = engine.entity()
      val listener = engine.onEntityRemoved {
        invoked = true
      }
      engine.removeEntity(entity)
      assertThat(invoked).isTrue()
      engine.removeEntityListener(listener)
    }

    it("should run listeners bound to an iterating system") {
      var addAssertion = false
      var removeAssertion = false

      val system = object : IteratingSystem(allOf(TestComponent::class).get()) {
        private lateinit var additionListener: EntityAdditionListener
        private lateinit var removalListener: EntityRemovalListener

        override fun processEntity(entity: Entity, deltaTime: Float) = Unit

        override fun addedToEngine(engine: Engine) {
          super.addedToEngine(engine)

          additionListener = onEntityAdded { entity ->
            if (entity[TestComponent.mapper] != null) {
              addAssertion = true
            }
          }

          removalListener = onEntityRemoved { entity ->
            if (entity[TestComponent.mapper] != null) {
              removeAssertion = true
            }
          }
        }

        override fun removedFromEngine(engine: Engine) {
          super.removedFromEngine(engine)

          engine.removeEntityListener(additionListener)
          engine.removeEntityListener(removalListener)
        }
      }

      engine.addSystem(system)
      val entity = engine.entity {
        with<TestComponent>()
      }
      engine.removeEntity(entity)
      engine.removeSystem(system)

      assertThat(addAssertion).isTrue()
      assertThat(removeAssertion).isTrue()
    }

    it("should run listeners bound to an interval iterating system") {
      var addAssertion = false
      var removeAssertion = false

      val system = object : IntervalIteratingSystem(allOf(TestComponent::class).get(), 0f) {
        private lateinit var additionListener: EntityAdditionListener
        private lateinit var removalListener: EntityRemovalListener

        override fun processEntity(entity: Entity) = Unit

        override fun addedToEngine(engine: Engine) {
          super.addedToEngine(engine)

          additionListener = onEntityAdded { entity ->
            if (entity[TestComponent.mapper] != null) {
              addAssertion = true
            }
          }

          removalListener = onEntityRemoved { entity ->
            if (entity[TestComponent.mapper] != null) {
              removeAssertion = true
            }
          }
        }

        override fun removedFromEngine(engine: Engine) {
          super.removedFromEngine(engine)

          engine.removeEntityListener(additionListener)
          engine.removeEntityListener(removalListener)
        }
      }

      engine.addSystem(system)
      val entity = engine.entity {
        with<TestComponent>()
      }
      engine.removeEntity(entity)
      engine.removeSystem(system)

      assertThat(addAssertion).isTrue
      assertThat(removeAssertion).isTrue
    }

    it("should run listeners bound to a sorted iterating system") {
      var addAssertion = false
      var removeAssertion = false

      val system = object : SortedIteratingSystem(allOf(TestComponent::class).get(), Comparator.comparing(Any::hashCode)) {
        private lateinit var additionListener: EntityAdditionListener
        private lateinit var removalListener: EntityRemovalListener

        override fun processEntity(entity: Entity, deltaTime: Float) = Unit

        override fun addedToEngine(engine: Engine) {
          super.addedToEngine(engine)

          additionListener = onEntityAdded { entity ->
            if (entity[TestComponent.mapper] != null) {
              addAssertion = true
            }
          }

          removalListener = onEntityRemoved { entity ->
            if (entity[TestComponent.mapper] != null) {
              removeAssertion = true
            }
          }
        }

        override fun removedFromEngine(engine: Engine) {
          super.removedFromEngine(engine)

          engine.removeEntityListener(additionListener)
          engine.removeEntityListener(removalListener)
        }
      }

      engine.addSystem(system)
      val entity = engine.entity {
        with<TestComponent>()
      }
      engine.removeEntity(entity)
      engine.removeSystem(system)

      assertThat(addAssertion).isTrue
      assertThat(removeAssertion).isTrue
    }
  }
})

internal class TestComponent : Component {
  companion object : Mapper<TestComponent>()
}<commit_msg>Entity listener test improvements. #409<commit_after>package ktx.ashley

import com.badlogic.ashley.core.Component
import com.badlogic.ashley.core.Engine
import com.badlogic.ashley.core.Entity
import com.badlogic.ashley.systems.IntervalIteratingSystem
import com.badlogic.ashley.systems.IteratingSystem
import com.badlogic.ashley.systems.SortedIteratingSystem
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import java.util.Comparator

object ListenersSpec : Spek({
  describe("utilities for entity listeners") {
    val engine = Engine()

    it("should invoke an addition entity listener") {
      var invoked = false
      val listener = object : EntityAdditionListener {
        override fun entityAdded(entity: Entity) {
          invoked = true
        }
      }
      engine.addEntityListener(listener)
      val entity = engine.entity()
      engine.removeEntity(entity)
      assertThat(invoked).isTrue()
      engine.removeEntityListener(listener)
    }

    it("should invoke a removal entity listener") {
      var invoked = false
      val listener = object : EntityRemovalListener {
        override fun entityRemoved(entity: Entity) {
          invoked = true
        }
      }
      val entity = engine.entity()
      engine.addEntityListener(listener)
      engine.removeEntity(entity)
      assertThat(invoked).isTrue()
      engine.removeEntityListener(listener)
    }

    it("should invoke an addition entity listener made with a lambda") {
      var invoked = false
      val listener = engine.onEntityAdded {
        invoked = true
      }
      val entity = engine.entity()
      assertThat(invoked).isTrue()
      engine.removeEntity(entity)
      engine.removeEntityListener(listener)
    }

    it("should invoke a removal entity listener made with a lambda") {
      var invoked = false
      val entity = engine.entity()
      val listener = engine.onEntityRemoved {
        invoked = true
      }
      engine.removeEntity(entity)
      assertThat(invoked).isTrue()
      engine.removeEntityListener(listener)
    }

    it("should run listeners bound to an iterating system") {
      var addAssertion = false
      var removeAssertion = false

      val system = object : IteratingSystem(allOf(TestComponent::class).get()) {
        private lateinit var additionListener: EntityAdditionListener
        private lateinit var removalListener: EntityRemovalListener

        override fun processEntity(entity: Entity, deltaTime: Float) = Unit

        override fun addedToEngine(engine: Engine) {
          super.addedToEngine(engine)

          additionListener = onEntityAdded { entity ->
            if (entity[TestComponent.mapper] != null) {
              addAssertion = true
            }
          }

          removalListener = onEntityRemoved { entity ->
            if (entity[TestComponent.mapper] != null) {
              removeAssertion = true
            }
          }
        }

        override fun removedFromEngine(engine: Engine) {
          super.removedFromEngine(engine)

          engine.removeEntityListener(additionListener)
          engine.removeEntityListener(removalListener)
        }
      }

      engine.addSystem(system)

      engine.entity().apply(engine::removeEntity)
      assertThat(addAssertion).isFalse
      assertThat(removeAssertion).isFalse

      val entity = engine.entity {
        with<TestComponent>()
      }
      engine.removeEntity(entity)
      engine.removeSystem(system)

      assertThat(addAssertion).isTrue
      assertThat(removeAssertion).isTrue
    }

    it("should run listeners bound to an interval iterating system") {
      var addAssertion = false
      var removeAssertion = false

      val system = object : IntervalIteratingSystem(allOf(TestComponent::class).get(), 0f) {
        private lateinit var additionListener: EntityAdditionListener
        private lateinit var removalListener: EntityRemovalListener

        override fun processEntity(entity: Entity) = Unit

        override fun addedToEngine(engine: Engine) {
          super.addedToEngine(engine)

          additionListener = onEntityAdded { entity ->
            if (entity[TestComponent.mapper] != null) {
              addAssertion = true
            }
          }

          removalListener = onEntityRemoved { entity ->
            if (entity[TestComponent.mapper] != null) {
              removeAssertion = true
            }
          }
        }

        override fun removedFromEngine(engine: Engine) {
          super.removedFromEngine(engine)

          engine.removeEntityListener(additionListener)
          engine.removeEntityListener(removalListener)
        }
      }

      engine.addSystem(system)

      engine.entity().apply(engine::removeEntity)
      assertThat(addAssertion).isFalse
      assertThat(removeAssertion).isFalse

      val entity = engine.entity {
        with<TestComponent>()
      }
      engine.removeEntity(entity)
      engine.removeSystem(system)

      assertThat(addAssertion).isTrue
      assertThat(removeAssertion).isTrue
    }

    it("should run listeners bound to a sorted iterating system") {
      var addAssertion = false
      var removeAssertion = false

      val system = object : SortedIteratingSystem(allOf(TestComponent::class).get(), Comparator.comparing(Any::hashCode)) {
        private lateinit var additionListener: EntityAdditionListener
        private lateinit var removalListener: EntityRemovalListener

        override fun processEntity(entity: Entity, deltaTime: Float) = Unit

        override fun addedToEngine(engine: Engine) {
          super.addedToEngine(engine)

          additionListener = onEntityAdded { entity ->
            if (entity[TestComponent.mapper] != null) {
              addAssertion = true
            }
          }

          removalListener = onEntityRemoved { entity ->
            if (entity[TestComponent.mapper] != null) {
              removeAssertion = true
            }
          }
        }

        override fun removedFromEngine(engine: Engine) {
          super.removedFromEngine(engine)

          engine.removeEntityListener(additionListener)
          engine.removeEntityListener(removalListener)
        }
      }

      engine.addSystem(system)

      engine.entity().apply(engine::removeEntity)
      assertThat(addAssertion).isFalse
      assertThat(removeAssertion).isFalse

      val entity = engine.entity {
        with<TestComponent>()
      }
      engine.removeEntity(entity)
      engine.removeSystem(system)

      assertThat(addAssertion).isTrue
      assertThat(removeAssertion).isTrue
    }
  }
})

internal class TestComponent : Component {
  companion object : Mapper<TestComponent>()
}
{'fraction_non_alphanumeric': 0.08129218169409266, 'fraction_numerical': 0.00037916129521498444, 'mean_word_length': 2.534709193245779, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, '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': '1079816', 'n_tokens_mistral': 3398, 'n_tokens_neox': 3328, 'n_words': 731}