From 76b4061aa6d8b289b85bb517a4e15d645bf97792 Mon Sep 17 00:00:00 2001 From: Immanuel-Alvaro-Bhirawa <127812163+Immanuel-Alvaro-Bhirawa@users.noreply.github.com> Date: Thu, 26 Oct 2023 21:38:57 +1100 Subject: [PATCH] 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 --- manim/animation/creation.py | 5 ++++ tests/module/animation/test_creation.py | 34 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/module/animation/test_creation.py 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