Repository: vil02/puzzle_generator

Issue:
Ensure that the output of `create` does not contain long lines
The output of [`create`](https://github.com/vil02/puzzle_generator/blob/32e2102f144ed1748ce7df17b0e72c92efb6ae6c/puzzle_generator/create_puzzle.py#L54) contains some very long lines. Modify the code in such a way, that each line of the output is at most [88](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-length) characters long.

## How to see the problem
Generate and inspect any puzzle, for example by running
```shell
poetry run python3 examples/basic_usage.py > example_output.py
```
and opening the generated `example_output.py` file.


## Testing strategy
Adding a line like
```python3
assert all(len(_) <= 88 for _ in in_puzzle.splitlines())
```
into [`_run_puzzle_str`](https://github.com/vil02/puzzle_generator/blob/32e2102f144ed1748ce7df17b0e72c92efb6ae6c/tests/test_create_puzzle.py#L105) is a good start.

Hints from issue comments:
@mpa-LHutchinson: Hi @vil02 , can you assign this issue to me? I'll get working on it as soon as I can today@vil02: @mpa-LHutchinson: if you would like to discuss something more directly, feel free to e-mail me: vil02_puzzle_generator@10g.pl.@vil02: @mpa-LHutchinson: do you need some support?@mpa-LHutchinson: Hi @vil02, I've been busy as of late and I don't think I'll be able to work on this issue. If you want you can assign it to someone else. Sorry for not letting you know sooner.@vil02: @mpa-LHutchinson: no worries. Thanks for an update.

Solution patch:
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/pyproject.toml b/pyproject.toml
index a1c16a1..4a7bdae 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "puzzle-generator"
-version = "0.13.0"
+version = "0.14.0"
 description = "Generates python code representing a puzzle"
 authors = ["piotr.idzik <vil02_puzzle_generator@10g.pl>"]
 readme = "./puzzle_generator/README.md"


Test patch:
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)