Add OpenGLMobject tests (#2080)

This commit is contained in:
Ryan McCauley 2021-09-28 00:47:06 +01:00 committed by GitHub
commit a10a7d87c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 2 deletions

View file

@ -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:

View file

@ -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)
@pytest.fixture
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"

0
tests/opengl/__init__.py Normal file
View file

View file

@ -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

View file

@ -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