Add tests for verb conjugation
This commit is contained in:
parent
24a6d2cfab
commit
adb370b1d3
4 changed files with 46 additions and 9 deletions
|
|
@ -40,9 +40,11 @@ The `FuncParser` also accepts a direct dict mapping of `{'name': callable, ...}`
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
import re
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import inspect
|
import inspect
|
||||||
import random
|
import random
|
||||||
|
from functools import partial
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from ast import literal_eval
|
from ast import literal_eval
|
||||||
from simpleeval import simple_eval
|
from simpleeval import simple_eval
|
||||||
|
|
@ -50,6 +52,7 @@ from evennia.utils import logger
|
||||||
from evennia.utils.utils import (
|
from evennia.utils.utils import (
|
||||||
make_iter, callables_from_module, variable_from_module, pad, crop, justify)
|
make_iter, callables_from_module, variable_from_module, pad, crop, justify)
|
||||||
from evennia.utils import search
|
from evennia.utils import search
|
||||||
|
from evennia.utils.verb_conjugation.conjugate import verb_actor_stance_components
|
||||||
|
|
||||||
_CLIENT_DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH
|
_CLIENT_DEFAULT_WIDTH = settings.CLIENT_DEFAULT_WIDTH
|
||||||
_MAX_NESTING = 20
|
_MAX_NESTING = 20
|
||||||
|
|
@ -520,8 +523,9 @@ class FuncParser:
|
||||||
# return explicit return
|
# return explicit return
|
||||||
return exec_return
|
return exec_return
|
||||||
|
|
||||||
# add the last bit to the finished string and return
|
# add the last bit to the finished string
|
||||||
fullstr += infuncstr
|
fullstr += infuncstr
|
||||||
|
|
||||||
return fullstr
|
return fullstr
|
||||||
|
|
||||||
def parse_to_any(self, string, raise_errors=False, **reserved_kwargs):
|
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."
|
Others will see "With a grin, CharName jumps."
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if not args:
|
||||||
|
return ''
|
||||||
if not (you_obj and you_target):
|
if not (you_obj and you_target):
|
||||||
raise ParsingError("No you_obj/target supplied to $conj callable")
|
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
|
# these are made available as callables by adding 'evennia.utils.funcparser' as
|
||||||
|
|
@ -1163,4 +1170,5 @@ FUNCPARSER_CALLABLES = {
|
||||||
# referencing
|
# referencing
|
||||||
"you": funcparser_callable_you,
|
"you": funcparser_callable_you,
|
||||||
"You": funcparser_callable_You,
|
"You": funcparser_callable_You,
|
||||||
|
"conj": funcparser_callable_conjugate,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -267,6 +267,14 @@ class TestFuncParser(TestCase):
|
||||||
ret = parser.parse("This is a $foo(foo=moo) string", foo="bar")
|
ret = parser.parse("This is a $foo(foo=moo) string", foo="bar")
|
||||||
self.assertEqual("This is a _test(test=foo, foo=bar) string", ret)
|
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):
|
class TestDefaultCallables(TestCase):
|
||||||
"""
|
"""
|
||||||
Test default callables.
|
Test default callables.
|
||||||
|
|
@ -276,9 +284,11 @@ class TestDefaultCallables(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
self.parser = funcparser.FuncParser(settings.INLINEFUNC_MODULES)
|
self.parser = funcparser.FuncParser(settings.INLINEFUNC_MODULES)
|
||||||
|
self.obj1 = _DummyObj("Char1")
|
||||||
|
self.obj2 = _DummyObj("Char2")
|
||||||
|
|
||||||
@parameterized.expand([
|
@parameterized.expand([
|
||||||
("Test py1 $py('')", "Test py1 ''"),
|
("Test py1 $eval('')", "Test py1 "),
|
||||||
])
|
])
|
||||||
def test_callable(self, string, expected):
|
def test_callable(self, string, expected):
|
||||||
"""
|
"""
|
||||||
|
|
@ -288,6 +298,21 @@ class TestDefaultCallables(TestCase):
|
||||||
ret = self.parser.parse(string, raise_errors=True)
|
ret = self.parser.parse(string, raise_errors=True)
|
||||||
self.assertEqual(expected, ret)
|
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):
|
class TestOldDefaultCallables(TestCase):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -322,7 +322,6 @@ def verb_is_present_participle(verb):
|
||||||
return tense == "present participle"
|
return tense == "present participle"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def verb_is_past(verb, person="", negated=False):
|
def verb_is_past(verb, person="", negated=False):
|
||||||
"""
|
"""
|
||||||
Checks whether the verb is in the past tense.
|
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:
|
if "participle" in tense or "plural" in tense:
|
||||||
return (verb, verb)
|
return (verb, verb)
|
||||||
if tense == "infinitive" or "present" in tense:
|
if tense == "infinitive" or "present" in tense:
|
||||||
return (verb_present(verb, person="2"),
|
you_str = verb_present(verb, person="2") or verb
|
||||||
verb_present(verb, person="3"))
|
them_str = verb_present(verb, person="3") or verb + "s"
|
||||||
else:
|
else:
|
||||||
return (verb_past(verb, person="2"),
|
you_str = verb_past(verb, person="2") or verb
|
||||||
verb_past(verb, person="3"))
|
them_str = verb_past(verb, person="3") or verb + "s"
|
||||||
|
return (you_str, them_str)
|
||||||
|
|
|
||||||
|
|
@ -228,6 +228,10 @@ class TestVerbConjugate(TestCase):
|
||||||
("doing", ("doing", "doing")),
|
("doing", ("doing", "doing")),
|
||||||
("are", ("are", "is")),
|
("are", ("are", "is")),
|
||||||
("had", ("had", "had")),
|
("had", ("had", "had")),
|
||||||
|
("grin", ("grin", "grins")),
|
||||||
|
("smile", ("smile", "smiles")),
|
||||||
|
("vex", ("vex", "vexes")),
|
||||||
|
("thrust", ("thrust", "thrusts")),
|
||||||
])
|
])
|
||||||
def test_verb_actor_stance_components(self, verb, expected):
|
def test_verb_actor_stance_components(self, verb, expected):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue