<commit_before>/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (<EMAIL>).
 * See LICENSE for details.
 */
@file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE")
package com.almasb.fxgl.entity

import com.almasb.fxgl.core.math.Vec2
import com.almasb.fxgl.core.serialization.Bundle
import com.almasb.fxgl.entity.component.Component
import com.almasb.fxgl.entity.component.ComponentListener
import com.almasb.fxgl.entity.component.Required
import com.almasb.fxgl.entity.component.SerializableComponent
import com.almasb.fxgl.entity.components.*
import com.almasb.fxgl.physics.BoundingShape
import com.almasb.fxgl.physics.HitBox
import javafx.geometry.Point2D
import javafx.geometry.Point3D
import javafx.geometry.Rectangle2D
import org.hamcrest.CoreMatchers.*
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.Serializable

/**
 *
 *
 * @author <NAME> (<EMAIL>)
 */
class EntityTest {

    private lateinit var entity: Entity

    @BeforeEach
    fun setUp() {
        entity = Entity()
    }

    @Test
    fun `Add component`() {
        val comp = TestComponent()
        entity.addComponent(comp)

        assertThat(entity.components, hasItem(comp))
    }

    @Test
    fun `Add component fails if same type already exists`() {
        val exception = assertDoesNotThrow {
            entity.addComponent(TestComponent())
            entity.addComponent(TestComponent())
        }
    }

    @Test
    fun `Add component does not fail if component is anonymous`() {
        val c = object : Component() {}
        entity.addComponent(c)

        assertThat(entity.components, hasItem<Component>(c))
    }

    @Test
    fun `Add component fails if required component is missing`() {
        assertThrows(IllegalStateException::class.java, {
            entity.addComponent(RequireTestComponent())
        })
    }

    @Test
    fun `Get component`() {
        val comp = TestComponent()
        entity.addComponent(comp)

        assertThat(entity.getComponent(TestComponent::class.java), `is`(comp))
    }

    @Test
    fun `Get component fails if not present`() {
        assertThrows(IllegalArgumentException::class.java, {
            entity.getComponent(TestComponent::class.java)
        })
    }

    @Test
    fun `Get component optional`() {
        val comp = TestComponent()
        entity.addComponent(comp)

        assertThat(entity.getComponentOptional(TestComponent::class.java).get(), `is`(comp))
    }

    @Test
    fun `Get core components`() {
        assertThat(entity.transformComponent, `is`(entity.getComponent(TransformComponent::class.java)))
        assertThat(entity.boundingBoxComponent, `is`(entity.getComponent(BoundingBoxComponent::class.java)))
        assertThat(entity.viewComponent, `is`(entity.getComponent(ViewComponent::class.java)))
        assertThat(entity.typeComponent, `is`(entity.getComponent(TypeComponent::class.java)))
    }

    @Test
    fun `Has component`() {
        entity.addComponent(TestComponent())

        assertTrue(entity.hasComponent(TestComponent::class.java))
    }

    @Test
    fun `Remove component returns true if removed`() {
        val comp = TestComponent()
        entity.addComponent(comp)

        val isRemoved = entity.removeComponent(TestComponent::class.java)

        assertTrue(isRemoved)
        assertThat(entity.components, not(hasItem(comp)))
    }

    @Test
    fun `Remove component returns false if not found`() {
        val isRemoved = entity.removeComponent(TestComponent::class.java)

        assertFalse(isRemoved)
    }

    @Test
    fun `Remove component fails if component is required by another component`() {
        entity.addComponent(TestComponent())
        entity.addComponent(RequireTestComponent())

        assertThrows(IllegalArgumentException::class.java, {
            entity.removeComponent(TestComponent::class.java)
        })
    }

    @Test
    fun `Remove component fails if component is required by a control`() {
        entity.addComponent(HPComponent(0.0))
        entity.addComponent(HPControl())

        val e = assertThrows(IllegalArgumentException::class.java, {
            entity.removeComponent(HPComponent::class.java)
        })

        assertThat(e.message, `is`("Required component: [HPComponent] by: HPControl"))
    }

    @Test
    fun `Removing core component is ignored`() {
        assertThat(entity.components.size, `is`(4))

        assertDoesNotThrow {
            entity.removeComponent(TypeComponent::class.java)
            entity.removeComponent(TransformComponent::class.java)
            entity.removeComponent(BoundingBoxComponent::class.java)
            entity.removeComponent(ViewComponent::class.java)
        }

        assertThat(entity.components.size, `is`(4))
    }

    @Test
    fun `Add control`() {
        val control = TestControl()
        entity.addComponent(control)

        assertThat(entity.components, hasItem(control))
    }

    // Test seems duplicate, see l.51 `Add component fails if same type already exists`
    @Test
    fun `Add control fails if same type exists`() {
        val control = TestControl()
        entity.addComponent(control)

        assertDoesNotThrow {
            entity.addComponent(control)
        }
    }

    @Test
    fun `Add control fails if required module is missing`() {
        assertThrows(IllegalStateException::class.java, {
            entity.addComponent(HPControl())
        })
    }

    @Test
    fun `Add control with required module`() {
        entity.addComponent(HPComponent(0.0))
        entity.addComponent(HPControl())

        assertTrue(entity.hasComponent(HPControl::class.java))
    }

