Skip to content

Commit

Permalink
Change tests to use the config fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGrace2282 committed Jul 12, 2024
1 parent 7d268bd commit 926e962
Show file tree
Hide file tree
Showing 19 changed files with 396 additions and 393 deletions.
6 changes: 3 additions & 3 deletions tests/helpers/graphical_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import numpy as np

from manim import config, logger
from manim import logger
from manim.scene.scene import Scene


def set_test_scene(scene_object: type[Scene], module_name: str):
def set_test_scene(scene_object: type[Scene], module_name: str, config):
"""Function used to set up the test data for a new feature. This will basically set up a pre-rendered frame for a scene. This is meant to be used only
when setting up tests. Please refer to the wiki.
Expand Down Expand Up @@ -46,7 +46,7 @@ def set_test_scene(scene_object: type[Scene], module_name: str):

assert not np.all(
data == np.array([0, 0, 0, 255]),
), f"Control data generated for {str(scene)} only contains empty pixels."
), f"Control data generated for {scene!s} only contains empty pixels."
assert data.shape == (480, 854, 4)
tests_directory = Path(__file__).absolute().parent.parent
path_control_data = Path(tests_directory) / "control_data" / "graphical_units_data"
Expand Down
4 changes: 2 additions & 2 deletions tests/module/animation/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from manim import FadeIn, Scene, config
from manim import FadeIn, Scene


@pytest.mark.parametrize(
Expand All @@ -15,7 +15,7 @@ def test_animation_forbidden_run_time(run_time):
test_scene.play(FadeIn(None, run_time=run_time))


def test_animation_run_time_shorter_than_frame_rate(caplog):
def test_animation_run_time_shorter_than_frame_rate(caplog, config):
test_scene = Scene()
test_scene.play(FadeIn(None, run_time=1 / (config.frame_rate + 1)))
assert (
Expand Down
4 changes: 2 additions & 2 deletions tests/module/animation/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest

from manim import AddTextLetterByLetter, Text, config
from manim import AddTextLetterByLetter, Text


def test_non_empty_text_creation():
Expand All @@ -25,7 +25,7 @@ def test_whitespace_text_creation():
AddTextLetterByLetter(Text(" "))


def test_run_time_for_non_empty_text():
def test_run_time_for_non_empty_text(config):
"""Ensure the run_time is calculated correctly for non-empty text."""
s = Text("Hello")
run_time_per_char = 0.1
Expand Down
4 changes: 2 additions & 2 deletions tests/module/mobject/mobject/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pathlib import Path

from manim import BraceLabel, Mobject, config
from manim import BraceLabel, Mobject


def test_mobject_copy():
Expand All @@ -18,7 +18,7 @@ def test_mobject_copy():
assert orig.submobjects[i] is not copy.submobjects[i]


def test_bracelabel_copy(tmp_path):
def test_bracelabel_copy(tmp_path, config):
"""Test that a copy is a deepcopy."""
# For this test to work, we need to tweak some folders temporarily
original_text_dir = config["text_dir"]
Expand Down
16 changes: 8 additions & 8 deletions tests/module/mobject/mobject/test_opengl_metaclass.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from __future__ import annotations

from manim import Mobject, config, tempconfig
from manim import Mobject
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
from manim.mobject.opengl.opengl_mobject import OpenGLMobject


def test_metaclass_registry():
def test_metaclass_registry(config):
class SomeTestMobject(Mobject, metaclass=ConvertToOpenGL):
pass

assert SomeTestMobject in ConvertToOpenGL._converted_classes

with tempconfig({"renderer": "opengl"}):
assert OpenGLMobject in SomeTestMobject.__bases__
assert Mobject not in SomeTestMobject.__bases__
config.renderer = "opengl"
assert OpenGLMobject in SomeTestMobject.__bases__
assert Mobject not in SomeTestMobject.__bases__

config.renderer = "cairo"
assert Mobject in SomeTestMobject.__bases__
assert OpenGLMobject not in SomeTestMobject.__bases__
config.renderer = "cairo"
assert Mobject in SomeTestMobject.__bases__
assert OpenGLMobject not in SomeTestMobject.__bases__
7 changes: 1 addition & 6 deletions tests/module/mobject/mobject/test_set_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import numpy as np

from manim import RendererType, config
from manim.constants import RIGHT
from manim.mobject.geometry.polygram import Square


def test_Data():
config.renderer = RendererType.OPENGL
def test_Data(using_opengl_renderer):
a = Square().move_to(RIGHT)
data_bb = a.data["bounding_box"]
np.testing.assert_array_equal(
Expand Down Expand Up @@ -39,6 +37,3 @@ def test_Data():
)

np.testing.assert_array_equal(a.bounding_box, data_bb)
config.renderer = (
RendererType.CAIRO
) # needs to be here or else the following cairo tests fail
32 changes: 16 additions & 16 deletions tests/module/mobject/text/test_texmobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import numpy as np
import pytest

from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, config, tempconfig
from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, tempconfig


def test_MathTex():
def test_MathTex(config):
MathTex("a^2 + b^2 = c^2")
assert Path(config.media_dir, "Tex", "e4be163a00cf424f.svg").exists()


def test_SingleStringMathTex():
def test_SingleStringMathTex(config):
SingleStringMathTex("test")
assert Path(config.media_dir, "Tex", "8ce17c7f5013209f.svg").exists()

Expand All @@ -27,7 +27,7 @@ def test_double_braces_testing(text_input, length_sub):
assert len(t1.submobjects) == length_sub


def test_tex():
def test_tex(config):
Tex("The horse does not eat cucumber salad.")
assert Path(config.media_dir, "Tex", "c3945e23e546c95a.svg").exists()

Expand All @@ -45,7 +45,7 @@ def test_tex_temp_directory(tmpdir, monkeypatch):
assert Path("media", "Tex", "c3945e23e546c95a.svg").exists()


def test_percent_char_rendering():
def test_percent_char_rendering(config):
Tex(r"\%")
assert Path(config.media_dir, "Tex", "4a583af4d19a3adf.tex").exists()

Expand Down Expand Up @@ -194,33 +194,33 @@ def test_error_in_nested_context(capsys):
\end{align}
"""

