changed module location, mocked out delay calls

This commit is contained in:
Tegiminis 2022-08-02 12:00:44 -07:00
parent a30e152405
commit 7dfbc12d7b
5 changed files with 35 additions and 6 deletions

View file

@ -294,7 +294,7 @@ class Mod:
self.perstack = perstack
class BuffHandler(object):
class BuffHandler:
ownerref = None
dbkey = "buffs"
@ -734,6 +734,8 @@ class BuffHandler(object):
buff: The buff to search for. This can be a string (the key) or a class reference (the buff type)
Returns a bool. If no buff and no key is specified, returns False."""
if not buff:
return False
if not (isinstance(buff, type) or isinstance(buff, str)):
raise TypeError

View file

@ -1,14 +1,16 @@
# in a module tests.py somewhere i your game dir
from unittest.mock import Mock, patch
"""
Tests for the buff system contrib
"""
from unittest.mock import Mock, call, patch
from evennia import DefaultObject, create_object
from evennia.utils import create
from evennia.utils.utils import lazy_property
from .samplebuffs import StatBuff
# the function we want to test
from .buff import BaseBuff, Mod, BuffHandler, BuffableProperty
from evennia.utils.test_resources import EvenniaTest
from evennia.contrib.rpg.buffs import buff
class _EmptyBuff(BaseBuff):
pass
@ -117,6 +119,7 @@ class TestBuffsAndHandler(EvenniaTest):
del self.testobj
super().tearDown()
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_addremove(self):
"""tests adding and removing buffs"""
# setup
@ -159,6 +162,7 @@ class TestBuffsAndHandler(EvenniaTest):
handler.clear()
self.assertFalse(self.testobj.buffs.all)
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_getters(self):
"""tests all built-in getters"""
# setup
@ -184,6 +188,7 @@ class TestBuffsAndHandler(EvenniaTest):
self.assertTrue("ttb" in handler.get_by_cachevalue("ttbcache"))
self.assertTrue("ttb" in handler.get_by_cachevalue("ttbcache", True))
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_details(self):
"""tests that buff details like name and flavor are correct"""
handler: BuffHandler = self.testobj.buffs
@ -192,6 +197,7 @@ class TestBuffsAndHandler(EvenniaTest):
self.assertEqual(handler.get("tmb").flavor, "modderbuff")
self.assertEqual(handler.get("ttb").name, "ttb")
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_modify(self):
"""tests to ensure that values are modified correctly, and stack across mods"""
# setup
@ -226,6 +232,7 @@ class TestBuffsAndHandler(EvenniaTest):
self.assertEqual(handler.check(_stat1, "stat1"), 30)
self.assertEqual(handler.check(_stat2, "stat2"), 20)
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_trigger(self):
"""tests to ensure triggers correctly fire"""
# setup
@ -242,6 +249,7 @@ class TestBuffsAndHandler(EvenniaTest):
self.assertTrue(self.testobj.db.triggertest1)
self.assertTrue(self.testobj.db.triggertest2)
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_context_conditional(self):
"""tests to ensure context is passed to buffs, and also tests conditionals"""
# setup
@ -269,6 +277,7 @@ class TestBuffsAndHandler(EvenniaTest):
self.assertEqual(self.testobj.db.att, self.obj2)
self.assertEqual(self.testobj.db.dmg, 5)
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_complex(self):
"""tests a complex mod (conditionals, multiple triggers/mods)"""
# setup
@ -310,11 +319,26 @@ class TestBuffsAndHandler(EvenniaTest):
handler.check(self.testobj.db.comtwo, "com2", context=self.testobj.db.comtext), 100
)
def test_timing(self):
@patch("evennia.contrib.rpg.buffs.buff.utils.delay")
def test_timing(self, mock_delay: Mock):
"""tests timing-related features, such as ticking and duration"""
# setup
handler: BuffHandler = self.testobj.buffs
mock_delay.side_effect = [None, handler.cleanup]
handler.add(_TestTimeBuff)
calls = [
call(
1,
buff.tick_buff,
handler=handler,
buffkey="ttib",
context={},
initial=False,
persistent=True,
),
call(5, handler.cleanup, persistent=True),
]
mock_delay.assert_has_calls(calls)
self.testobj.db.timetest, self.testobj.db.ticktest = 1, False
# test duration and ticking
_instance = handler.get("ttib")
@ -328,6 +352,7 @@ class TestBuffsAndHandler(EvenniaTest):
handler.cleanup()
self.assertFalse(handler.get("ttib"), None)
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_buffableproperty(self):
"""tests buffable properties"""
# setup
@ -336,6 +361,7 @@ class TestBuffsAndHandler(EvenniaTest):
self.testobj.buffs.remove("tmb")
self.assertEqual(self.testobj.stat1, 10)
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_stresstest(self):
"""tests large amounts of buffs, and related removal methods"""
# setup
@ -349,6 +375,7 @@ class TestBuffsAndHandler(EvenniaTest):
self.testobj.buffs.clear()
self.assertFalse(self.testobj.buffs.all)
@patch("evennia.contrib.rpg.buffs.buff.utils.delay", new=Mock())
def test_modgen(self):
"""test generating mods on the fly"""
# setup