Skip to content

Commit 57e96f4

Browse files
Change tests to use the config fixture
1 parent 96d8db8 commit 57e96f4

19 files changed

+396
-393
lines changed

tests/helpers/graphical_units.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
import numpy as np
99

10-
from manim import config, logger
10+
from manim import logger
1111
from manim.scene.scene import Scene
1212

1313

14-
def set_test_scene(scene_object: type[Scene], module_name: str):
14+
def set_test_scene(scene_object: type[Scene], module_name: str, config):
1515
"""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
1616
when setting up tests. Please refer to the wiki.
1717
@@ -46,7 +46,7 @@ def set_test_scene(scene_object: type[Scene], module_name: str):
4646

4747
assert not np.all(
4848
data == np.array([0, 0, 0, 255]),
49-
), f"Control data generated for {str(scene)} only contains empty pixels."
49+
), f"Control data generated for {scene!s} only contains empty pixels."
5050
assert data.shape == (480, 854, 4)
5151
tests_directory = Path(__file__).absolute().parent.parent
5252
path_control_data = Path(tests_directory) / "control_data" / "graphical_units_data"

tests/module/animation/test_animation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from manim import FadeIn, Scene, config
5+
from manim import FadeIn, Scene
66

77

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

1717

18-
def test_animation_run_time_shorter_than_frame_rate(caplog):
18+
def test_animation_run_time_shorter_than_frame_rate(caplog, config):
1919
test_scene = Scene()
2020
test_scene.play(FadeIn(None, run_time=1 / (config.frame_rate + 1)))
2121
assert (

tests/module/animation/test_creation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import pytest
55

6-
from manim import AddTextLetterByLetter, Text, config
6+
from manim import AddTextLetterByLetter, Text
77

88

99
def test_non_empty_text_creation():
@@ -25,7 +25,7 @@ def test_whitespace_text_creation():
2525
AddTextLetterByLetter(Text(" "))
2626

2727

28-
def test_run_time_for_non_empty_text():
28+
def test_run_time_for_non_empty_text(config):
2929
"""Ensure the run_time is calculated correctly for non-empty text."""
3030
s = Text("Hello")
3131
run_time_per_char = 0.1

tests/module/mobject/mobject/test_copy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pathlib import Path
44

5-
from manim import BraceLabel, Mobject, config
5+
from manim import BraceLabel, Mobject
66

77

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

2020

21-
def test_bracelabel_copy(tmp_path):
21+
def test_bracelabel_copy(tmp_path, config):
2222
"""Test that a copy is a deepcopy."""
2323
# For this test to work, we need to tweak some folders temporarily
2424
original_text_dir = config["text_dir"]
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
from __future__ import annotations
22

3-
from manim import Mobject, config, tempconfig
3+
from manim import Mobject
44
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
55
from manim.mobject.opengl.opengl_mobject import OpenGLMobject
66

77

8-
def test_metaclass_registry():
8+
def test_metaclass_registry(config):
99
class SomeTestMobject(Mobject, metaclass=ConvertToOpenGL):
1010
pass
1111

1212
assert SomeTestMobject in ConvertToOpenGL._converted_classes
1313

14-
with tempconfig({"renderer": "opengl"}):
15-
assert OpenGLMobject in SomeTestMobject.__bases__
16-
assert Mobject not in SomeTestMobject.__bases__
14+
config.renderer = "opengl"
15+
assert OpenGLMobject in SomeTestMobject.__bases__
16+
assert Mobject not in SomeTestMobject.__bases__
1717

18-
config.renderer = "cairo"
19-
assert Mobject in SomeTestMobject.__bases__
20-
assert OpenGLMobject not in SomeTestMobject.__bases__
18+
config.renderer = "cairo"
19+
assert Mobject in SomeTestMobject.__bases__
20+
assert OpenGLMobject not in SomeTestMobject.__bases__

tests/module/mobject/mobject/test_set_attr.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
import numpy as np
44

5-
from manim import RendererType, config
65
from manim.constants import RIGHT
76
from manim.mobject.geometry.polygram import Square
87

98

10-
def test_Data():
11-
config.renderer = RendererType.OPENGL
9+
def test_Data(using_opengl_renderer):
1210
a = Square().move_to(RIGHT)
1311
data_bb = a.data["bounding_box"]
1412
np.testing.assert_array_equal(
@@ -39,6 +37,3 @@ def test_Data():
3937
)
4038

4139
np.testing.assert_array_equal(a.bounding_box, data_bb)
42-
config.renderer = (
43-
RendererType.CAIRO
44-
) # needs to be here or else the following cairo tests fail

tests/module/mobject/text/test_texmobject.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import numpy as np
66
import pytest
77

8-
from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, config, tempconfig
8+
from manim import MathTex, SingleStringMathTex, Tex, TexTemplate, tempconfig
99

1010

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

1515

16-
def test_SingleStringMathTex():
16+
def test_SingleStringMathTex(config):
1717
SingleStringMathTex("test")
1818
assert Path(config.media_dir, "Tex", "8ce17c7f5013209f.svg").exists()
1919

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

2929

30-
def test_tex():
30+
def test_tex(config):
3131
Tex("The horse does not eat cucumber salad.")
3232
assert Path(config.media_dir, "Tex", "c3945e23e546c95a.svg").exists()
3333

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

4747

48-
def test_percent_char_rendering():
48+
def test_percent_char_rendering(config):
4949
Tex(r"\%")
5050
assert Path(config.media_dir, "Tex", "4a583af4d19a3adf.tex").exists()
5151

@@ -194,33 +194,33 @@ def test_error_in_nested_context(capsys):
194194
\end{align}
195195
"""
196196

