Last active
August 29, 2015 14:02
-
-
Save andresaraujo/04f52a5d7404678bc22a to your computer and use it in GitHub Desktop.
Ashley spock unit test example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ashley | |
import spock.lang.Specification | |
import ashley.core.Entity | |
import ashley.core.Component | |
class ComponentA extends Component {} | |
class ComponentB extends Component {} | |
class ComponentC extends Component {} | |
class EmptyEntitySpec extends Specification { | |
def entity = new Entity() | |
def "index"() { | |
expect: | |
entity.getIndex() == 2 | |
entity2.getIndex() == 0 | |
entity3.getIndex() == 1 | |
//these are instantiated before entity | |
where: | |
entity2 = new Entity() | |
entity3 = new Entity() | |
} | |
def "componentBits should be empty"() { | |
expect: | |
entity.getComponentBits().isEmpty() == true | |
} | |
def "Should have no components"() { | |
expect: | |
entity.getComponents().getSize() == 0 | |
} | |
def "Remove should return null"() { | |
expect: | |
entity.remove(ComponentA.class) == null | |
} | |
def "getComponent should return null"() { | |
expect: | |
entity.getComponent(ComponentA.class) == null | |
} | |
def "hasComponent should return false"() { | |
expect: | |
entity.hasComponent(ComponentA.class) == false | |
} | |
def "add"() { | |
when: | |
entity.add(compA) | |
then: | |
entity.hasComponent(ComponentA.class) == true | |
entity.hasComponent(ComponentB.class) == false | |
entity.getComponent(ComponentA.class) == compA | |
where: | |
compA = new ComponentA() | |
} | |
} | |
class TwoComponentEntitySpec extends Specification { | |
def entity = new Entity() | |
def setup() { | |
entity.add(new ComponentA()) | |
entity.add(new ComponentB()) | |
} | |
def "componentBits should not be empty"() { | |
expect: | |
entity.getComponentBits().isEmpty() == false | |
} | |
def "Should have two components"() { | |
expect: | |
entity.getComponents().getSize() == 2 | |
} | |
def "getComponent should return not null"() { | |
expect: | |
entity.getComponent(ComponentA.class) != null | |
} | |
def "hasComponent should return true"() { | |
expect: | |
entity.hasComponent(ComponentA.class) == true | |
entity.hasComponent(ComponentB.class) == true | |
} | |
def "remove"() { | |
when: | |
entity.remove(ComponentA) | |
then: | |
entity.hasComponent(ComponentA.class) == false | |
entity.getComponent(ComponentA.class) == null | |
entity.getComponents().getSize() == 1 | |
} | |
def "add"() { | |
when: | |
entity.add(compC) | |
then: | |
entity.hasComponent(ComponentC.class) == true | |
entity.getComponent(ComponentC.class) == compC | |
entity.getComponents().getSize() == 3 | |
where: | |
compC = new ComponentC() | |
} | |
def "removeAll"() { | |
when: | |
entity.removeAll() | |
then: | |
entity.hasComponent(ComponentA.class) == false | |
entity.hasComponent(ComponentB.class) == false | |
entity.getComponent(ComponentA.class) == null | |
entity.getComponent(ComponentB.class) == null | |
entity.getComponents().getSize() == 0 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ashley | |
import spock.lang.Specification | |
import ashley.core.Component | |
import ashley.core.ComponentType | |
class ComponentTypeSpec extends Specification { | |
class ComponentA extends Component {} | |
class ComponentB extends Component {} | |
def "Should have the same component type"() { | |
expect: | |
componentType1.equals(componentType2) | |
componentType2.equals(componentType1) | |
componentType1.getIndex() == componentType2.getIndex() | |
componentType1.getIndex() == ComponentType.getIndexFor(ComponentA.class) | |
componentType2.getIndex() == ComponentType.getIndexFor(ComponentA.class) | |
where: | |
componentType1 = ComponentType.getTypeFor(ComponentA.class) | |
componentType2 = ComponentType.getTypeFor(ComponentA.class) | |
} | |
def "Should have the different component type"() { | |
expect: | |
!componentType1.equals(componentType2) | |
!componentType2.equals(componentType1) | |
componentType1.getIndex() != componentType2.getIndex() | |
componentType1.getIndex() != ComponentType.getIndexFor(ComponentB.class) | |
componentType2.getIndex() != ComponentType.getIndexFor(ComponentA.class) | |
where: | |
componentType1 = ComponentType.getTypeFor(ComponentA.class) | |
componentType2 = ComponentType.getTypeFor(ComponentB.class) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment