diff --git a/src/mudlib/commands/things.py b/src/mudlib/commands/things.py index 6b3dbe8..7f6a2f0 100644 --- a/src/mudlib/commands/things.py +++ b/src/mudlib/commands/things.py @@ -49,7 +49,7 @@ async def cmd_get(player: Player, args: str) -> None: return if not player.can_accept(thing): - await player.send(f"You can't pick that up.\r\n") + await player.send("You can't pick that up.\r\n") return thing.move_to(player) @@ -76,5 +76,21 @@ async def cmd_drop(player: Player, args: str) -> None: await player.send(f"You drop {thing.name}.\r\n") +async def cmd_inventory(player: Player, args: str) -> None: + """List items in the player's inventory.""" + things = [obj for obj in player.contents if isinstance(obj, Thing)] + + if not things: + await player.send("You aren't carrying anything.\r\n") + return + + lines = ["You are carrying:\r\n"] + for thing in things: + lines.append(f" {thing.name}\r\n") + + await player.send("".join(lines)) + + register(CommandDefinition("get", cmd_get, aliases=["take", "pick"])) register(CommandDefinition("drop", cmd_drop)) +register(CommandDefinition("inventory", cmd_inventory, aliases=["i"])) diff --git a/tests/test_inventory.py b/tests/test_inventory.py new file mode 100644 index 0000000..fcce885 --- /dev/null +++ b/tests/test_inventory.py @@ -0,0 +1,105 @@ +"""Tests for inventory command.""" + +from unittest.mock import AsyncMock, MagicMock + +import pytest + +from mudlib import commands +from mudlib.commands import things # noqa: F401 +from mudlib.player import Player +from mudlib.thing import Thing +from mudlib.zone import Zone + + +@pytest.fixture +def test_zone(): + """Create a test zone.""" + return Zone( + name="testzone", + width=10, + height=10, + toroidal=True, + terrain=[["." for _ in range(10)] for _ in range(10)], + ) + + +@pytest.fixture +def mock_writer(): + """Create a mock writer.""" + return MagicMock(write=MagicMock(), drain=AsyncMock()) + + +@pytest.fixture +def mock_reader(): + """Create a mock reader.""" + return MagicMock() + + +@pytest.fixture +def player(mock_reader, mock_writer, test_zone): + """Create a test player.""" + return Player( + name="TestPlayer", + x=5, + y=5, + reader=mock_reader, + writer=mock_writer, + location=test_zone, + ) + + +@pytest.mark.asyncio +async def test_inventory_empty(player, mock_writer): + """Test inventory with no items.""" + from mudlib.commands.things import cmd_inventory + + await cmd_inventory(player, "") + + output = mock_writer.write.call_args_list[-1][0][0] + assert "You aren't carrying anything." in output + + +@pytest.mark.asyncio +async def test_inventory_one_item(player, mock_writer): + """Test inventory with one item.""" + from mudlib.commands.things import cmd_inventory + + Thing(name="rock", location=player) + + await cmd_inventory(player, "") + + output = mock_writer.write.call_args_list[-1][0][0] + assert "You are carrying:" in output + assert "rock" in output + + +@pytest.mark.asyncio +async def test_inventory_multiple_items(player, mock_writer): + """Test inventory with multiple items.""" + from mudlib.commands.things import cmd_inventory + + Thing(name="rock", location=player) + Thing(name="sword", location=player) + Thing(name="shield", location=player) + + await cmd_inventory(player, "") + + output = mock_writer.write.call_args_list[-1][0][0] + assert "You are carrying:" in output + assert "rock" in output + assert "sword" in output + assert "shield" in output + + +def test_inventory_command_registered(): + """Test that inventory command is registered.""" + assert "inventory" in commands._registry + cmd = commands._registry["inventory"] + assert cmd.name == "inventory" + + +def test_inventory_alias_registered(): + """Test that 'i' alias is registered.""" + assert "i" in commands._registry + cmd = commands._registry["i"] + assert cmd.name == "inventory"