Merge pull request #3359 from InspectorCaracal/gendersub-patch

Fix gendersub contrib's gender sourcing
This commit is contained in:
Griatch 2023-12-10 17:49:39 +01:00 committed by GitHub
commit 5cd58f4ef8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 5 deletions

View file

@ -101,7 +101,7 @@ class GenderCharacter(DefaultCharacter):
super().at_object_creation() super().at_object_creation()
self.db.gender = "ambiguous" self.db.gender = "ambiguous"
def _get_pronoun(self, regex_match): def _get_pronoun(self, regex_match, source=None):
""" """
Get pronoun from the pronoun marker in the text. This is used as Get pronoun from the pronoun marker in the text. This is used as
the callable for the re.sub function. the callable for the re.sub function.
@ -116,8 +116,10 @@ class GenderCharacter(DefaultCharacter):
- `|a`, `|A`: Absolute Possessive form: his, hers, its, His, Hers, Its, Theirs - `|a`, `|A`: Absolute Possessive form: his, hers, its, His, Hers, Its, Theirs
""" """
if not source:
source = self
typ = regex_match.group()[1] # "s", "O" etc typ = regex_match.group()[1] # "s", "O" etc
gender = self.attributes.get("gender", default="ambiguous") gender = source.attributes.get("gender", default="ambiguous")
gender = gender if gender in ("male", "female", "neutral") else "ambiguous" gender = gender if gender in ("male", "female", "neutral") else "ambiguous"
pronoun = _GENDER_PRONOUN_MAP[gender][typ.lower()] pronoun = _GENDER_PRONOUN_MAP[gender][typ.lower()]
return pronoun.capitalize() if typ.isupper() else pronoun return pronoun.capitalize() if typ.isupper() else pronoun
@ -149,11 +151,17 @@ class GenderCharacter(DefaultCharacter):
try: try:
if text and isinstance(text, tuple): if text and isinstance(text, tuple):
text = (_RE_GENDER_PRONOUN.sub(self._get_pronoun, text[0]), *text[1:]) text = (
_RE_GENDER_PRONOUN.sub(
lambda x: self._get_pronoun(x, source=from_obj), text[0]
),
*text[1:],
)
else: else:
text = _RE_GENDER_PRONOUN.sub(self._get_pronoun, text) text = _RE_GENDER_PRONOUN.sub(lambda x: self._get_pronoun(x, source=from_obj), text)
except TypeError: except TypeError:
pass pass
except Exception as e: except Exception as e:
logger.log_trace(e) logger.log_trace(e)
super().msg(text, from_obj=from_obj, session=session, **kwargs) super().msg(text, from_obj=from_obj, session=session, **kwargs)

View file

@ -28,5 +28,38 @@ class TestGenderSub(BaseEvenniaCommandTest):
"evennia.contrib.game_systems.gendersub.gendersub.DefaultCharacter.msg" "evennia.contrib.game_systems.gendersub.gendersub.DefaultCharacter.msg"
) as mock_msg: ) as mock_msg:
char.db.gender = "female" char.db.gender = "female"
char.msg("Test |p gender") char.msg(txt)
mock_msg.assert_called_with("Test her gender", from_obj=None, session=None) mock_msg.assert_called_with("Test her gender", from_obj=None, session=None)
def test_gendering_others(self):
"""ensure characters see the gender of the sender, not themselves"""
fem = create_object(
gendersub.GenderCharacter,
key="Gendered",
location=self.room2,
attributes=[("gender", "female")],
)
masc = create_object(
gendersub.GenderCharacter,
key="Gendered",
location=self.room2,
attributes=[("gender", "male")],
)
txt = "Test |p gender"
with patch(
"evennia.contrib.game_systems.gendersub.gendersub.DefaultCharacter.msg"
) as mock_msg:
fem.msg(txt, from_obj=masc)
self.assertIn("Test his gender", mock_msg.call_args.args)
masc.msg(txt, from_obj=fem)
self.assertIn("Test her gender", mock_msg.call_args.args)
def test_ungendered_source(self):
char = create_object(gendersub.GenderCharacter, key="Gendered", location=self.room1)
txt = "Test |p gender"
with patch(
"evennia.contrib.game_systems.gendersub.gendersub.DefaultCharacter.msg"
) as mock_msg:
char.msg(txt, from_obj=self.char1)
self.assertIn("Test their gender", mock_msg.call_args.args)