    @Test
    fun `Add control fails if within update of another control`() {
        val control = ControlAddingControl()
        entity.addComponent(control)<commit_msg>Updated test name, removed duplicate test<commit_after>/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (<EMAIL>).
 * See LICENSE for details.
 */
@file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE")
package com.almasb.fxgl.entity

import com.almasb.fxgl.core.math.Vec2
import com.almasb.fxgl.core.serialization.Bundle
import com.almasb.fxgl.entity.component.Component
import com.almasb.fxgl.entity.component.ComponentListener
import com.almasb.fxgl.entity.component.Required
import com.almasb.fxgl.entity.component.SerializableComponent
import com.almasb.fxgl.entity.components.*
import com.almasb.fxgl.physics.BoundingShape
import com.almasb.fxgl.physics.HitBox
import javafx.geometry.Point2D
import javafx.geometry.Point3D
import javafx.geometry.Rectangle2D
import org.hamcrest.CoreMatchers.*
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.Serializable

/**
 *
 *
 * @author <NAME> (<EMAIL>)
 */
class EntityTest {

    private lateinit var entity: Entity

    @BeforeEach
    fun setUp() {
        entity = Entity()
    }

    @Test
    fun `Add component`() {
        val comp = TestComponent()
        entity.addComponent(comp)

        assertThat(entity.components, hasItem(comp))
    }

    @Test
    fun `Add component is ignored if same type already exists`() {
        val exception = assertDoesNotThrow {
            entity.addComponent(TestComponent())
            entity.addComponent(TestComponent())
        }
    }

    @Test
    fun `Add component does not fail if component is anonymous`() {
        val c = object : Component() {}
        entity.addComponent(c)

        assertThat(entity.components, hasItem<Component>(c))
    }

    @Test
    fun `Add component fails if required component is missing`() {
        assertThrows(IllegalStateException::class.java, {
            entity.addComponent(RequireTestComponent())
        })
    }

    @Test
    fun `Get component`() {
        val comp = TestComponent()
        entity.addComponent(comp)

        assertThat(entity.getComponent(TestComponent::class.java), `is`(comp))
    }

    @Test
    fun `Get component fails if not present`() {
        assertThrows(IllegalArgumentException::class.java, {
            entity.getComponent(TestComponent::class.java)
        })
    }

    @Test
    fun `Get component optional`() {
        val comp = TestComponent()
        entity.addComponent(comp)

        assertThat(entity.getComponentOptional(TestComponent::class.java).get(), `is`(comp))
    }

    @Test
    fun `Get core components`() {
        assertThat(entity.transformComponent, `is`(entity.getComponent(TransformComponent::class.java)))
        assertThat(entity.boundingBoxComponent, `is`(entity.getComponent(BoundingBoxComponent::class.java)))
        assertThat(entity.viewComponent, `is`(entity.getComponent(ViewComponent::class.java)))
        assertThat(entity.typeComponent, `is`(entity.getComponent(TypeComponent::class.java)))
    }

    @Test
    fun `Has component`() {
        entity.addComponent(TestComponent())

        assertTrue(entity.hasComponent(TestComponent::class.java))
    }

    @Test
    fun `Remove component returns true if removed`() {
        val comp = TestComponent()
        entity.addComponent(comp)

        val isRemoved = entity.removeComponent(TestComponent::class.java)

        assertTrue(isRemoved)
        assertThat(entity.components, not(hasItem(comp)))
    }

    @Test
    fun `Remove component returns false if not found`() {
        val isRemoved = entity.removeComponent(TestComponent::class.java)

        assertFalse(isRemoved)
    }

    @Test
    fun `Remove component fails if component is required by another component`() {
        entity.addComponent(TestComponent())
        entity.addComponent(RequireTestComponent())

        assertThrows(IllegalArgumentException::class.java, {
            entity.removeComponent(TestComponent::class.java)
        })
    }

    @Test
    fun `Remove component fails if component is required by a control`() {
        entity.addComponent(HPComponent(0.0))
        entity.addComponent(HPControl())

        val e = assertThrows(IllegalArgumentException::class.java, {
            entity.removeComponent(HPComponent::class.java)
        })

        assertThat(e.message, `is`("Required component: [HPComponent] by: HPControl"))
    }

    @Test
    fun `Removing core component is ignored`() {
        assertThat(entity.components.size, `is`(4))

        assertDoesNotThrow {
            entity.removeComponent(TypeComponent::class.java)
            entity.removeComponent(TransformComponent::class.java)
            entity.removeComponent(BoundingBoxComponent::class.java)
            entity.removeComponent(ViewComponent::class.java)
        }

        assertThat(entity.components.size, `is`(4))
    }

    @Test
    fun `Add control`() {
        val control = TestControl()
        entity.addComponent(control)

        assertThat(entity.components, hasItem(control))
    }

    @Test
    fun `Add control fails if required module is missing`() {
        assertThrows(IllegalStateException::class.java, {
            entity.addComponent(HPControl())
        })
    }

    @Test
    fun `Add control with required module`() {
        entity.addComponent(HPComponent(0.0))
        entity.addComponent(HPControl())

        assertTrue(entity.hasComponent(HPControl::class.java))
    }

    @Test
    fun `Add control fails if within update of another control`() {
        val control = ControlAddingControl()
        entity.addComponent(control)
{'fraction_non_alphanumeric': 0.10936426116838488, 'fraction_numerical': 0.0018900343642611684, 'mean_word_length': 3.0448227936066714, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5831146', 'n_tokens_mistral': 3287, 'n_tokens_neox': 3228, 'n_words': 653}