From 017d90a4ab6fc10aecb6aac9b5478975a890afac Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Wed, 20 Nov 2024 21:07:56 +0100 Subject: [PATCH] feat: break long strings in `_PUZZLE` --- puzzle_generator/create_puzzle.py | 18 +++++++++++++++--- tests/test_create_puzzle.py | 16 +++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/puzzle_generator/create_puzzle.py b/puzzle_generator/create_puzzle.py index af5c7ad..2bda7dc 100644 --- a/puzzle_generator/create_puzzle.py +++ b/puzzle_generator/create_puzzle.py @@ -31,14 +31,26 @@ def _advertisement() -> str: """ +def _split_long_str(in_str: str, max_len: int) -> typing.List[str]: + if len(in_str) < max_len: + return [in_str] + return [in_str[:max_len]] + _split_long_str(in_str[max_len:], max_len) + + +def _str_to_code(in_str: str, max_len: int, quotes: str) -> str: + return "\n".join(quotes + _ + quotes for _ in _split_long_str(in_str, max_len)) + + def _create_str(in_encrypted_puzzle, configurator) -> str: modules: str = "\n".join("import " + _ for _ in configurator.get_modules()) + "\n" objects: str = "\n".join( inspect.getsource(_) for _ in configurator.get_needed_objects() ) - question = in_encrypted_puzzle[0] - rest_str = bytestr_utils.bytes_to_bytestr(in_encrypted_puzzle[1]) - puzzle_data: str = f'_PUZZLE = ("""{question}""", bytestr_to_bytes("{rest_str}"))' + question = _str_to_code(in_encrypted_puzzle[0], 78, '"""') + rest_str = _str_to_code( + bytestr_utils.bytes_to_bytestr(in_encrypted_puzzle[1]), 78, '"' + ) + puzzle_data: str = f"_PUZZLE = ({question}, bytestr_to_bytes({rest_str}))" call: str = "run_puzzle(_PUZZLE, _DECRYPT, input)" return ( diff --git a/tests/test_create_puzzle.py b/tests/test_create_puzzle.py index 28fb8f0..64a8819 100644 --- a/tests/test_create_puzzle.py +++ b/tests/test_create_puzzle.py @@ -44,9 +44,16 @@ def _positive_puzzle_tc(qa_list: typing.List[str]) -> _PuzzleTestCase: "Congratulations!", ] +_MULTILINE_QUESTION = ( + "Question 1❓\n" + "-very-long-line----------------------------------------------------" + "-------------------------------------------------------------------" + "------------------------------------------------end-of-long-line-\n" + "With several lines!\n" +) _QA_LIST_2 = [ - "Question 1❓\n-------\nWith several lines!\n", + _MULTILINE_QUESTION, "1", "Q2?", "A2", @@ -72,10 +79,8 @@ def _positive_puzzle_tc(qa_list: typing.List[str]) -> _PuzzleTestCase: _PuzzleTestCase( qa_list=_QA_LIST_2, input=["Wrong!"], - output="""Question 1❓ -------- -With several lines! - + output=_MULTILINE_QUESTION + + """ This is a wrong answer. Try again! """, ), @@ -111,6 +116,7 @@ def _run_puzzle_str( f"puzzle-generator {importlib.metadata.version('puzzle-generator')}" in in_puzzle ) + assert all(len(_) <= 88 for _ in in_puzzle.splitlines()) with open(in_puzzle_path, "w", encoding="utf-8") as puzzle_file: puzzle_file.write(in_puzzle) return _run_puzzle_file(in_puzzle_path, answers)