From 88c1fadd5a24aac62e98410654397bbc15ad2b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Sat, 13 May 2023 12:07:03 +0500 Subject: [PATCH 1/2] Added tests for utils --- pyproject.toml | 5 + tests/test_text_utils.py | 191 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 tests/test_text_utils.py 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' From 8871479c856b15d33d6f2580e5044cb2ab7f08b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Sat, 13 May 2023 12:11:17 +0500 Subject: [PATCH 2/2] Added tests GitHub Actions --- .github/workflows/test.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..9549602 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,16 @@ +on: [push] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: 3.x + - name: Install dependencies + run: pip install .[test] + - name: Test with pytest + run: pytest -vv --color=yes