"""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_inside_paragraphs(self): self.assertEqual(clean_text("a b \t c"), "a b c") def test_paragraph_breaks_survive(self): # Blank lines are the smart chunker's paragraph boundaries (and # reset its quotation state), so they survive cleaning while a # run of blank lines collapses to a single break. self.assertEqual(clean_text("a\n\n b \t c"), "a\n\nb c") self.assertEqual(clean_text("a\n\n\n\nb"), "a\n\nb") self.assertEqual(clean_text("a\n \nb"), "a\n\nb") self.assertEqual(clean_text("line one\nline two"), "line one line two") self.assertEqual(clean_text("a \n\n b"), "a\n\nb") def test_paragraph_breaks_not_glued_to_text(self): self.assertEqual( clean_text("End of chapter.\n\n\n New chapter. \n\nStarts here."), "End of chapter.\n\nNew chapter.\n\nStarts here.", ) 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): # The page number's own line becomes a paragraph break (a safe # chunk boundary), not a glued sentence. self.assertEqual( clean_text("End of page.\n7\nNext page text."), "End of page.\n\nNext page text.", ) def test_page_number_removal_leaves_paragraph_break(self): result = clean_text("Chapter one\n\n12\n\nChapter two") self.assertEqual(result, "Chapter one\n\nChapter two") self.assertNotIn(" ", result) class CleanHtmlTests(unittest.TestCase): def test_strips_tags(self): self.assertEqual(clean_html("
Hello world
"), "Hello world") def test_removes_script_and_style(self): html = "Text
" self.assertEqual(clean_html(html), "Text") def test_unescapes_entities(self): self.assertEqual(clean_html("Tom & Jerry"), "Tom & Jerry") def test_empty(self): self.assertEqual(clean_html(""), "") self.assertEqual(clean_html(None), "") def test_inline_markup_never_splits_words(self): # Inline tags must not inject spaces mid-word (they become chunk # boundaries and corrupt pronunciation). self.assertEqual( clean_html("He was unbelievable and didn't stop.
"), "He was unbelievable and didn't stop.", ) def test_block_tags_become_paragraph_breaks(self): self.assertEqual( clean_html("First para.
Second para.
| A | B |
| C | D |
unbelievable