You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.0 KiB

  1. package playground;
  2. import static org.junit.Assert.assertTrue;
  3. import java.awt.Color;
  4. import org.junit.jupiter.api.AfterAll;
  5. import org.junit.jupiter.api.BeforeAll;
  6. import org.junit.jupiter.api.Test;
  7. import gameobjects.EgoObject;
  8. import gameobjects.GameObject;
  9. import gameobjects.RectObject;
  10. /**
  11. * Tests {@link SpaceInvadersLevel} for
  12. * <ol>
  13. * <li>calcEnemySpeedX() returns the same value as constant SpaceInvadersLevel.ENEMYSPEEDX
  14. * <li>calcEnemySpeedY() returns the same value as constant SpaceInvadersLevel.ENEMYSPEEDY
  15. * <li>calcNrEnemies() returns the same value as constant SpaceInvadersLevel.NR_ENEMIES
  16. * <li>actionIfEnemyIsHit() adds 200 points to score
  17. * <li>actionIfEgoObjectIsHit() reduces number of lives (egoLives)
  18. * </ol>
  19. * @author jkonert
  20. *
  21. */
  22. class SpaceInvadersLevelTest {
  23. private static SpaceInvadersLevel myLevel;
  24. @BeforeAll
  25. static void setUpBeforeClass() throws Exception {
  26. myLevel = new SpaceInvadersLevel();
  27. SpaceInvadersLevel.setGlobalFlag("egoLives", 5);
  28. SpaceInvadersLevel.setGlobalFlag("points", 500);
  29. SpaceInvadersLevel.setGlobalFlag("highscore", 5000);
  30. }
  31. @AfterAll
  32. static void tearDownAfterClass() throws Exception {
  33. // nothing
  34. }
  35. @Test
  36. void testCalcEnemySpeedX() {
  37. assertTrue("EnemySpeedX is as in SpaceInvadersLevel defined", myLevel.calcEnemySpeedX() == SpaceInvadersLevel.ENEMYSPEEDX);
  38. }
  39. @Test
  40. void testCalcEnemySpeedY() {
  41. assertTrue("EnemySpeedY is as in SpaceInvadersLevel defined", myLevel.calcEnemySpeedY() == SpaceInvadersLevel.ENEMYSPEEDY);
  42. }
  43. @Test
  44. void testCalcNrEnemies() {
  45. assertTrue("NrOfEnemies is as in SpaceInvadersLevel defined", myLevel.calcNrEnemies() == SpaceInvadersLevel.NR_ENEMIES);
  46. }
  47. @Test
  48. void testActionIfEnemyIsHitPointsUp() {
  49. Integer numPointsBefore = (Integer)Playground.getGlobalFlag("points");
  50. GameObject dummyShot = new RectObject("shot1", myLevel, 0,0,0,0, 12, 12, Color.WHITE);
  51. GameObject dummyEnemy = new RectObject("ego1", myLevel, 0,0,0,0, 12, 12, Color.BLACK);
  52. myLevel.addObject(dummyShot);
  53. myLevel.addObject(dummyEnemy);
  54. myLevel.actionIfEnemyIsHit(dummyEnemy, dummyShot);; // this is the call under test
  55. Integer numPointsAfter = (Integer)Playground.getGlobalFlag("points"); // changed?
  56. assertTrue("numPoints is up +200 after EnemyIsHit", numPointsAfter == numPointsBefore + 200); // points are set +200 , check.
  57. }
  58. @Test
  59. void testActionIfEgoObjectIsHitLivesDown() {
  60. Integer numLivesBefore = (Integer)Playground.getGlobalFlag("egoLives");
  61. GameObject dummyShot = new RectObject("shot1", myLevel, 0,0,0,0, 12, 12, Color.RED);
  62. GameObject dummyEgo = new EgoObject("ego1", myLevel, 0,0,0,0, 5);
  63. myLevel.addObject(dummyShot);
  64. myLevel.actionIfEgoObjectIsHit(dummyShot, dummyEgo); // this is the call under test
  65. Integer numLivesAfter = (Integer)Playground.getGlobalFlag("egoLives"); // changed?
  66. assertTrue("numLives is reduced by one ifEgoIsHit", numLivesAfter == numLivesBefore - 1); // lives is reduced by one
  67. }
  68. }