Issue
I have this function:
def text_to_html(text):
"""
Convert text into HTML
Args:
text (): text to convert
Returns:
HTML string
"""
html = "<p>" + text.replace("\n", "<br>") + "</p>"
return html
I'm trying to create a unit test for this function like this.
class TestDebtModules(unittest.TestCase):
def test_html(self):
string = """
THIS
IS A TEST
STRING
"""
string_input = text_to_html(string)
string_output = r'<p><br>THIS <br>IS A TEST <br>STRING<br></p>'
self.assertEqual(string_input, string_output)
However. it's throwing an assertion error because it's adding extra characters in my string. Is this a known bug or is it me doing something wrong? (Hopefully the latter). If it happens to be the former, is there any workaround for this?
Expected :<p><br> THIS<br> IS A TEST<br> STRING<br> </p>
Actual :<p><br>THIS <br>IS A TEST <br>STRING<br></p>
Solution
Your string is in """
a multiline string, which includes the spaces and newlines.
To remove those spaces, consider using a string with implicit newlines instead, like below:
string = "THIS\nIS A TEST\nSTRING\n"
or
string = ("THIS\n"
"IS A TEST\n"
"STRING\n")
If you still wanted to use it in the same way, you could remove the spaces as below:
string = """
THIS
IS A TEST
STRING
"""
string = string.replace(" ", "")
However, this would also remove the spaces between words
Answered By - HPringles
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.