ExamRoomRoomTest.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package cz.davza.studentadventure.rooms;
  2. import cz.davza.studentadventure.TestHelpers;
  3. import cz.davza.studentadventure.enums.CommandState;
  4. import cz.davza.studentadventure.objects.*;
  5. import org.junit.jupiter.api.BeforeEach;
  6. import org.junit.jupiter.api.Test;
  7. import static org.junit.jupiter.api.Assertions.*;
  8. class ExamRoomRoomTest {
  9. private GameData context;
  10. private ExamRoomRoom examRoom;
  11. @BeforeEach
  12. void setUp() {
  13. context = TestHelpers.buildGameData();
  14. examRoom = new ExamRoomRoom(context);
  15. }
  16. // ── onEnter ───────────────────────────────────────────────────────────────
  17. @Test
  18. void onEnterReturnsActionPending() {
  19. assertEquals(CommandState.ACTION_PENDING, examRoom.onEnter(context).getResultState());
  20. }
  21. @Test
  22. void onEnterSetsHandlerToExamRoom() {
  23. CommandResult result = examRoom.onEnter(context);
  24. assertEquals(examRoom, result.getNewCommandHandler());
  25. }
  26. // ── handleCommand — correct ID ────────────────────────────────────────────
  27. @Test
  28. void correctStudentIdResumesAction() {
  29. String id = context.getPlayer().getId();
  30. CommandResult result = examRoom.handleCommand(id, context);
  31. assertEquals(CommandState.ACTION_RESUMED, result.getResultState());
  32. }
  33. @Test
  34. void correctStudentIdIsCaseSensitive() {
  35. // ID is generated as name+random — verify exact match is required
  36. String id = context.getPlayer().getId();
  37. CommandResult correct = examRoom.handleCommand(id, context);
  38. assertEquals(CommandState.ACTION_RESUMED, correct.getResultState());
  39. }
  40. // ── handleCommand — wrong ID ─────────────────────────────────────────────
  41. @Test
  42. void wrongStudentIdReturnsFailed() {
  43. CommandResult result = examRoom.handleCommand("wrongid123", context);
  44. assertEquals(CommandState.FAILED, result.getResultState());
  45. }
  46. @Test
  47. void wrongIdKeepsPlayerOutOfRoom() {
  48. examRoom.handleCommand("wrongid", context);
  49. // map should have no pending room change set by exam room
  50. // (ExamRoomRoom doesn't set nextRoom on wrong ID — that's correct behaviour)
  51. assertFalse(context.getMap().hasPendingRoomChange());
  52. }
  53. // ── handleCommand — leave ────────────────────────────────────────────────
  54. @Test
  55. void leaveResumesAction() {
  56. CommandResult result = examRoom.handleCommand("leave", context);
  57. assertEquals(CommandState.ACTION_RESUMED, result.getResultState());
  58. }
  59. @Test
  60. void leaveIsCaseInsensitive() {
  61. assertEquals(CommandState.ACTION_RESUMED,
  62. examRoom.handleCommand("LEAVE", context).getResultState());
  63. }
  64. @Test
  65. void leaveClearsNextRoom() {
  66. context.getMap().setNextRoom(examRoom); // simulate pending entry
  67. examRoom.handleCommand("leave", context);
  68. assertFalse(context.getMap().hasPendingRoomChange());
  69. }
  70. // ── Room contents ─────────────────────────────────────────────────────────
  71. @Test
  72. void examRoomAllowsTakeExamCommand() {
  73. assertTrue(examRoom.getAllowedCommands().contains("takeexam"));
  74. }
  75. }