Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Check for add in VGroup (#304)

azarzadavila429120a173b3
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index 805c23000..129c4b294 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -846,11 +846,30 @@ class VMobject(Mobject):
class VGroup(VMobject):
def __init__(self, *vmobjects, **kwargs):
- if not all([isinstance(m, VMobject) for m in vmobjects]):
- raise Exception("All submobjects must be of type VMobject")
VMobject.__init__(self, **kwargs)
self.add(*vmobjects)
+ def add(self, *vmobjects):
+ """Checks if all passed elements are an instance of VMobject and then add them to submobjects
+
+ Parameters
+ ----------
+ vmobjects : :class:`~.VMobject`
+ List of VMobject to add
+
+ Returns
+ -------
+ None
+
+ Raises
+ ------
+ TypeError
+ If one element of the list is not an instance of VMobject
+ """
+ if not all(isinstance(m, VMobject) for m in vmobjects):
+ raise TypeError("All submobjects must be of type VMobject")
+ super().add(*vmobjects)
+
class VDict(VMobject):
"""A VGroup-like class, also offering submobject access by
diff --git a/tests/test_vectorized_mobject.py b/tests/test_vectorized_mobject.py
new file mode 100644
index 000000000..284db64b9
--- /dev/null
+++ b/tests/test_vectorized_mobject.py
@@ -0,0 +1,31 @@
+import pytest
+from manim import Mobject, VMobject, VGroup
+
+
+def test_vgroup_init():
+ """Test the VGroup instantiation."""
+ VGroup()
+ VGroup(VMobject())
+ VGroup(VMobject(), VMobject())
+ with pytest.raises(TypeError):
+ VGroup(Mobject())
+ with pytest.raises(TypeError):
+ VGroup(Mobject(), Mobject())
+
+
+def test_vgroup_add():
+ """Test the VGroup add method."""
+ obj = VGroup()
+ assert len(obj.submobjects) == 0
+ obj.add(VMobject())
+ assert len(obj.submobjects) == 1
+ with pytest.raises(TypeError):
+ obj.add(Mobject())
+ assert len(obj.submobjects) == 1
+ with pytest.raises(TypeError):
+ # If only one of the added object is not an instance of VMobject, none of them should be added
+ obj.add(VMobject(), Mobject())
+ assert len(obj.submobjects) == 1
+ with pytest.raises(Exception): # TODO change this to ValueError once #307 is merged
+ # a Mobject cannot contain itself
+ obj.add(obj)