Skip to main content

tests/python/test_text.py

1"""
2Tests for `chives.text`.
3"""
5import pytest
7from chives.text import coloured, smartify, TextColour
10@pytest.mark.parametrize(
11 "text, expected",
12 [
13 ("<", "<"),
14 ("<0", "<0"),
15 ("--", "–"),
16 ("---", "—"),
17 ("&quot;", "“"),
18 ("'", "‘"),
19 ("Isn't it delightful -- she said", "Isn’t it delightful – she said"),
20 ("Are you ... sure?", "Are you … sure?"),
21 ("<h2>Isn't it delightful?</h2>", "<h2>Isn’t it delightful?</h2>"),
22 ("<li>Isn't it delightful?</li>", "<li>Isn’t it delightful?</li>"),
23 ("<p>&quot;It's nice&quot;, he said</p>", "<p>“It’s nice”, he said</p>"),
24 (
25 "<!-- this -- is -- not -- a -- valid -- comment -->",
26 "<!– this – is – not – a – valid – comment –>",
27 ),
28 ],
30def test_smartify(text: str, expected: str) -> None:
31 """
32 Test smartify().
33 """
34 actual = smartify(text)
35 assert actual == expected
37 assert smartify(actual) == actual
40@pytest.mark.parametrize(
41 "text",
42 [
43 "<",
44 "<0",
45 '<a href="https://example.com">example.com</a>',
46 '<pre>print("hello world")</pre>',
47 '<pre><code data-lang="python">print("hello world")</code></pre>',
48 "<br/>",
49 "</br>",
50 '<style>\n @use "components/list_of_posts";\n</style>',
51 ],
53def test_is_unchanged_by_smartify(text: str) -> None:
54 """
55 Test these strings are unaffected by "smart" punctuation.
56 """
57 assert smartify(text) == text
60@pytest.mark.parametrize(
61 "text, colour, expected",
62 [
63 ("hello world", "red", "\033[91mhello world\033[0m"),
64 ("hello world", "yellow", "\033[93mhello world\033[0m"),
65 ("hello world", "green", "\033[92mhello world\033[0m"),
66 ("hello world", "blue", "\033[94mhello world\033[0m"),
67 ],
69def test_coloured(text: str, colour: TextColour, expected: str) -> None:
70 """
71 Tests for `coloured`.
72 """
73 assert coloured(text, colour) == expected
76def test_unrecognised_colour() -> None:
77 """
78 Calling `coloured` with an unrecognised colour is a ValueError.
79 """
80 with pytest.raises(ValueError, match="unrecognised colour"):
81 coloured("hello world", colour="gray") # type: ignore