diff --git a/pyproject.toml b/pyproject.toml index 4959582..dd5a790 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,11 @@ dependencies = [ ] dynamic = ["version"] +[project.optional-dependencies] +test = [ + "pytest" +] + [project.urls] Source = "https://github.com/hatkidchan/mastoposter" diff --git a/tests/test_text_utils.py b/tests/test_text_utils.py new file mode 100644 index 0000000..4e1309f --- /dev/null +++ b/tests/test_text_utils.py @@ -0,0 +1,191 @@ +""" +mastoposter - configurable reposter from Mastodon-compatible Fediverse servers +Copyright (C) 2022-2023 hatkidchan + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +""" + +from bs4 import BeautifulSoup +from pytest import mark + +from mastoposter.text import md_escape, node_process + + +def test_md_escape(): + assert md_escape(r"text") == r"text" + assert md_escape(r"*meow*") == r"\*meow\*" + assert md_escape(r"\~test") == r"\\\~test" + + +def test_node_to_plaintext_strip_tag(): + soup = BeautifulSoup('test', features="lxml") + assert node_process(soup, "plain") == "test" + + +def test_node_to_plaintext_tag_a(): + soup = BeautifulSoup('test', + features="lxml") + assert node_process(soup, "plain") == "test (https://example.com)" + + +def test_node_to_plaintext_tag_p(): + soup = BeautifulSoup('

Lorem ipsum

', features="lxml") + assert node_process(soup, "plain") == "Lorem ipsum\n\n" + + +def test_node_to_plaintext_tag_br(): + soup = BeautifulSoup('

test1
test2

', features="lxml") + assert node_process(soup, "plain").rstrip() == "test1\ntest2" + + +def test_node_to_plaintext_tag_blockquote(): + soup = BeautifulSoup('
Lorem ipsum
', + features="lxml") + assert node_process(soup, "plain").rstrip() == "\u258d Lorem ipsum" + + +def test_node_to_plaintext_tag_ul(): + soup = BeautifulSoup('', features="lxml") + assert node_process(soup, "plain") == "\n\u2022 test1\n\u2022 test2" + + +def test_node_to_plaintext_tag_ol(): + soup = BeautifulSoup('
  1. test1
  2. test2
', features="lxml") + assert node_process(soup, "plain") == "\n1. test1\n2. test2" + + +@mark.parametrize("tag", ["video", "span"]) +def test_node_to_markdown_strip_tag(tag): + soup = BeautifulSoup('<{0}>test'.format(tag), features="lxml") + assert node_process(soup, "markdown") == "test" + + +def test_node_to_markdown_tag_a(): + soup = BeautifulSoup('test', + features="lxml") + assert node_process(soup, "markdown") == "[test](https://example.com)" + + +def test_node_to_markdown_tag_p(): + soup = BeautifulSoup('

Lorem ipsum

', features="lxml") + assert node_process(soup, "markdown") == "Lorem ipsum\n\n" + + +def test_node_to_markdown_tag_i(): + soup = BeautifulSoup('test', features="lxml") + assert node_process(soup, "markdown") == "*test*" + + +def test_node_to_markdown_tag_b(): + soup = BeautifulSoup('test', features="lxml") + assert node_process(soup, "markdown") == "**test**" + + +def test_node_to_markdown_tag_s(): + soup = BeautifulSoup('test', features="lxml") + assert node_process(soup, "markdown") == "~~test~~" + + +def test_node_to_markdown_tag_u(): + soup = BeautifulSoup('test', features="lxml") + assert node_process(soup, "markdown") == "__test__" + + +def test_node_to_markdown_tag_pre(): + soup = BeautifulSoup('
Lorem ipsum
', features="lxml") + assert node_process(soup, "markdown") == "\n```Lorem ipsum```\n" + + +def test_node_to_markdown_tag_code(): + soup = BeautifulSoup('test', features="lxml") + assert node_process(soup, "markdown") == "`test`" + + +def test_node_to_markdown_tag_blockquote(): + soup = BeautifulSoup('
Lorem ipsum
', features="lxml") + assert node_process(soup, "markdown") == "> Lorem ipsum" + + +def test_node_to_markdown_tag_br(): + soup = BeautifulSoup('

test1
test2

', features="lxml") + assert node_process(soup, "markdown").rstrip() == "test1\ntest2" + + +def test_node_to_markdown_tag_ul(): + soup = BeautifulSoup('', features="lxml") + assert node_process(soup, "markdown") == "\n* test1\n* test2" + + +def test_node_to_markdown_tag_ol(): + soup = BeautifulSoup('
  1. test1
  2. test2
', features="lxml") + assert node_process(soup, "markdown") == "\n1. test1\n2. test2" + + +@mark.parametrize("tag", ["video", "span"]) +def test_node_to_html_strip_tag(tag): + soup = BeautifulSoup('<{0}>test'.format(tag), features="lxml") + assert node_process(soup, "html") == "test" + + +@mark.parametrize("tag", ["i", "b", "s", "u", "code"]) +def test_node_to_html_keep_tag(tag): + html = '<{0}>test'.format(tag) + soup = BeautifulSoup(html, features="lxml") + assert node_process(soup, "html") == html + + +@mark.parametrize("old_tag,new_tag", + [("strong", "b"), ("em", "i"), ("del", "s"), ("ins", "u")]) +def test_node_to_html_subst_tag(old_tag, new_tag): + soup = BeautifulSoup('<{0}>test'.format(old_tag), features="lxml") + assert node_process(soup, "html") == '<{0}>test'.format(new_tag) + + +def test_node_to_html_tag_a(): + html = 'test' + soup = BeautifulSoup(html, features="lxml") + assert node_process(soup, "html") == html + + +def test_node_to_html_tag_p(): + soup = BeautifulSoup('

Lorem ipsum

', features="lxml") + assert node_process(soup, "html") == "Lorem ipsum\n\n" + + +def test_node_to_html_tag_pre(): + soup = BeautifulSoup('
Lorem ipsum
', features="lxml") + assert node_process(soup, "html") == "\n
Lorem ipsum
\n" + + +def test_node_to_html_tag_blockquote(): + soup = BeautifulSoup('
Lorem ipsum
', + features="lxml") + assert node_process(soup, "html").rstrip() == "▍ Lorem ipsum" + + +def test_node_to_html_tag_br(): + soup = BeautifulSoup('

test1
test2

', features="lxml") + assert node_process(soup, "html").rstrip() == "test1\ntest2" + + +def test_node_to_html_tag_ul(): + soup = BeautifulSoup('', features="lxml") + assert node_process(soup, "html") == "\n\u2022 test1\n\u2022 test2" + + +def test_node_to_html_tag_ol(): + soup = BeautifulSoup('
  1. test1
  2. test2
', features="lxml") + assert node_process(soup, "html") == "\n1. test1\n2. test2" + + +def test_node_to_html_spoiler(): + soup = BeautifulSoup('test', features="lxml") + assert node_process(soup, "html") == 'test'