Add more tests

This commit is contained in:
2023-01-22 10:09:12 +01:00
parent d4fd70965e
commit d3849a60ab
2 changed files with 63 additions and 28 deletions

View File

@ -4,7 +4,7 @@ import tempfile
from reader import Reader from reader import Reader
from discord_rss_bot.custom_filters import convert_to_md, encode_url, entry_is_blacklisted, entry_is_whitelisted from discord_rss_bot.custom_filters import encode_url, entry_is_blacklisted, entry_is_whitelisted
from discord_rss_bot.settings import get_reader from discord_rss_bot.settings import get_reader
@ -104,30 +104,3 @@ def test_entry_is_blacklisted() -> None:
# Close the reader, so we can delete the directory. # Close the reader, so we can delete the directory.
custom_reader.close() custom_reader.close()
def test_convert_to_md():
# Test normal input
assert (
convert_to_md("<h1>Headline</h1><h2>Subheadline</h2><h3>subsubheadline</h3>")
== """Headline
========
Subheadline
-----------
### subsubheadline
"""
)
# Test input with tables
assert (
convert_to_md(
"<table><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tbody><tr><td>Row 1, Column 1</td><td>Row 1, Column 2</td><td>Row 1, Column 3</td></tr><tr><td>Row 2, Column 1</td><td>Row 2, Column 2</td><td>Row 2, Column 3</td></tr></tbody></table>" # noqa: E501
)
== "Column 1Column 2Column 3Row 1, Column 1Row 1, Column 2Row 1, Column 3Row 2, Column 1Row 2, Column 2Row 2, Column 3" # noqa: E501
)
# Test empty input
assert convert_to_md("") == ""
# Test input as None
assert convert_to_md(None) == "" # type: ignore

62
tests/test_markdown.py Normal file
View File

@ -0,0 +1,62 @@
from discord_rss_bot.markdown import convert_html_to_md
def test_convert_to_md():
# Test bold
assert convert_html_to_md("<b>bold</b>") == "**bold**"
# Test italic
assert convert_html_to_md("<i>italic</i>") == "*italic*"
# Test blockquote
assert convert_html_to_md("<blockquote>blockquote</blockquote>") == ">>> blockquote"
# Test code
assert convert_html_to_md("<code>code</code>") == "`code`"
# Test strikethrough
assert convert_html_to_md("<s>strikethrough</s>") == "~~strikethrough~~"
# Test link
assert convert_html_to_md('<a href="https://example.com">link</a>') == "[link](https://example.com)"
# Test pre code
assert convert_html_to_md("<pre><code>pre code</code></pre>") == "``pre code``"
# Test strong
assert convert_html_to_md("<strong>strong</strong>") == "**strong**"
# Test multiple tags
assert (
convert_html_to_md(
'<b>bold</b> <i>italic</i> <a href="https://example.com">link</a> <code>code</code> <s>strikethrough</s>'
)
== "**bold** *italic* [link](https://example.com) `code` ~~strikethrough~~"
)
# Test removing all other tags
assert convert_html_to_md("<p>paragraph</p>") == "paragraph"
assert convert_html_to_md("<p>paragraph</p><p>paragraph</p>") == "paragraphparagraph"
# Test <br> tags
assert (
convert_html_to_md("<p>paragraph<br>paragraph</p>")
== """paragraph
paragraph"""
)
# Test removing trailing newline
assert convert_html_to_md("paragraph ") == "paragraph"
# Test removing leading and trailing whitespace
assert convert_html_to_md(" paragraph ") == "paragraph"
# Test removing leading and trailing whitespace and trailing newline
assert (
convert_html_to_md(
""" paragraph
""" # noqa: W293
)
== "paragraph"
)