2Tests for `chives.text`.
7from chives.text import coloured, smartify, TextColour
10@pytest.mark.parametrize(
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>"It's nice", he said</p>", "<p>“It’s nice”, he said</p>"),
25 "<!-- this -- is -- not -- a -- valid -- comment -->",
26 "<!– this – is – not – a – valid – comment –>",
30def test_smartify(text: str, expected: str) -> None:
34 actual = smartify(text)
35 assert actual == expected
37 assert smartify(actual) == actual
40@pytest.mark.parametrize(
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>',
50 '<style>\n @use "components/list_of_posts";\n</style>',
53def test_is_unchanged_by_smartify(text: str) -> None:
55 Test these strings are unaffected by "smart" punctuation.
57 assert smartify(text) == text
60@pytest.mark.parametrize(
61 "text, colour, expected",
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"),
69def test_coloured(text: str, colour: TextColour, expected: str) -> None:
73 assert coloured(text, colour) == expected
76def test_unrecognised_colour() -> None:
78 Calling `coloured` with an unrecognised colour is a ValueError.
80 with pytest.raises(ValueError, match="unrecognised colour"):
81 coloured("hello world", colour="gray") # type: ignore