@@ -13,17 +13,21 @@ from pyWebLayout.concrete.text import Text, Line
|
||||
from pyWebLayout.abstract.inline import Word
|
||||
from pyWebLayout.style import Font, FontStyle, FontWeight, TextDecoration
|
||||
from pyWebLayout.style import Alignment
|
||||
from tests.utils.test_fonts import create_default_test_font, ensure_consistent_font_in_tests
|
||||
|
||||
class TestText(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Ensure consistent font usage across tests
|
||||
ensure_consistent_font_in_tests()
|
||||
|
||||
# Create a real PIL image (canvas) for testing
|
||||
self.canvas = Image.new('RGB', (800, 600), color='white')
|
||||
|
||||
# Create a real ImageDraw object
|
||||
self.draw = ImageDraw.Draw(self.canvas)
|
||||
|
||||
# Create a real Font object
|
||||
self.style = Font()
|
||||
# Create a consistent test Font object using bundled font
|
||||
self.style = create_default_test_font()
|
||||
|
||||
|
||||
def test_init(self):
|
||||
@@ -114,14 +118,17 @@ class TestText(unittest.TestCase):
|
||||
|
||||
class TestLine(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Ensure consistent font usage across tests
|
||||
ensure_consistent_font_in_tests()
|
||||
|
||||
# Create a real PIL image (canvas) for testing
|
||||
self.canvas = Image.new('RGB', (800, 600), color='white')
|
||||
|
||||
# Create a real ImageDraw object
|
||||
self.draw = ImageDraw.Draw(self.canvas)
|
||||
|
||||
# Create a real Font object
|
||||
self.style = Font()
|
||||
# Create a consistent test Font object using bundled font
|
||||
self.style = create_default_test_font()
|
||||
|
||||
def test_line_init(self):
|
||||
"""Test Line initialization with real objects"""
|
||||
@@ -170,7 +177,7 @@ class TestLine(unittest.TestCase):
|
||||
self.assertEqual(line.text_objects[0].text, "Hello")
|
||||
|
||||
def test_line_add_word_until_overflow(self):
|
||||
"""Test adding a simple word to a line"""
|
||||
"""Test adding a word until overflow occurs with consistent font measurements"""
|
||||
spacing = (5, 15)
|
||||
origin = np.array([0, 0])
|
||||
size = np.array([400, 50])
|
||||
@@ -197,10 +204,10 @@ class TestLine(unittest.TestCase):
|
||||
self.assertEqual(overflow_part.text, "dam")
|
||||
return
|
||||
|
||||
self.assertFalse(True)
|
||||
self.fail("Expected overflow to occur but reached max iterations")
|
||||
|
||||
def test_line_add_word_until_overflow_small(self):
|
||||
"""Test adding a simple word to a line"""
|
||||
"""Test adding small words until line is full (no overflow expected)"""
|
||||
spacing = (5, 15)
|
||||
origin = np.array([0, 0])
|
||||
size = np.array([400, 50])
|
||||
@@ -227,10 +234,10 @@ class TestLine(unittest.TestCase):
|
||||
self.assertIsNone(overflow_part)
|
||||
return
|
||||
|
||||
self.assertFalse(True)
|
||||
self.fail("Expected line to reach capacity but reached max iterations")
|
||||
|
||||
def test_line_add_word_until_overflow_long_brute(self):
|
||||
"""Test adding a simple word to a line"""
|
||||
"""Test adding a simple word to a line with consistent font measurements"""
|
||||
spacing = (5, 15)
|
||||
origin = np.array([0, 0])
|
||||
size = np.array([400, 50])
|
||||
@@ -245,6 +252,8 @@ class TestLine(unittest.TestCase):
|
||||
)
|
||||
|
||||
# Create a word to add
|
||||
# Note: Expected overflow result depends on the specific font measurements
|
||||
# With DejaVuSans bundled font, this should consistently return "A" as overflow
|
||||
|
||||
for i in range(100):
|
||||
word = Word(text="AAAAAAA", style=self.style)
|
||||
@@ -254,10 +263,11 @@ class TestLine(unittest.TestCase):
|
||||
success, overflow_part = line.add_word(word)
|
||||
# If successful, the word should be added
|
||||
if overflow_part:
|
||||
self.assertEqual(overflow_part.text , "AA")
|
||||
# Updated to match DejaVuSans font measurements for consistency
|
||||
self.assertEqual(overflow_part.text, "A")
|
||||
return
|
||||
|
||||
self.assertFalse(True)
|
||||
self.fail("Expected overflow to occur but reached max iterations")
|
||||
|
||||
|
||||
def test_line_render(self):
|
||||
|
||||
Reference in New Issue
Block a user