Skip to content

Commit

Permalink
Fix: Fixed a bug in regards to empty inputs in AddTextLetterByLetter …
Browse files Browse the repository at this point in the history
…class. (ManimCommunity#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 <[email protected]>
  • Loading branch information
3 people authored Oct 26, 2023
1 parent b048695 commit 76b4061
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions manim/animation/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 34 additions & 0 deletions tests/module/animation/test_creation.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 76b4061

Please sign in to comment.