@@ -26,7 +26,6 @@ class TestWord(unittest.TestCase):
|
||||
|
||||
self.assertEqual(word.text, "hello")
|
||||
self.assertEqual(word.style, self.font)
|
||||
self.assertEqual(word.background, self.font.background)
|
||||
self.assertIsNone(word.previous)
|
||||
self.assertIsNone(word.next)
|
||||
self.assertIsNone(word.hyphenated_parts)
|
||||
@@ -252,27 +251,6 @@ class TestWord(unittest.TestCase):
|
||||
for i, expected_part in enumerate(expected_parts):
|
||||
self.assertEqual(word.get_hyphenated_part(i), expected_part)
|
||||
|
||||
def test_word_create_and_add_to_with_container_style(self):
|
||||
"""Test Word.create_and_add_to with container that has style property."""
|
||||
# Create mock container with style and add_word method
|
||||
mock_container = Mock()
|
||||
mock_container.style = self.font
|
||||
mock_container.add_word = Mock()
|
||||
# Ensure _words and background don't interfere
|
||||
del mock_container._words
|
||||
del mock_container.background # Remove background so it inherits from font
|
||||
|
||||
# Create and add word
|
||||
word = Word.create_and_add_to("hello", mock_container)
|
||||
|
||||
# Test that word was created with correct properties
|
||||
self.assertIsInstance(word, Word)
|
||||
self.assertEqual(word.text, "hello")
|
||||
self.assertEqual(word.style, self.font)
|
||||
self.assertEqual(word.background, self.font.background)
|
||||
|
||||
# Test that add_word was called
|
||||
mock_container.add_word.assert_called_once_with(word)
|
||||
|
||||
def test_word_create_and_add_to_with_style_override(self):
|
||||
"""Test Word.create_and_add_to with explicit style parameter."""
|
||||
|
||||
@@ -398,22 +398,22 @@ class TestHTMLFontRegistryIntegration(unittest.TestCase):
|
||||
</div>
|
||||
"""
|
||||
|
||||
# Initially empty font registry
|
||||
initial_font_count = len(self.doc._fonts)
|
||||
# Initially empty style registry
|
||||
initial_style_count = self.doc.get_style_registry().get_style_count()
|
||||
|
||||
# Parse HTML with document context
|
||||
blocks = parse_html_string(html_content, self.base_font, document=self.doc)
|
||||
|
||||
# Should have created fonts for different styles
|
||||
final_font_count = len(self.doc._fonts)
|
||||
self.assertGreater(final_font_count, initial_font_count,
|
||||
"Should have created fonts in registry")
|
||||
# Should have created styles for different formatting
|
||||
final_style_count = self.doc.get_style_registry().get_style_count()
|
||||
self.assertGreater(final_style_count, initial_style_count,
|
||||
"Should have created styles in registry")
|
||||
|
||||
# Should have created blocks
|
||||
self.assertGreater(len(blocks), 0, "Should have created blocks")
|
||||
|
||||
def test_font_registry_reuses_fonts(self):
|
||||
"""Test that parsing same content reuses existing fonts."""
|
||||
"""Test that parsing same content reuses existing styles."""
|
||||
html_content = """
|
||||
<div>
|
||||
<p>This is <strong>bold text</strong> and <em>italic text</em>.</p>
|
||||
@@ -423,43 +423,43 @@ class TestHTMLFontRegistryIntegration(unittest.TestCase):
|
||||
|
||||
# First parse
|
||||
blocks1 = parse_html_string(html_content, self.base_font, document=self.doc)
|
||||
first_parse_font_count = len(self.doc._fonts)
|
||||
first_parse_style_count = self.doc.get_style_registry().get_style_count()
|
||||
|
||||
# Second parse with same content
|
||||
blocks2 = parse_html_string(html_content, self.base_font, document=self.doc)
|
||||
second_parse_font_count = len(self.doc._fonts)
|
||||
second_parse_style_count = self.doc.get_style_registry().get_style_count()
|
||||
|
||||
# Font count should not increase on second parse
|
||||
self.assertEqual(first_parse_font_count, second_parse_font_count,
|
||||
"Should reuse existing fonts instead of creating new ones")
|
||||
# Style count should not increase on second parse
|
||||
self.assertEqual(first_parse_style_count, second_parse_style_count,
|
||||
"Should reuse existing styles instead of creating new ones")
|
||||
|
||||
# Both parses should create same number of blocks
|
||||
self.assertEqual(len(blocks1), len(blocks2),
|
||||
"Should create same structure on both parses")
|
||||
|
||||
def test_font_registry_different_styles_create_different_fonts(self):
|
||||
"""Test that different styles create different font objects."""
|
||||
# Create fonts with different properties
|
||||
font1 = self.doc.get_or_create_font(
|
||||
font_size=14, colour=(255, 0, 0), weight=FontWeight.BOLD
|
||||
"""Test that different styles create different style objects."""
|
||||
# Create styles with different properties
|
||||
style_id1, style1 = self.doc.get_or_create_style(
|
||||
font_size=14, color=(255, 0, 0), font_weight=FontWeight.BOLD
|
||||
)
|
||||
font2 = self.doc.get_or_create_font(
|
||||
font_size=16, colour=(255, 0, 0), weight=FontWeight.BOLD
|
||||
style_id2, style2 = self.doc.get_or_create_style(
|
||||
font_size=16, color=(255, 0, 0), font_weight=FontWeight.BOLD
|
||||
)
|
||||
font3 = self.doc.get_or_create_font(
|
||||
font_size=14, colour=(0, 255, 0), weight=FontWeight.BOLD
|
||||
style_id3, style3 = self.doc.get_or_create_style(
|
||||
font_size=14, color=(0, 255, 0), font_weight=FontWeight.BOLD
|
||||
)
|
||||
|
||||
# Should be different objects
|
||||
self.assertIsNot(font1, font2, "Different sizes should create different fonts")
|
||||
self.assertIsNot(font1, font3, "Different colors should create different fonts")
|
||||
self.assertIsNot(font2, font3, "All fonts should be different")
|
||||
# Should be different style IDs
|
||||
self.assertNotEqual(style_id1, style_id2, "Different sizes should create different styles")
|
||||
self.assertNotEqual(style_id1, style_id3, "Different colors should create different styles")
|
||||
self.assertNotEqual(style_id2, style_id3, "All styles should be different")
|
||||
|
||||
# Should have 3 fonts in registry
|
||||
self.assertEqual(len(self.doc._fonts), 3)
|
||||
# Should have multiple styles in registry
|
||||
self.assertGreaterEqual(self.doc.get_style_registry().get_style_count(), 3)
|
||||
|
||||
def test_font_registry_integration_with_html_styles(self):
|
||||
"""Test that HTML parsing uses font registry for styled content."""
|
||||
"""Test that HTML parsing uses style registry for styled content."""
|
||||
html_content = """
|
||||
<p>Normal text with <strong>bold</strong> and <em>italic</em> and
|
||||
<span style="color: red;">red text</span>.</p>
|
||||
@@ -485,14 +485,17 @@ class TestHTMLFontRegistryIntegration(unittest.TestCase):
|
||||
self.assertGreater(len(italic_words), 0, "Should have italic words")
|
||||
self.assertGreater(len(red_words), 0, "Should have red words")
|
||||
|
||||
# Font registry should contain multiple fonts for different styles
|
||||
self.assertGreater(len(self.doc._fonts), 1,
|
||||
"Should have multiple fonts for different styles")
|
||||
# Style registry should contain multiple styles for different formatting
|
||||
self.assertGreater(self.doc.get_style_registry().get_style_count(), 1,
|
||||
"Should have multiple styles for different formatting")
|
||||
|
||||
def test_font_registry_without_document_context(self):
|
||||
"""Test that parsing without document context works (fallback behavior)."""
|
||||
html_content = "<p>This is <strong>bold text</strong>.</p>"
|
||||
|
||||
# Get initial style count (should include default style)
|
||||
initial_style_count = self.doc.get_style_registry().get_style_count()
|
||||
|
||||
# Parse without document context
|
||||
blocks = parse_html_string(html_content, self.base_font)
|
||||
|
||||
@@ -500,12 +503,13 @@ class TestHTMLFontRegistryIntegration(unittest.TestCase):
|
||||
self.assertEqual(len(blocks), 1)
|
||||
self.assertIsInstance(blocks[0], Paragraph)
|
||||
|
||||
# Should not affect document's font registry
|
||||
self.assertEqual(len(self.doc._fonts), 0,
|
||||
"Document font registry should remain empty")
|
||||
# Should not affect document's style registry
|
||||
final_style_count = self.doc.get_style_registry().get_style_count()
|
||||
self.assertEqual(final_style_count, initial_style_count,
|
||||
"Document style registry should remain unchanged")
|
||||
|
||||
def test_complex_html_font_reuse(self):
|
||||
"""Test font reuse with complex HTML containing repeated styles."""
|
||||
"""Test style reuse with complex HTML containing repeated styles."""
|
||||
html_content = """
|
||||
<div>
|
||||
<h1>First Header</h1>
|
||||
@@ -517,21 +521,21 @@ class TestHTMLFontRegistryIntegration(unittest.TestCase):
|
||||
|
||||
# Parse content
|
||||
blocks = parse_html_string(html_content, self.base_font, document=self.doc)
|
||||
font_count_after_parse = len(self.doc._fonts)
|
||||
style_count_after_parse = self.doc.get_style_registry().get_style_count()
|
||||
|
||||
# Parse same content again
|
||||
blocks2 = parse_html_string(html_content, self.base_font, document=self.doc)
|
||||
font_count_after_second_parse = len(self.doc._fonts)
|
||||
style_count_after_second_parse = self.doc.get_style_registry().get_style_count()
|
||||
|
||||
# Font count should not increase on second parse
|
||||
self.assertEqual(font_count_after_parse, font_count_after_second_parse,
|
||||
"Fonts should be reused for repeated styles")
|
||||
# Style count should not increase on second parse
|
||||
self.assertEqual(style_count_after_parse, style_count_after_second_parse,
|
||||
"Styles should be reused for repeated formatting")
|
||||
|
||||
# Both should create same structure
|
||||
self.assertEqual(len(blocks), len(blocks2))
|
||||
|
||||
def test_font_registry_with_nested_styles(self):
|
||||
"""Test font registry with nested HTML styles."""
|
||||
"""Test style registry with nested HTML styles."""
|
||||
html_content = """
|
||||
<p>Text with <strong>bold and <em>bold italic</em> nested</strong> styles.</p>
|
||||
"""
|
||||
@@ -539,7 +543,7 @@ class TestHTMLFontRegistryIntegration(unittest.TestCase):
|
||||
# Parse content
|
||||
blocks = parse_html_string(html_content, self.base_font, document=self.doc)
|
||||
|
||||
# Should create fonts for different style combinations
|
||||
# Should create styles for different style combinations
|
||||
paragraph = blocks[0]
|
||||
words = list(paragraph.words())
|
||||
|
||||
@@ -551,9 +555,9 @@ class TestHTMLFontRegistryIntegration(unittest.TestCase):
|
||||
self.assertGreater(len(bold_italic_words), 0,
|
||||
"Should have words with combined bold+italic style")
|
||||
|
||||
# Should have multiple fonts in registry for different combinations
|
||||
self.assertGreater(len(self.doc._fonts), 1,
|
||||
"Should create separate fonts for style combinations")
|
||||
# Should have multiple styles in registry for different combinations
|
||||
self.assertGreater(self.doc.get_style_registry().get_style_count(), 1,
|
||||
"Should create separate styles for style combinations")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user