Skip to content

Commit

Permalink
Make #CALL raise an error if the return value is not a string
Browse files Browse the repository at this point in the history
  • Loading branch information
skoolkid committed Jan 18, 2025
1 parent 594cd97 commit a83d3e7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion skoolkit/skoolmacro.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2012-2024 Richard Dymond (rjdymond@gmail.com)
# Copyright 2012-2025 Richard Dymond (rjdymond@gmail.com)
#
# This file is part of SkoolKit.
#
Expand Down Expand Up @@ -780,6 +780,8 @@ def parse_call(writer, text, index, *cwd):
raise MacroParsingError(f"{ftype} call {text[index + 1:p_end]} failed: {e}")
if retval is None:
retval = ''
if not isinstance(retval, str):
raise MacroParsingError(f"Return value from {text[index + 1:p_end]} is not a string")
return end, retval

def parse_chr(writer, text, index, *cwd):
Expand Down
5 changes: 5 additions & 0 deletions tests/macrotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ def test_macro_call_with_nonexistent_method(self):
self.assertEqual(writer.expand(macro), '')
self.assertEqual(self.err.getvalue().split('\n')[0], f'WARNING: Unknown method name in #CALL macro: {method_name}')

def test_macro_call_with_return_value_that_is_not_a_string(self):
writer = self._get_writer()
writer.test_call = self._test_call_retval_not_string
self._assert_error(writer, '#CALL(test_call())', 'Return value from test_call() is not a string', ERROR_PREFIX.format('CALL'))

def test_macro_call_invalid(self):
writer = self._get_writer()
writer.test_call = self._test_call
Expand Down
3 changes: 3 additions & 0 deletions tests/test_skoolasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def _test_call_no_retval(self, *args):
def _test_call_no_args(self):
return 'OK'

def _test_call_retval_not_string(self):
return 1

def test_macros_are_expanded(self):
skool = """
@start
Expand Down
3 changes: 3 additions & 0 deletions tests/test_skoolhtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,9 @@ def _test_call_no_retval(self, *args):
def _test_call_no_args(self, cwd):
return 'OK'

def _test_call_retval_not_string(self, cwd):
return 1

def _unsupported_macro(self, *args):
raise UnsupportedMacroError()

Expand Down

0 comments on commit a83d3e7

Please sign in to comment.