Skip to content

Commit 76b4061

Browse files
Immanuel-Alvaro-Bhirawapre-commit-ci[bot]behackl
authored
Fix: Fixed a bug in regards to empty inputs in AddTextLetterByLetter class. (#3404)
* Misc: Just a class to test out some functions * Fix: Fixed a bug in AddTextLetterByLetter class * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix: Adjusted changes according to Ben's comments * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix: Removed imports * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Feat: Adjusted changes to AddTextLetterByLetter * Feat: Added test_creation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Benjamin Hackl <devel@benjamin-hackl.at>
1 parent b048695 commit 76b4061

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

manim/animation/creation.py

+5
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ def __init__(
563563
**kwargs,
564564
) -> None:
565565
self.time_per_char = time_per_char
566+
# Check for empty text using family_members_with_points()
567+
if not text.family_members_with_points():
568+
raise ValueError(
569+
f"The text mobject {text} does not seem to contain any characters."
570+
)
566571
if run_time is None:
567572
# minimum time per character is 1/frame_rate, otherwise
568573
# the animation does not finish.
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from __future__ import annotations
2+
3+
import numpy as np
4+
import pytest
5+
6+
from manim import AddTextLetterByLetter, Text, config
7+
8+
9+
def test_non_empty_text_creation():
10+
"""Check if AddTextLetterByLetter works for non-empty text."""
11+
s = Text("Hello")
12+
anim = AddTextLetterByLetter(s)
13+
assert anim.mobject.text == "Hello"
14+
15+
16+
def test_empty_text_creation():
17+
"""Ensure ValueError is raised for empty text."""
18+
with pytest.raises(ValueError, match="does not seem to contain any characters"):
19+
AddTextLetterByLetter(Text(""))
20+
21+
22+
def test_whitespace_text_creation():
23+
"""Ensure ValueError is raised for whitespace-only text, assuming the whitespace characters have no points."""
24+
with pytest.raises(ValueError, match="does not seem to contain any characters"):
25+
AddTextLetterByLetter(Text(" "))
26+
27+
28+
def test_run_time_for_non_empty_text():
29+
"""Ensure the run_time is calculated correctly for non-empty text."""
30+
s = Text("Hello")
31+
run_time_per_char = 0.1
32+
expected_run_time = np.max((1 / config.frame_rate, run_time_per_char)) * len(s.text)
33+
anim = AddTextLetterByLetter(s, time_per_char=run_time_per_char)
34+
assert anim.run_time == expected_run_time

0 commit comments

Comments
 (0)