| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package cz.davza.studentadventure;
- import cz.davza.studentadventure.base.CommandBase;
- import cz.davza.studentadventure.interfaces.IOutputSink;
- import cz.davza.studentadventure.managers.CommandManager;
- import cz.davza.studentadventure.objects.*;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- /**
- * Shared test utilities.
- */
- public class TestHelpers {
- /** Captures all writeLine calls for assertion. */
- public static class CapturingOutputSink implements IOutputSink {
- private final StringBuilder buffer = new StringBuilder();
- @Override public void writeLine(String line) { buffer.append(line).append("\n"); }
- @Override public void buffer(String text) {}
- @Override public void flush() {}
- public String getOutput() { return buffer.toString(); }
- public void clear() { buffer.setLength(0); }
- public boolean contains(String text) { return buffer.toString().contains(text); }
- }
- /** Creates a fully wired GameData starting in the bedroom. */
- public static GameData buildGameData() {
- Player player = new Player("TestStudent", 20);
- GameMap map = new GameMap();
- GameTime time = new GameTime();
- return new GameData(player, map, time);
- }
- /**
- * Parses and executes a command string against the given context,
- * bypassing the allowed-commands check so commands can be tested
- * in isolation regardless of which room the player is in.
- */
- public static CommandResult execute(String input, GameData context) {
- // Build an unrestricted allowed list containing everything
- List<String> allAllowed = List.of(
- "study","sleep","walk","pickup","drop","consume","interact",
- "steal","buy","takeexam","inventory","stats","time","describe",
- "help","availablecommands","studentid"
- );
- CommandBase cmd = CommandManager.parseCommand(input, allAllowed);
- if (cmd == null) throw new IllegalArgumentException("Unknown command: " + input);
- return cmd.Execute(context);
- }
- }
|