with pytest.raises(ValueError) as err:
with pytest.raises(ValueError):
Tex(invalid_tex)

stdout = str(capsys.readouterr().out)
# validate useless context is not included
assert r"\begin{frame}" not in stdout


def test_tempconfig_resetting_tex_template():
def test_tempconfig_resetting_tex_template(config):
my_template = TexTemplate()
my_template.preamble = "Custom preamble!"
tex_template_config_value = config.tex_template
with tempconfig({"tex_template": my_template}):
assert config.tex_template.preamble == "Custom preamble!"

assert config.tex_template.preamble != "Custom preamble!"


def test_tex_garbage_collection(tmpdir, monkeypatch):
def test_tex_garbage_collection(tmpdir, monkeypatch, config):
monkeypatch.chdir(tmpdir)
Path(tmpdir, "media").mkdir()
config.media_dir = "media"

with tempconfig({"media_dir": "media"}):
tex_without_log = Tex("Hello World!") # d771330b76d29ffb.tex
assert Path("media", "Tex", "d771330b76d29ffb.tex").exists()
assert not Path("media", "Tex", "d771330b76d29ffb.log").exists()
tex_without_log = Tex("Hello World!") # d771330b76d29ffb.tex
assert Path("media", "Tex", "d771330b76d29ffb.tex").exists()
assert not Path("media", "Tex", "d771330b76d29ffb.log").exists()

config.no_latex_cleanup = True

with tempconfig({"media_dir": "media", "no_latex_cleanup": True}):
tex_with_log = Tex("Hello World, again!") # da27670a37b08799.tex
assert Path("media", "Tex", "da27670a37b08799.log").exists()
tex_with_log = Tex("Hello World, again!") # da27670a37b08799.tex
assert Path("media", "Tex", "da27670a37b08799.log").exists()
172 changes: 84 additions & 88 deletions tests/module/scene/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,104 +4,100 @@

import pytest

from manim import Circle, FadeIn, Group, Mobject, Scene, Square, tempconfig
from manim import Circle, FadeIn, Group, Mobject, Scene, Square
from manim.animation.animation import Wait


def test_scene_add_remove():
with tempconfig({"dry_run": True}):
scene = Scene()
assert len(scene.mobjects) == 0
scene.add(Mobject())
assert len(scene.mobjects) == 1
scene.add(*(Mobject() for _ in range(10)))
assert len(scene.mobjects) == 11

# Check that adding a mobject twice does not actually add it twice
repeated = Mobject()
scene.add(repeated)
assert len(scene.mobjects) == 12
scene.add(repeated)
assert len(scene.mobjects) == 12

# Check that Scene.add() returns the Scene (for chained calls)
assert scene.add(Mobject()) is scene
to_remove = Mobject()
scene = Scene()
scene.add(to_remove)
scene.add(*(Mobject() for _ in range(10)))
assert len(scene.mobjects) == 11
scene.remove(to_remove)
assert len(scene.mobjects) == 10
scene.remove(to_remove)
assert len(scene.mobjects) == 10

# Check that Scene.remove() returns the instance (for chained calls)
assert scene.add(Mobject()) is scene


def test_scene_time():
with tempconfig({"dry_run": True}):
scene = Scene()
assert scene.renderer.time == 0
scene.wait(2)
assert scene.renderer.time == 2
scene.play(FadeIn(Circle()), run_time=0.5)
assert pytest.approx(scene.renderer.time) == 2.5
scene.renderer._original_skipping_status = True
scene.play(FadeIn(Square()), run_time=5) # this animation gets skipped.
assert pytest.approx(scene.renderer.time) == 7.5