197-
with pytest.raises(ValueError) as err:
197+
with pytest.raises(ValueError):
198198
Tex(invalid_tex)
199199

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

204204

205-
def test_tempconfig_resetting_tex_template():
205+
def test_tempconfig_resetting_tex_template(config):
206206
my_template = TexTemplate()
207207
my_template.preamble = "Custom preamble!"
208-
tex_template_config_value = config.tex_template
209208
with tempconfig({"tex_template": my_template}):
210209
assert config.tex_template.preamble == "Custom preamble!"
211210

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

214213

215-
def test_tex_garbage_collection(tmpdir, monkeypatch):
214+
def test_tex_garbage_collection(tmpdir, monkeypatch, config):
216215
monkeypatch.chdir(tmpdir)
217216
Path(tmpdir, "media").mkdir()
217+
config.media_dir = "media"
218218

219-
with tempconfig({"media_dir": "media"}):
220-
tex_without_log = Tex("Hello World!") # d771330b76d29ffb.tex
221-
assert Path("media", "Tex", "d771330b76d29ffb.tex").exists()
222-
assert not Path("media", "Tex", "d771330b76d29ffb.log").exists()
219+
tex_without_log = Tex("Hello World!") # d771330b76d29ffb.tex
220+
assert Path("media", "Tex", "d771330b76d29ffb.tex").exists()
221+
assert not Path("media", "Tex", "d771330b76d29ffb.log").exists()
222+
223+
config.no_latex_cleanup = True
223224

224-
with tempconfig({"media_dir": "media", "no_latex_cleanup": True}):
225-
tex_with_log = Tex("Hello World, again!") # da27670a37b08799.tex
226-
assert Path("media", "Tex", "da27670a37b08799.log").exists()
225+
tex_with_log = Tex("Hello World, again!") # da27670a37b08799.tex
226+
assert Path("media", "Tex", "da27670a37b08799.log").exists()

tests/module/scene/test_scene.py

Lines changed: 84 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,104 +4,100 @@
44

55
import pytest
66

7-
from manim import Circle, FadeIn, Group, Mobject, Scene, Square, tempconfig
7+
from manim import Circle, FadeIn, Group, Mobject, Scene, Square
88
from manim.animation.animation import Wait
99

