TestHelpers.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package cz.davza.studentadventure;
  2. import cz.davza.studentadventure.base.CommandBase;
  3. import cz.davza.studentadventure.interfaces.IOutputSink;
  4. import cz.davza.studentadventure.managers.CommandManager;
  5. import cz.davza.studentadventure.objects.*;
  6. import java.util.Arrays;
  7. import java.util.Collections;
  8. import java.util.List;
  9. /**
  10. * Shared test utilities.
  11. */
  12. public class TestHelpers {
  13. /** Captures all writeLine calls for assertion. */
  14. public static class CapturingOutputSink implements IOutputSink {
  15. private final StringBuilder buffer = new StringBuilder();
  16. @Override public void writeLine(String line) { buffer.append(line).append("\n"); }
  17. @Override public void buffer(String text) {}
  18. @Override public void flush() {}
  19. public String getOutput() { return buffer.toString(); }
  20. public void clear() { buffer.setLength(0); }
  21. public boolean contains(String text) { return buffer.toString().contains(text); }
  22. }
  23. /** Creates a fully wired GameData starting in the bedroom. */
  24. public static GameData buildGameData() {
  25. Player player = new Player("TestStudent", 20);
  26. GameMap map = new GameMap();
  27. GameTime time = new GameTime();
  28. return new GameData(player, map, time);
  29. }
  30. /**
  31. * Parses and executes a command string against the given context,
  32. * bypassing the allowed-commands check so commands can be tested
  33. * in isolation regardless of which room the player is in.
  34. */
  35. public static CommandResult execute(String input, GameData context) {
  36. // Build an unrestricted allowed list containing everything
  37. List<String> allAllowed = List.of(
  38. "study","sleep","walk","pickup","drop","consume","interact",
  39. "steal","buy","takeexam","inventory","stats","time","describe",
  40. "help","availablecommands","studentid"
  41. );
  42. CommandBase cmd = CommandManager.parseCommand(input, allAllowed);
  43. if (cmd == null) throw new IllegalArgumentException("Unknown command: " + input);
  44. return cmd.Execute(context);
  45. }
  46. }