diff --git a/manim/animation/creation.py b/manim/animation/creation.py index a2e7f8a362..f28c123025 100644 --- a/manim/animation/creation.py +++ b/manim/animation/creation.py @@ -563,6 +563,11 @@ def __init__( **kwargs, ) -> None: self.time_per_char = time_per_char + # Check for empty text using family_members_with_points() + if not text.family_members_with_points(): + raise ValueError( + f"The text mobject {text} does not seem to contain any characters." + ) if run_time is None: # minimum time per character is 1/frame_rate, otherwise # the animation does not finish. diff --git a/tests/module/animation/test_creation.py b/tests/module/animation/test_creation.py new file mode 100644 index 0000000000..3208ad1b41 --- /dev/null +++ b/tests/module/animation/test_creation.py @@ -0,0 +1,34 @@ +from __future__ import annotations + +import numpy as np +import pytest + +from manim import AddTextLetterByLetter, Text, config + + +def test_non_empty_text_creation(): + """Check if AddTextLetterByLetter works for non-empty text.""" + s = Text("Hello") + anim = AddTextLetterByLetter(s) + assert anim.mobject.text == "Hello" + + +def test_empty_text_creation(): + """Ensure ValueError is raised for empty text.""" + with pytest.raises(ValueError, match="does not seem to contain any characters"): + AddTextLetterByLetter(Text("")) + + +def test_whitespace_text_creation(): + """Ensure ValueError is raised for whitespace-only text, assuming the whitespace characters have no points.""" + with pytest.raises(ValueError, match="does not seem to contain any characters"): + AddTextLetterByLetter(Text(" ")) + + +def test_run_time_for_non_empty_text(): + """Ensure the run_time is calculated correctly for non-empty text.""" + s = Text("Hello") + run_time_per_char = 0.1 + expected_run_time = np.max((1 / config.frame_rate, run_time_per_char)) * len(s.text) + anim = AddTextLetterByLetter(s, time_per_char=run_time_per_char) + assert anim.run_time == expected_run_time