From 080db7092c5d39acc9e35059a10c7009e530614e Mon Sep 17 00:00:00 2001 From: Griatch Date: Fri, 5 Aug 2022 16:02:53 +0200 Subject: [PATCH] Finish evadventure menu unit tests --- .../contrib/tutorials/evadventure/commands.py | 14 +-- evennia/contrib/tutorials/evadventure/npcs.py | 4 +- .../evadventure/tests/test_commands.py | 98 ++++++++++++++++++- 3 files changed, 107 insertions(+), 9 deletions(-) diff --git a/evennia/contrib/tutorials/evadventure/commands.py b/evennia/contrib/tutorials/evadventure/commands.py index 3e7f8a28a6..95cb71ca5e 100644 --- a/evennia/contrib/tutorials/evadventure/commands.py +++ b/evennia/contrib/tutorials/evadventure/commands.py @@ -159,11 +159,11 @@ class CmdWieldOrWear(EvAdventureCommand): current = self.caller.equipment.slots[use_slot] if current == item: - self.caller.msg(f"You are already using {item.key} here.") + self.caller.msg(f"You are already using {item.key}.") return # move it to the right slot based on the type of object - self.caller.equipment.use(item) + self.caller.equipment.move(item) # inform the user of the change (and potential swap) if current: @@ -381,14 +381,16 @@ class CmdGive(EvAdventureCommand): if self.coins: current_coins = caller.coins - if caller.coins < current_coins: - caller.msg("You only have |y{current_coins}|n to give.") + if self.coins > current_coins: + caller.msg(f"You only have |y{current_coins}|n coins to give.") return # do transaction caller.coins -= self.coins receiver.coins += self.coins caller.location.msg_contents( - f"$You() $conj(give) $You(receiver.key) {self.coins} coins." + f"$You() $conj(give) $You({receiver.key}) {self.coins} coins.", + from_obj=caller, + mapping={receiver.key: receiver}, ) return @@ -432,7 +434,7 @@ class CmdTalk(EvAdventureCommand): key = "talk" def func(self): - target = self.search(self.args) + target = self.caller.search(self.args) if not target: return diff --git a/evennia/contrib/tutorials/evadventure/npcs.py b/evennia/contrib/tutorials/evadventure/npcs.py index 02f777a316..2b717d5287 100644 --- a/evennia/contrib/tutorials/evadventure/npcs.py +++ b/evennia/contrib/tutorials/evadventure/npcs.py @@ -105,8 +105,8 @@ class EvAdventureTalkativeNPC(EvAdventureNPC): """ - menudata = AttributeProperty(None, autocreate=False) - menu_kwargs = AttributeProperty(None, autocreate=False) + menudata = AttributeProperty(dict(), autocreate=False) + menu_kwargs = AttributeProperty(dict(), autocreate=False) # text shown when greeting at the start of a conversation. If this is an # iterable, a random reply will be chosen by the menu hi_text = AttributeProperty("Hi!", autocreate=False) diff --git a/evennia/contrib/tutorials/evadventure/tests/test_commands.py b/evennia/contrib/tutorials/evadventure/tests/test_commands.py index 807487c022..1c06a4124e 100644 --- a/evennia/contrib/tutorials/evadventure/tests/test_commands.py +++ b/evennia/contrib/tutorials/evadventure/tests/test_commands.py @@ -3,11 +3,15 @@ Test the EvAdventure commands. """ -from unittest.mock import MagicMock, patch +from unittest.mock import call, patch +from anything import Something +from evennia.utils.create import create_object from evennia.utils.test_resources import BaseEvenniaCommandTest from .. import commands +from ..characters import EvAdventureCharacter +from ..npcs import EvAdventureMob, EvAdventureShopKeeper from .mixins import EvAdventureMixin @@ -28,3 +32,95 @@ Backpack is empty. You use 0/11 equipment slots. """.strip(), ) + + @patch("evennia.contrib.tutorials.evadventure.commands.join_combat") + def test_attack(self, mock_join_combat): + self.location.allow_combat = True + + target = create_object(EvAdventureMob, key="Ogre", location=self.location) + + self.call(commands.CmdAttackTurnBased(), "ogre", "") + + mock_join_combat.assert_called_with(self.char1, target, session=Something) + + target.delete() + + def test_wield_or_wear(self): + self.char1.equipment.add(self.helmet) + self.char1.equipment.add(self.weapon) + self.shield.location = self.location + + self.call(commands.CmdWieldOrWear(), "shield", "Could not find 'shield'") + self.call(commands.CmdWieldOrWear(), "helmet", "You put helmet on your head.") + self.call( + commands.CmdWieldOrWear(), + "weapon", + "You hold weapon in your strongest hand, ready for action.", + ) + self.call(commands.CmdWieldOrWear(), "helmet", "You are already using helmet.") + + def test_remove(self): + self.char1.equipment.add(self.helmet) + self.call(commands.CmdWieldOrWear(), "helmet", "You put helmet on your head.") + + self.call(commands.CmdRemove(), "helmet", "You stash helmet in your backpack.") + + def test_give__coins(self): + recipient = create_object(EvAdventureCharacter, key="Friend", location=self.location) + recipient.coins = 0 + self.char1.coins = 100 + + self.call(commands.CmdGive(), "40 coins to friend", "You give Friend 40 coins.") + self.assertEqual(self.char1.coins, 60) + self.assertEqual(recipient.coins, 40) + + self.call(commands.CmdGive(), "10 to friend", "You give Friend 10 coins.") + self.assertEqual(self.char1.coins, 50) + self.assertEqual(recipient.coins, 50) + + self.call(commands.CmdGive(), "60 to friend", "You only have 50 coins to give.") + + recipient.delete() + + @patch("evennia.contrib.tutorials.evadventure.commands.EvMenu") + def test_give__item(self, mock_EvMenu): + + self.char1.equipment.add(self.helmet) + recipient = create_object(EvAdventureCharacter, key="Friend", location=self.location) + + self.call(commands.CmdGive(), "helmet to friend", "") + + mock_EvMenu.assert_has_calls( + ( + call( + recipient, + {"node_receive": Something, "node_end": Something}, + item=self.helmet, + giver=self.char1, + ), + call( + self.char1, + {"node_give": Something, "node_end": Something}, + item=self.helmet, + receiver=recipient, + ), + ) + ) + + recipient.delete() + + @patch("evennia.contrib.tutorials.evadventure.npcs.EvMenu") + def test_talk(self, mock_EvMenu): + npc = create_object(EvAdventureShopKeeper, key="shopkeep", location=self.location) + + npc.menudata = {"foo": None, "bar": None} + + self.call(commands.CmdTalk(), "shopkeep", "") + + mock_EvMenu.assert_called_with( + self.char1, + {"foo": None, "bar": None}, + startnode="node_start", + session=None, + npc=npc, + )