ExamRoomTest.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package cz.davza.studentadventure.rooms;
  2. import cz.davza.studentadventure.Items.Book;
  3. import cz.davza.studentadventure.TestHelpers;
  4. import cz.davza.studentadventure.enums.CommandState;
  5. import cz.davza.studentadventure.objects.CommandResult;
  6. import cz.davza.studentadventure.objects.GameData;
  7. import org.junit.jupiter.api.BeforeEach;
  8. import org.junit.jupiter.api.Test;
  9. import static org.junit.jupiter.api.Assertions.*;
  10. class ExamRoomTest {
  11. private GameData context;
  12. private ExamRoomRoom examRoom;
  13. @BeforeEach
  14. void setUp() {
  15. context = TestHelpers.buildGameData();
  16. // Navigate to exam room entrance: bedroom → hallway → classroom → examroom
  17. context.getPlayer().getInventory().Add(new Book(), 1);
  18. context.getMap().setNextRoom(context.getMap().getCurrentRoom().getExit("hallway"));
  19. context.getMap().commitRoomChange();
  20. context.getMap().setNextRoom(context.getMap().getCurrentRoom().getExit("classroom"));
  21. context.getMap().commitRoomChange();
  22. examRoom = (ExamRoomRoom) context.getMap().getCurrentRoom().getExit("examroom");
  23. }
  24. @Test
  25. void onEnterReturnsActionPending() {
  26. CommandResult result = examRoom.onEnter(context);
  27. assertEquals(CommandState.ACTION_PENDING, result.getResultState());
  28. }
  29. @Test
  30. void onEnterSetsExamRoomAsHandler() {
  31. CommandResult result = examRoom.onEnter(context);
  32. assertEquals(examRoom, result.getNewCommandHandler());
  33. }
  34. @Test
  35. void correctIdAllowsEntry() {
  36. examRoom.onEnter(context);
  37. String correctId = context.getPlayer().getId();
  38. CommandResult result = examRoom.handleCommand(correctId, context);
  39. assertEquals(CommandState.ACTION_RESUMED, result.getResultState());
  40. }
  41. @Test
  42. void wrongIdReturnsFailed() {
  43. examRoom.onEnter(context);
  44. CommandResult result = examRoom.handleCommand("wrongid999", context);
  45. assertEquals(CommandState.FAILED, result.getResultState());
  46. }
  47. @Test
  48. void leaveCommandCancelsEntry() {
  49. examRoom.onEnter(context);
  50. CommandResult result = examRoom.handleCommand("leave", context);
  51. assertEquals(CommandState.ACTION_RESUMED, result.getResultState());
  52. }
  53. @Test
  54. void leaveCommandCancelsRoomChange() {
  55. context.getMap().setNextRoom(examRoom);
  56. examRoom.onEnter(context);
  57. examRoom.handleCommand("leave", context);
  58. assertNull(context.getMap().getNextRoom());
  59. }
  60. @Test
  61. void idCheckIsCaseInsensitive() {
  62. examRoom.onEnter(context);
  63. String id = context.getPlayer().getId();
  64. CommandResult result = examRoom.handleCommand(id.toUpperCase(), context);
  65. assertEquals(CommandState.ACTION_RESUMED, result.getResultState());
  66. }
  67. @Test
  68. void examRoomHasTakeExamCommand() {
  69. assertTrue(examRoom.getAllowedCommands().contains("takeexam"));
  70. }
  71. }