Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Add OpenGLMobject tests (#2080)

Ryan McCauleya10a7d87c8fd
diff --git a/manim/mobject/opengl_mobject.py b/manim/mobject/opengl_mobject.py
index 30f59d9e5..39c1f63ef 100644
--- a/manim/mobject/opengl_mobject.py
+++ b/manim/mobject/opengl_mobject.py
@@ -419,8 +419,10 @@ class OpenGLMobject:
mobjects[0].parent = self
if self in mobjects:
- raise Exception("Mobject cannot contain self")
+ raise ValueError("OpenGLMobject cannot contain self")
for mobject in mobjects:
+ if not isinstance(mobject, OpenGLMobject):
+ raise TypeError("All submobjects must be of type OpenGLMobject")
if mobject not in self.submobjects:
self.submobjects.append(mobject)
if self not in mobject.parents:
diff --git a/tests/conftest.py b/tests/conftest.py
index 7d7f6f95a..09b5d2c97 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,8 +1,11 @@
import os
import sys
+from pathlib import Path
import pytest
+from manim import config, tempconfig
+
def pytest_addoption(parser):
parser.addoption(
@@ -57,3 +60,13 @@ def reset_cfg_file():
yield
with open(cfgfilepath, "w") as cfgfile:
cfgfile.write(original)
+
+
+def using_opengl_renderer():
+ """Standard fixture for running with opengl that makes tests use a standard_config.cfg with a temp dir."""
+ with tempconfig({"renderer": "opengl"}):
+ yield
+ # as a special case needed to manually revert back to cairo
+ # due to side effects of setting the renderer
+ config.renderer = "cairo"
diff --git a/tests/opengl/__init__.py b/tests/opengl/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/opengl/test_opengl_mobject.py b/tests/opengl/test_opengl_mobject.py
new file mode 100644
index 000000000..70471750a
--- /dev/null
+++ b/tests/opengl/test_opengl_mobject.py
@@ -0,0 +1,50 @@
+import pytest
+
+from manim import config
+from manim.mobject.opengl_mobject import OpenGLMobject
+
+
+def test_opengl_mobject_add(using_opengl_renderer):
+ """Test OpenGLMobject.add()."""
+ """Call this function with a Container instance to test its add() method."""
+ # check that obj.submobjects is updated correctly
+ obj = OpenGLMobject()
+ assert len(obj.submobjects) == 0
+ obj.add(OpenGLMobject())
+ assert len(obj.submobjects) == 1
+ obj.add(*(OpenGLMobject() for _ in range(10)))
+ assert len(obj.submobjects) == 11
+
+ # check that adding a OpenGLMobject twice does not actually add it twice
+ repeated = OpenGLMobject()
+ obj.add(repeated)
+ assert len(obj.submobjects) == 12
+ obj.add(repeated)
+ assert len(obj.submobjects) == 12
+
+ # check that OpenGLMobject.add() returns the OpenGLMobject (for chained calls)
+ assert obj.add(OpenGLMobject()) is obj
+ obj = OpenGLMobject()
+
+ # a OpenGLMobject cannot contain itself
+ with pytest.raises(ValueError):
+ obj.add(obj)
+
+ # can only add OpenGLMobjects
+ with pytest.raises(TypeError):
+ obj.add("foo")
+
+
+def test_opengl_mobject_remove(using_opengl_renderer):
+ """Test OpenGLMobject.remove()."""
+ obj = OpenGLMobject()
+ to_remove = OpenGLMobject()
+ obj.add(to_remove)
+ obj.add(*(OpenGLMobject() for _ in range(10)))
+ assert len(obj.submobjects) == 11
+ obj.remove(to_remove)
+ assert len(obj.submobjects) == 10
+ obj.remove(to_remove)
+ assert len(obj.submobjects) == 10
+
+ assert obj.remove(OpenGLMobject()) is obj
diff --git a/tests/test_scene_rendering/standard_config.cfg b/tests/standard_config.cfg
similarity index 100%
rename from tests/test_scene_rendering/standard_config.cfg
rename to tests/standard_config.cfg
diff --git a/tests/test_scene_rendering/conftest.py b/tests/test_scene_rendering/conftest.py
index 7aff314e5..8484e9868 100644
--- a/tests/test_scene_rendering/conftest.py
+++ b/tests/test_scene_rendering/conftest.py
@@ -18,7 +18,9 @@ def simple_scenes_path():
@pytest.fixture
def using_temp_config(tmpdir):
"""Standard fixture that makes tests use a standard_config.cfg with a temp dir."""
- with tempconfig(config.digest_file(Path(__file__).parent / "standard_config.cfg")):
+ with tempconfig(
+ config.digest_file(Path(__file__).parent.parent / "standard_config.cfg"),
+ ):
config.media_dir = tmpdir
yield