From adb370b1d31f1c68850ae6fe4b597e2c0b7b59fd Mon Sep 17 00:00:00 2001 From: Griatch Date: Mon, 22 Mar 2021 20:18:27 +0100 Subject: [PATCH] Add tests for verb conjugation --- evennia/utils/funcparser.py | 14 ++++++++--- evennia/utils/tests/test_funcparser.py | 27 ++++++++++++++++++++- evennia/utils/verb_conjugation/conjugate.py | 10 ++++---- evennia/utils/verb_conjugation/tests.py | 4 +++ 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/evennia/utils/funcparser.py b/evennia/utils/funcparser.py index e1f142f0d6..7ead30cab6 100644 --- a/evennia/utils/funcparser.py +++ b/evennia/utils/funcparser.py @@ -40,9 +40,11 @@ The `FuncParser` also accepts a direct dict mapping of `{'name': callable, ...}` """ +import re import dataclasses import inspect import random +from functools import partial from django.conf import settings from ast import literal_eval from simpleeval import simple_eval @@ -50,6 +52,7 @@ from evennia.utils import logger from evennia.utils.utils import ( make_iter, callables_from_module, variable_from_module, pad, crop, justify) from evennia.utils import search +from evennia.utils.verb_conjugation.conjugate import verb_actor_stance_components _CLIENT_DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH _MAX_NESTING = 20 @@ -520,8 +523,9 @@ class FuncParser: # return explicit return return exec_return - # add the last bit to the finished string and return + # add the last bit to the finished string fullstr += infuncstr + return fullstr def parse_to_any(self, string, raise_errors=False, **reserved_kwargs): @@ -1116,10 +1120,13 @@ def funcparser_callable_conjugate(*args, you_obj=None, you_target=None, **kwargs Others will see "With a grin, CharName jumps." """ - + if not args: + return '' if not (you_obj and you_target): raise ParsingError("No you_obj/target supplied to $conj callable") - return '' + + you_str, them_str = verb_actor_stance_components(args[0]) + return you_str if you_obj == you_target else them_str # these are made available as callables by adding 'evennia.utils.funcparser' as @@ -1163,4 +1170,5 @@ FUNCPARSER_CALLABLES = { # referencing "you": funcparser_callable_you, "You": funcparser_callable_You, + "conj": funcparser_callable_conjugate, } diff --git a/evennia/utils/tests/test_funcparser.py b/evennia/utils/tests/test_funcparser.py index f79f4d97b4..62f1a8f177 100644 --- a/evennia/utils/tests/test_funcparser.py +++ b/evennia/utils/tests/test_funcparser.py @@ -267,6 +267,14 @@ class TestFuncParser(TestCase): ret = parser.parse("This is a $foo(foo=moo) string", foo="bar") self.assertEqual("This is a _test(test=foo, foo=bar) string", ret) +class _DummyObj: + def __init__(self, name): + self.name = name + + def get_display_name(self, looker=None): + return self.name + + class TestDefaultCallables(TestCase): """ Test default callables. @@ -276,9 +284,11 @@ class TestDefaultCallables(TestCase): def setUp(self): from django.conf import settings self.parser = funcparser.FuncParser(settings.INLINEFUNC_MODULES) + self.obj1 = _DummyObj("Char1") + self.obj2 = _DummyObj("Char2") @parameterized.expand([ - ("Test py1 $py('')", "Test py1 ''"), + ("Test py1 $eval('')", "Test py1 "), ]) def test_callable(self, string, expected): """ @@ -288,6 +298,21 @@ class TestDefaultCallables(TestCase): ret = self.parser.parse(string, raise_errors=True) self.assertEqual(expected, ret) + @parameterized.expand([ + ("$You() $conj(smile) at him.", "You smile at him.", "Char1 smiles at him."), + ]) + def test_conjugate(self, string, expected_you, expected_them): + """ + Test callables with various input strings + + """ + ret = self.parser.parse(string, you_obj=self.obj1, you_target=self.obj1, + raise_errors=True) + self.assertEqual(expected_you, ret) + ret = self.parser.parse(string, you_obj=self.obj1, you_target=self.obj2, + raise_errors=True) + self.assertEqual(expected_them, ret) + class TestOldDefaultCallables(TestCase): """ diff --git a/evennia/utils/verb_conjugation/conjugate.py b/evennia/utils/verb_conjugation/conjugate.py index f24e6ac8fa..48cba4f184 100644 --- a/evennia/utils/verb_conjugation/conjugate.py +++ b/evennia/utils/verb_conjugation/conjugate.py @@ -322,7 +322,6 @@ def verb_is_present_participle(verb): return tense == "present participle" - def verb_is_past(verb, person="", negated=False): """ Checks whether the verb is in the past tense. @@ -380,8 +379,9 @@ def verb_actor_stance_components(verb): if "participle" in tense or "plural" in tense: return (verb, verb) if tense == "infinitive" or "present" in tense: - return (verb_present(verb, person="2"), - verb_present(verb, person="3")) + you_str = verb_present(verb, person="2") or verb + them_str = verb_present(verb, person="3") or verb + "s" else: - return (verb_past(verb, person="2"), - verb_past(verb, person="3")) + you_str = verb_past(verb, person="2") or verb + them_str = verb_past(verb, person="3") or verb + "s" + return (you_str, them_str) diff --git a/evennia/utils/verb_conjugation/tests.py b/evennia/utils/verb_conjugation/tests.py index 8a80fa260c..abc0225e2a 100644 --- a/evennia/utils/verb_conjugation/tests.py +++ b/evennia/utils/verb_conjugation/tests.py @@ -228,6 +228,10 @@ class TestVerbConjugate(TestCase): ("doing", ("doing", "doing")), ("are", ("are", "is")), ("had", ("had", "had")), + ("grin", ("grin", "grins")), + ("smile", ("smile", "smiles")), + ("vex", ("vex", "vexes")), + ("thrust", ("thrust", "thrusts")), ]) def test_verb_actor_stance_components(self, verb, expected): """