Fix nesting count - with tests. Resolve #2926
This commit is contained in:
parent
bd297876ca
commit
a2289c46f8
2 changed files with 24 additions and 12 deletions
|
|
@ -352,7 +352,7 @@ class FuncParser:
|
||||||
|
|
||||||
if curr_func:
|
if curr_func:
|
||||||
# we are starting a nested funcdef
|
# we are starting a nested funcdef
|
||||||
if len(callstack) > _MAX_NESTING:
|
if len(callstack) >= _MAX_NESTING - 1:
|
||||||
# stack full - ignore this function
|
# stack full - ignore this function
|
||||||
if raise_errors:
|
if raise_errors:
|
||||||
raise ParsingError(
|
raise ParsingError(
|
||||||
|
|
|
||||||
|
|
@ -252,25 +252,37 @@ class TestFuncParser(TestCase):
|
||||||
with self.assertRaises(funcparser.ParsingError):
|
with self.assertRaises(funcparser.ParsingError):
|
||||||
self.parser.parse(unparseable, raise_errors=True)
|
self.parser.parse(unparseable, raise_errors=True)
|
||||||
|
|
||||||
@patch("evennia.utils.funcparser._MAX_NESTING", 2)
|
@parameterized.expand(
|
||||||
def test_parse_max_nesting(self):
|
[
|
||||||
|
# max_nest, cause error for 4 nested funcs?
|
||||||
|
(0, False),
|
||||||
|
(1, False),
|
||||||
|
(2, False),
|
||||||
|
(3, False),
|
||||||
|
(4, True),
|
||||||
|
(5, True),
|
||||||
|
(6, True),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_parse_max_nesting(self, max_nest, ok):
|
||||||
"""
|
"""
|
||||||
Make sure it is an error if the max nesting value is reached.
|
Make sure it is an error if the max nesting value is reached. We test
|
||||||
|
four nested functions against differnt MAX_NESTING values.
|
||||||
|
|
||||||
TODO: Does this make sense? When it sees the first function, len(callstack)
|
TODO: Does this make sense? When it sees the first function, len(callstack)
|
||||||
is 0. It doesn't raise until the stack length is greater than the
|
is 0. It doesn't raise until the stack length is greater than the
|
||||||
_MAX_NESTING value, which means you can nest 4 values with a value of
|
_MAX_NESTING value, which means you can nest 4 values with a value of
|
||||||
2, as demonstrated by this test.
|
2, as demonstrated by this test.
|
||||||
"""
|
"""
|
||||||
string = "$add(1, $add(1, $add(1, $toint(42))))"
|
string = "$add(1, $add(1, $add(1, $eval(42))))"
|
||||||
ret = self.parser.parse(string)
|
|
||||||
|
|
||||||
# TODO: Does this return value actually make sense?
|
with patch("evennia.utils.funcparser._MAX_NESTING", max_nest):
|
||||||
# It removed the spaces from the calls.
|
if ok:
|
||||||
self.assertEqual("$add(1,$add(1,$add(1,$toint(42))))", ret)
|
ret = self.parser.parse(string, raise_errors=True)
|
||||||
|
self.assertEqual(ret, "45")
|
||||||
with self.assertRaises(funcparser.ParsingError):
|
else:
|
||||||
self.parser.parse(string, raise_errors=True)
|
with self.assertRaises(funcparser.ParsingError):
|
||||||
|
self.parser.parse(string, raise_errors=True)
|
||||||
|
|
||||||
def test_parse_underlying_exception(self):
|
def test_parse_underlying_exception(self):
|
||||||
string = "test $add(1, 1) $raise()"
|
string = "test $add(1, 1) $raise()"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue