comparison tests/test_format.py @ 27:c898b4df0f29

Use context for html stripping, with support for custom URL sizes For instance in Twitter URLs are 23 characters long since they use their own URL shortening service. Without taking this into account, post lengths would not be calculated correctly.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 19 Apr 2023 12:46:58 -0700
parents fb93d3fbff4e
children 9e4eb3f2754e
comparison
equal deleted inserted replaced
26:c8e0d4c12f92 27:c898b4df0f29
1 import pytest 1 import pytest
2 from silorider.format import format_entry, strip_html 2 from silorider.format import (
3 format_entry, strip_html, HtmlStrippingContext,
4 URLMODE_INLINE, URLMODE_LAST, URLMODE_BOTTOM_LIST)
3 5
4 6
5 test_url = 'https://example.org/article' 7 test_url = 'https://example.org/article'
6 8
7 9
32 "Something with a link http://example.org/blah"), 34 "Something with a link http://example.org/blah"),
33 ("<p>Something with <a href=\"http://example.org/first\">one link here</a> and <a href=\"http://example.org/second\">another there</a>...</p>", # NOQA 35 ("<p>Something with <a href=\"http://example.org/first\">one link here</a> and <a href=\"http://example.org/second\">another there</a>...</p>", # NOQA
34 "Something with one link here http://example.org/first and another there http://example.org/second...") # NOQA 36 "Something with one link here http://example.org/first and another there http://example.org/second...") # NOQA
35 ]) 37 ])
36 def test_strip_html(text, expected): 38 def test_strip_html(text, expected):
37 actual = strip_html(text) 39 ctx = HtmlStrippingContext()
40 ctx.url_mode = URLMODE_INLINE
41 actual = strip_html(text, ctx)
42 print(actual)
43 print(expected)
38 assert actual == expected 44 assert actual == expected
39 45
40 46
41 @pytest.mark.parametrize("text, expected", [ 47 @pytest.mark.parametrize("text, expected", [
42 ("<p>Something with <a href=\"http://example.org/blah\">a link</a>", 48 ("<p>Something with <a href=\"http://example.org/blah\">a link</a>",
43 "Something with a link\nhttp://example.org/blah"), 49 "Something with a link\nhttp://example.org/blah"),
44 ("<p>Something with a link <a href=\"http://example.org/blah\">http://example.org</a>", # NOQA 50 ("<p>Something with a link <a href=\"http://example.org/blah\">http://example.org</a>", # NOQA
45 "Something with a link http://example.org/blah"), 51 "Something with a link\nhttp://example.org/blah"),
46 ("<p>Something with <a href=\"http://example.org/first\">one link here</a> and <a href=\"http://example.org/second\">another there</a>...</p>", # NOQA 52 ("<p>Something with <a href=\"http://example.org/first\">one link here</a> and <a href=\"http://example.org/second\">another there</a>...</p>", # NOQA
47 "Something with one link here and another there...\nhttp://example.org/first\nhttp://example.org/second") # NOQA 53 "Something with one link here and another there...\nhttp://example.org/first\nhttp://example.org/second") # NOQA
48 ]) 54 ])
49 def test_strip_html_with_bottom_urls(text, expected): 55 def test_strip_html_with_bottom_urls(text, expected):
50 actual = strip_html(text, inline_urls=False) 56 ctx = HtmlStrippingContext()
57 ctx.url_mode = URLMODE_BOTTOM_LIST
58 actual = strip_html(text, ctx)
51 print(actual) 59 print(actual)
52 print(expected) 60 print(expected)
53 assert actual == expected 61 assert actual == expected
54 62
55 63