1010

11-
def test_scene_add_remove():
12-
with tempconfig({"dry_run": True}):
13-
scene = Scene()
14-
assert len(scene.mobjects) == 0
15-
scene.add(Mobject())
16-
assert len(scene.mobjects) == 1
17-
scene.add(*(Mobject() for _ in range(10)))
18-
assert len(scene.mobjects) == 11
19-
20-
# Check that adding a mobject twice does not actually add it twice
21-
repeated = Mobject()
22-
scene.add(repeated)
23-
assert len(scene.mobjects) == 12
24-
scene.add(repeated)
25-
assert len(scene.mobjects) == 12
26-
27-
# Check that Scene.add() returns the Scene (for chained calls)
28-
assert scene.add(Mobject()) is scene
29-
to_remove = Mobject()
30-
scene = Scene()
31-
scene.add(to_remove)
32-
scene.add(*(Mobject() for _ in range(10)))
33-
assert len(scene.mobjects) == 11
34-
scene.remove(to_remove)
35-
assert len(scene.mobjects) == 10
36-
scene.remove(to_remove)
37-
assert len(scene.mobjects) == 10
38-
39-
# Check that Scene.remove() returns the instance (for chained calls)
40-
assert scene.add(Mobject()) is scene
41-
42-
43-
def test_scene_time():
44-
with tempconfig({"dry_run": True}):
45-
scene = Scene()
46-
assert scene.renderer.time == 0
47-
scene.wait(2)
48-
assert scene.renderer.time == 2
49-
scene.play(FadeIn(Circle()), run_time=0.5)
50-
assert pytest.approx(scene.renderer.time) == 2.5
51-
scene.renderer._original_skipping_status = True
52-
scene.play(FadeIn(Square()), run_time=5) # this animation gets skipped.
53-
assert pytest.approx(scene.renderer.time) == 7.5
54-
55-
56-
def test_subcaption():
57-
with tempconfig({"dry_run": True}):
58-
scene = Scene()
59-
scene.add_subcaption("Testing add_subcaption", duration=1, offset=0)
60-
scene.wait()
61-
scene.play(
62-
Wait(),
63-
run_time=2,
64-
subcaption="Testing Scene.play subcaption interface",
65-
subcaption_duration=1.5,
66-
subcaption_offset=0.5,
67-
)
68-
subcaptions = scene.renderer.file_writer.subcaptions
69-
assert len(subcaptions) == 2
70-
assert subcaptions[0].start == datetime.timedelta(seconds=0)
71-
assert subcaptions[0].end == datetime.timedelta(seconds=1)
72-
assert subcaptions[0].content == "Testing add_subcaption"
73-
assert subcaptions[1].start == datetime.timedelta(seconds=1.5)
74-
assert subcaptions[1].end == datetime.timedelta(seconds=3)
75-
assert subcaptions[1].content == "Testing Scene.play subcaption interface"
76-
77-
78-
def test_replace():
11+
def test_scene_add_remove(dry_run):
12+
scene = Scene()
13+
assert len(scene.mobjects) == 0
14+
scene.add(Mobject())
15+
assert len(scene.mobjects) == 1
16+
scene.add(*(Mobject() for _ in range(10)))
17+
assert len(scene.mobjects) == 11
18+
19+
# Check that adding a mobject twice does not actually add it twice
20+
repeated = Mobject()
21+
scene.add(repeated)
22+
assert len(scene.mobjects) == 12
23+
scene.add(repeated)
24+
assert len(scene.mobjects) == 12
25+
26+
# Check that Scene.add() returns the Scene (for chained calls)
27+
assert scene.add(Mobject()) is scene
28+
to_remove = Mobject()
29+
scene = Scene()
30+
scene.add(to_remove)
31+
scene.add(*(Mobject() for _ in range(10)))
32+
assert len(scene.mobjects) == 11
33+
scene.remove(to_remove)
34+
assert len(scene.mobjects) == 10
35+
scene.remove(to_remove)
36+
assert len(scene.mobjects) == 10
37+
38+
# Check that Scene.remove() returns the instance (for chained calls)
39+
assert scene.add(Mobject()) is scene
40+
41+
42+
def test_scene_time(dry_run):
43+
scene = Scene()
44+
assert scene.renderer.time == 0
45+
scene.wait(2)
46+
assert scene.renderer.time == 2
47+
scene.play(FadeIn(Circle()), run_time=0.5)
48+
assert pytest.approx(scene.renderer.time) == 2.5
49+
scene.renderer._original_skipping_status = True
50+
scene.play(FadeIn(Square()), run_time=5) # this animation gets skipped.
51+
assert pytest.approx(scene.renderer.time) == 7.5
52+
53+
54+
def test_subcaption(dry_run):
55+
scene = Scene()
56+
scene.add_subcaption("Testing add_subcaption", duration=1, offset=0)
57+
scene.wait()
58+
scene.play(
59+
Wait(),
60+
run_time=2,
61+
subcaption="Testing Scene.play subcaption interface",
62+
subcaption_duration=1.5,
63+
subcaption_offset=0.5,
64+
)
65+
subcaptions = scene.renderer.file_writer.subcaptions
66+
assert len(subcaptions) == 2
67+
assert subcaptions[0].start == datetime.timedelta(seconds=0)
68+
assert subcaptions[0].end == datetime.timedelta(seconds=1)
69+
assert subcaptions[0].content == "Testing add_subcaption"
70+
assert subcaptions[1].start == datetime.timedelta(seconds=1.5)
71+
assert subcaptions[1].end == datetime.timedelta(seconds=3)
72+
assert subcaptions[1].content == "Testing Scene.play subcaption interface"
73+
74+
75+
def test_replace(dry_run):
7976
def assert_names(mobjs, names):
8077
assert len(mobjs) == len(names)
8178
for i in range(0, len(mobjs)):
8279
assert mobjs[i].name == names[i]
8380