def test_subcaption():
with tempconfig({"dry_run": True}):
scene = Scene()
scene.add_subcaption("Testing add_subcaption", duration=1, offset=0)
scene.wait()
scene.play(
Wait(),
run_time=2,
subcaption="Testing Scene.play subcaption interface",
subcaption_duration=1.5,
subcaption_offset=0.5,
)
subcaptions = scene.renderer.file_writer.subcaptions
assert len(subcaptions) == 2
assert subcaptions[0].start == datetime.timedelta(seconds=0)
assert subcaptions[0].end == datetime.timedelta(seconds=1)
assert subcaptions[0].content == "Testing add_subcaption"
assert subcaptions[1].start == datetime.timedelta(seconds=1.5)
assert subcaptions[1].end == datetime.timedelta(seconds=3)
assert subcaptions[1].content == "Testing Scene.play subcaption interface"


def test_replace():
def test_scene_add_remove(dry_run):
scene = Scene()
assert len(scene.mobjects) == 0
scene.add(Mobject())
assert len(scene.mobjects) == 1
scene.add(*(Mobject() for _ in range(10)))
assert len(scene.mobjects) == 11

# Check that adding a mobject twice does not actually add it twice
repeated = Mobject()
scene.add(repeated)
assert len(scene.mobjects) == 12
scene.add(repeated)
assert len(scene.mobjects) == 12

# Check that Scene.add() returns the Scene (for chained calls)
assert scene.add(Mobject()) is scene
to_remove = Mobject()
scene = Scene()
scene.add(to_remove)
scene.add(*(Mobject() for _ in range(10)))
assert len(scene.mobjects) == 11
scene.remove(to_remove)
assert len(scene.mobjects) == 10
scene.remove(to_remove)
assert len(scene.mobjects) == 10

# Check that Scene.remove() returns the instance (for chained calls)
assert scene.add(Mobject()) is scene


def test_scene_time(dry_run):
scene = Scene()
assert scene.renderer.time == 0
scene.wait(2)
assert scene.renderer.time == 2
scene.play(FadeIn(Circle()), run_time=0.5)
assert pytest.approx(scene.renderer.time) == 2.5
scene.renderer._original_skipping_status = True
scene.play(FadeIn(Square()), run_time=5) # this animation gets skipped.
assert pytest.approx(scene.renderer.time) == 7.5


def test_subcaption(dry_run):
scene = Scene()
scene.add_subcaption("Testing add_subcaption", duration=1, offset=0)
scene.wait()
scene.play(
Wait(),
run_time=2,
subcaption="Testing Scene.play subcaption interface",
subcaption_duration=1.5,
subcaption_offset=0.5,
)
subcaptions = scene.renderer.file_writer.subcaptions
assert len(subcaptions) == 2
assert subcaptions[0].start == datetime.timedelta(seconds=0)
assert subcaptions[0].end == datetime.timedelta(seconds=1)
assert subcaptions[0].content == "Testing add_subcaption"
assert subcaptions[1].start == datetime.timedelta(seconds=1.5)
assert subcaptions[1].end == datetime.timedelta(seconds=3)
assert subcaptions[1].content == "Testing Scene.play subcaption interface"


def test_replace(dry_run):
def assert_names(mobjs, names):
assert len(mobjs) == len(names)
for i in range(0, len(mobjs)):
assert mobjs[i].name == names[i]

with tempconfig({"dry_run": True}):
scene = Scene()
scene = Scene()

first = Mobject(name="first")
second = Mobject(name="second")
third = Mobject(name="third")
fourth = Mobject(name="fourth")
first = Mobject(name="first")
second = Mobject(name="second")
third = Mobject(name="third")
fourth = Mobject(name="fourth")

scene.add(first)
scene.add(Group(second, third, name="group"))
scene.add(fourth)
assert_names(scene.mobjects, ["first", "group", "fourth"])
assert_names(scene.mobjects[1], ["second", "third"])
scene.add(first)
scene.add(Group(second, third, name="group"))
scene.add(fourth)
assert_names(scene.mobjects, ["first", "group", "fourth"])
assert_names(scene.mobjects[1], ["second", "third"])

alpha = Mobject(name="alpha")
beta = Mobject(name="beta")
alpha = Mobject(name="alpha")
beta = Mobject(name="beta")

scene.replace(first, alpha)
assert_names(scene.mobjects, ["alpha", "group", "fourth"])
assert_names(scene.mobjects[1], ["second", "third"])
scene.replace(first, alpha)
assert_names(scene.mobjects, ["alpha", "group", "fourth"])
assert_names(scene.mobjects[1], ["second", "third"])

scene.replace(second, beta)
assert_names(scene.mobjects, ["alpha", "group", "fourth"])
assert_names(scene.mobjects[1], ["beta", "third"])
scene.replace(second, beta)
assert_names(scene.mobjects, ["alpha", "group", "fourth"])
assert_names(scene.mobjects[1], ["beta", "third"])
Loading

0 comments on commit 926e962

Please sign in to comment.