This commit is contained in:
@@ -102,6 +102,83 @@ class TestLineSplittingBug(unittest.TestCase):
|
||||
self.assertEqual(len(line.text_objects), 1)
|
||||
self.assertEqual(line.text_objects[0].text, "short")
|
||||
|
||||
def test_conservative_justified_hyphenation(self):
|
||||
"""Test that justified alignment is more conservative about mid-sentence hyphenation"""
|
||||
font = Font(font_path=None, font_size=12, colour=(0, 0, 0))
|
||||
line = Line((5, 15), (0, 0), (200, 20), font, halign=Alignment.JUSTIFY)
|
||||
|
||||
with patch('pyWebLayout.abstract.inline.pyphen') as mock_pyphen_module:
|
||||
mock_dic = Mock()
|
||||
mock_pyphen_module.Pyphen.return_value = mock_dic
|
||||
mock_dic.inserted.return_value = "test-word"
|
||||
|
||||
# Add words that should fit without hyphenation
|
||||
result1 = line.add_word("This")
|
||||
result2 = line.add_word("should")
|
||||
result3 = line.add_word("testword") # Should NOT be hyphenated with conservative settings
|
||||
|
||||
self.assertIsNone(result1)
|
||||
self.assertIsNone(result2)
|
||||
self.assertIsNone(result3) # Should fit without hyphenation
|
||||
self.assertEqual(len(line.text_objects), 3)
|
||||
self.assertEqual([obj.text for obj in line.text_objects], ["This", "should", "testword"])
|
||||
|
||||
def test_helper_methods_exist(self):
|
||||
"""Test that refactored helper methods exist and work"""
|
||||
font = Font(font_path=None, font_size=12, colour=(0, 0, 0))
|
||||
line = Line((5, 10), (0, 0), (200, 20), font)
|
||||
|
||||
# Test helper methods exist and return reasonable values
|
||||
available_width = line._calculate_available_width(font)
|
||||
self.assertIsInstance(available_width, int)
|
||||
self.assertGreater(available_width, 0)
|
||||
|
||||
safety_margin = line._get_safety_margin(font)
|
||||
self.assertIsInstance(safety_margin, int)
|
||||
self.assertGreaterEqual(safety_margin, 1)
|
||||
|
||||
fits = line._fits_with_normal_spacing(50, 100, font)
|
||||
self.assertIsInstance(fits, bool)
|
||||
|
||||
def test_no_cropping_with_safety_margin(self):
|
||||
"""Test that safety margin prevents text cropping"""
|
||||
font = Font(font_path=None, font_size=12, colour=(0, 0, 0))
|
||||
|
||||
# Create a line that's just barely wide enough
|
||||
line = Line((2, 5), (0, 0), (80, 20), font)
|
||||
|
||||
# Add words that should fit with safety margin
|
||||
result1 = line.add_word("test")
|
||||
result2 = line.add_word("word")
|
||||
|
||||
self.assertIsNone(result1)
|
||||
self.assertIsNone(result2)
|
||||
|
||||
# Verify both words were added
|
||||
self.assertEqual(len(line.text_objects), 2)
|
||||
self.assertEqual([obj.text for obj in line.text_objects], ["test", "word"])
|
||||
|
||||
def test_modular_word_fitting_strategies(self):
|
||||
"""Test that word fitting strategies work in proper order"""
|
||||
font = Font(font_path=None, font_size=12, colour=(0, 0, 0))
|
||||
line = Line((5, 10), (0, 0), (80, 20), font) # Narrower line to force overflow
|
||||
|
||||
# Test normal spacing strategy
|
||||
result1 = line.add_word("short")
|
||||
self.assertIsNone(result1)
|
||||
|
||||
# Test that we can add multiple words
|
||||
result2 = line.add_word("words")
|
||||
self.assertIsNone(result2)
|
||||
|
||||
# Test overflow handling with a definitely too-long word
|
||||
result3 = line.add_word("verylongwordthatdefinitelywontfitinnarrowline")
|
||||
self.assertIsNotNone(result3) # Should return overflow
|
||||
|
||||
# Line should have the first two words only
|
||||
self.assertEqual(len(line.text_objects), 2)
|
||||
self.assertEqual([obj.text for obj in line.text_objects], ["short", "words"])
|
||||
|
||||
|
||||
def demonstrate_bug():
|
||||
"""Demonstrate the bug with a practical example"""
|
||||
|
||||
Reference in New Issue
Block a user