Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions arcade/text.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -177,7 +177,7 @@ def __init__(
y: float,
color: RGBOrA255 = arcade.color.WHITE,
font_size: float = 12,
width: Optional[int] = 0,
width: int | None = None,
align: str = "left",
font_name: FontNameOrNames = ("calibri", "arial"),
bold: bool = False,
Expand All@@ -186,8 +186,8 @@ def __init__(
anchor_y: str = "baseline",
multiline: bool = False,
rotation: float = 0,
batch: Optional[pyglet.graphics.Batch] = None,
group: Optional[pyglet.graphics.Group] = None,
batch: pyglet.graphics.Batch | None = None,
group: pyglet.graphics.Group | None = None,
z: float = 0,
):
# Raises a RuntimeError if no window for better user feedback
Expand All@@ -196,8 +196,11 @@ def __init__(
if align not in ("left", "center", "right"):
raise ValueError("The 'align' parameter must be equal to 'left', 'right', or 'center'.")

if multiline and width == 0:
raise ValueError("The 'width' parameter must be set when 'multiline' is True.")
if multiline and not width:
raise ValueError(
f"The 'width' parameter must be set to a non-zero value when 'multiline' is True, "
f"but got {width!r}."
)

adjusted_font = _attempt_font_name_resolution(font_name)
self._label = pyglet.text.Label(
Expand DownExpand Up@@ -581,15 +584,15 @@ def create_text_sprite(
text: str,
color: RGBA255 = arcade.color.WHITE,
font_size: float = 12,
width: Optional[int] = None,
width: int | None = None,
align: str = "left",
font_name: FontNameOrNames = ("calibri", "arial"),
bold: bool = False,
italic: bool = False,
anchor_x: str = "left",
multiline: bool = False,
texture_atlas: Optional[TextureAtlasBase] = None,
background_color: Optional[RGBA255] = None,
texture_atlas: TextureAtlasBase | None = None,
background_color: RGBA255 | None = None,
) -> arcade.Sprite:
"""
Creates a sprite containing text based off of :py:class:`~arcade.Text`.
Expand DownExpand Up@@ -669,7 +672,7 @@ def draw_text(
y: int,
color: RGBA255 = arcade.color.WHITE,
font_size: float = 12,
width: int = 0,
width: int | None = None,
align: str = "left",
font_name: FontNameOrNames = ("calibri", "arial"),
bold: bool = False,
Expand DownExpand Up@@ -847,8 +850,11 @@ def draw_text(
if align not in ("left", "center", "right"):
raise ValueError("The 'align' parameter must be equal to 'left', 'right', or 'center'.")

if multiline and width == 0:
raise ValueError("The 'width' parameter must be set when 'multiline' is True.")
if multiline and not width:
raise ValueError(
f"The 'width' parameter must be set to a non-zero value when 'multiline' is True, "
f"but got {width!r}."
)

if not label:
adjusted_font = _attempt_font_name_resolution(font_name)
Expand Down
18 changes: 14 additions & 4 deletions tests/unit/text/test_text_error_handling.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,23 @@

def test_text_instance_raise_multiline_error(window):
with pytest.raises(ValueError) as e:
_ = arcade.Text("Initial text", 0, 0, multiline=True)
_ = arcade.Text("Initial text", 0, 0, width=0, multiline=True)

assert e.value.args[0] == "The 'width' parameter must be set when 'multiline' is True."
assert e.value.args[0] == "The 'width' parameter must be set to a non-zero value when 'multiline' is True, but got 0."

with pytest.raises(ValueError) as e:
_ = arcade.Text("Initial text", 0, 0, width=None, multiline=True)

assert e.value.args[0] == "The 'width' parameter must be set to a non-zero value when 'multiline' is True, but got None."


def test_text_function_raise_multiline_error(window):
with pytest.raises(ValueError) as e:
_ = arcade.draw_text("Initial text", 0, 0, multiline=True)
_ = arcade.draw_text("Initial text", 0, 0, width=0, multiline=True)

assert e.value.args[0] == "The 'width' parameter must be set to a non-zero value when 'multiline' is True, but got 0."

with pytest.raises(ValueError) as e:
_ = arcade.draw_text("Initial text", 0, 0, width=None, multiline=True)

assert e.value.args[0] == "The 'width' parameter must be set when 'multiline' is True."
assert e.value.args[0] == "The 'width' parameter must be set to a non-zero value when 'multiline' is True, but got None."