84-
with tempconfig({"dry_run": True}):
85-
scene = Scene()
81+
scene = Scene()
8682

87-
first = Mobject(name="first")
88-
second = Mobject(name="second")
89-
third = Mobject(name="third")
90-
fourth = Mobject(name="fourth")
83+
first = Mobject(name="first")
84+
second = Mobject(name="second")
85+
third = Mobject(name="third")
86+
fourth = Mobject(name="fourth")
9187

92-
scene.add(first)
93-
scene.add(Group(second, third, name="group"))
94-
scene.add(fourth)
95-
assert_names(scene.mobjects, ["first", "group", "fourth"])
96-
assert_names(scene.mobjects[1], ["second", "third"])
88+
scene.add(first)
89+
scene.add(Group(second, third, name="group"))
90+
scene.add(fourth)
91+
assert_names(scene.mobjects, ["first", "group", "fourth"])
92+
assert_names(scene.mobjects[1], ["second", "third"])
9793

98-
alpha = Mobject(name="alpha")
99-
beta = Mobject(name="beta")
94+
alpha = Mobject(name="alpha")
95+
beta = Mobject(name="beta")
10096

101-
scene.replace(first, alpha)
102-
assert_names(scene.mobjects, ["alpha", "group", "fourth"])
103-
assert_names(scene.mobjects[1], ["second", "third"])
97+
scene.replace(first, alpha)
98+
assert_names(scene.mobjects, ["alpha", "group", "fourth"])
99+
assert_names(scene.mobjects[1], ["second", "third"])
104100

105-
scene.replace(second, beta)
106-
assert_names(scene.mobjects, ["alpha", "group", "fourth"])
107-
assert_names(scene.mobjects[1], ["beta", "third"])
101+
scene.replace(second, beta)
102+
assert_names(scene.mobjects, ["alpha", "group", "fourth"])
103+
assert_names(scene.mobjects[1], ["beta", "third"])

0 commit comments

Comments
 (0)