aboutsummaryrefslogtreecommitdiff
path: root/tests/test_cleaning.py
blob: 41f4ed7b038756a763450b362f1fd9dcaa0370e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Tests for text and HTML cleaning."""

import unittest

from converter.extractors import clean_html, clean_text


class CleanTextTests(unittest.TestCase):
    def test_empty_input(self):
        self.assertEqual(clean_text(""), "")
        self.assertEqual(clean_text(None), "")

    def test_collapses_whitespace(self):
        self.assertEqual(clean_text("a\n\n   b \t c"), "a b c")

    def test_preserves_inline_numbers(self):
        self.assertEqual(clean_text("He was 42 years old."), "He was 42 years old.")

    def test_preserves_grouped_and_decimal_numbers(self):
        self.assertEqual(
            clean_text("Over 1,000 pages and 3.5 stars."),
            "Over 1,000 pages and 3.5 stars.",
        )

    def test_removes_standalone_page_numbers(self):
        self.assertEqual(
            clean_text("End of page.\n7\nNext page text."),
            "End of page. Next page text.",
        )

    def test_page_number_removal_leaves_single_spacing(self):
        result = clean_text("Chapter one\n\n12\n\nChapter two")
        self.assertEqual(result, "Chapter one Chapter two")
        self.assertNotIn("  ", result)


class CleanHtmlTests(unittest.TestCase):
    def test_strips_tags(self):
        self.assertEqual(clean_html("<p>Hello <b>world</b></p>"), "Hello world")

    def test_removes_script_and_style(self):
        html = "<style>.x{color:red}</style><p>Text</p><script>var a=1;</script>"
        self.assertEqual(clean_html(html), "Text")

    def test_unescapes_entities(self):
        self.assertEqual(clean_html("Tom &amp; Jerry"), "Tom & Jerry")

    def test_empty(self):
        self.assertEqual(clean_html(""), "")
        self.assertEqual(clean_html(None), "")


if __name__ == "__main__":
    unittest.main()