I have a string result which looks like this:
>>> result
'--- \n+++ \n@@ -48,7 +48,7 @@\n "%%time\\n",\n "def hello(name: str = \\"world\\"):\\n",\n "\\n",\n- " return f\'hello {name}\'\\n",\n+ " return f\\"hello {name}\\"\\n",\n "\\n",\n "\\n",\n "hello(3)"\n'
>>> print(result)
---
+++
@@ -48,7 +48,7 @@
"%%time\n",
"def hello(name: str = \"world\"):\n",
"\n",
- " return f'hello {name}'\n",
+ " return f\"hello {name}\"\n",
"\n",
"\n",
"hello(3)"
I'd like to write a test case for this. One (not so easy to read) way to write this would be
expected = '--- \n+++ \n@@ -48,7 +48,7 @@\n "%%time\\n",\n "def hello(name: str = \\"world\\"):\\n",\n "\\n",\n- " return f\'hello {name}\'\\n",\n+ " return f\\"hello {name}\\"\\n",\n "\\n",\n "\\n",\n "hello(3)"\n'
and then
assert result == expected
would work.
Another more readable way would be
expected = (
"""
---
+++
@@ -48,7 +48,7 @@
"%%time\n",
"def hello(name: str = \"world\"):\n",
"\n",
- " return f'hello {name}'\n",
+ " return f\"hello {name}\"\n",
"\n",
"\n",
"hello(3)"
"""
)
but then
assert result == expected
no longer works.
I think that the \n inside the strings make it hard to do something like
'\n'.join([i[8:] for i in expected.split('\n')])
- not to mention that this isn't easy to read.
Is there a way to write expected with triple quotes (as above) in such a way that result==expected will work?