def test_vmob_add_to_back(): """Test the Mobject add_to_back method.""" a = VMobject() b = Line() c = "text" with pytest.raises(ValueError): # Mobject cannot contain self a.add_to_back(a) with pytest.raises(TypeError): # All submobjects must be of type Mobject a.add_to_back(c) # No submobject gets added twice a.add_to_back(b) a.add_to_back(b, b) assert len(a.submobjects) == 1 a.submobjects.clear() a.add_to_back(b, b, b) a.add_to_back(b, b) assert len(a.submobjects) == 1 a.submobjects.clear() # Make sure the ordering has not changed o1, o2, o3 = Square(), Line(), Circle() a.add_to_back(o1, o2, o3) assert a.submobjects.pop() == o3 assert a.submobjects.pop() == o2 assert a.submobjects.pop() == o1
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_hash_consistency(): def assert_two_objects_produce_same_hash(obj1, obj2, debug=False): """ When debug is True, if the hashes differ an assertion comparing (element-wise) the two objects will be raised, and pytest will display a nice difference summary making it easier to debug. """ json1 = hashing.get_json(obj1) hashing._Memoizer.reset_already_processed() json2 = hashing.get_json(obj2) hashing._Memoizer.reset_already_processed() hash1 = crc32(repr(json1).encode()) hash2 = crc32(repr(json2).encode()) if hash1 != hash2 and debug: dict1 = json.loads(json1) dict2 = json.loads(json2) assert dict1 == dict2 assert hash1 == hash2, f"{obj1} and {obj2} have different hashes." assert_two_objects_produce_same_hash(Square(), Square()) s = Square() assert_two_objects_produce_same_hash(s, s.copy())
def test_fixed_mobjects(): scene = ThreeDScene() s = Square() c = Circle() scene.add_fixed_in_frame_mobjects(s, c) assert set(scene.mobjects) == {s, c} assert set(scene.camera.fixed_in_frame_mobjects) == {s, c} scene.remove_fixed_in_frame_mobjects(s) assert set(scene.mobjects) == {s, c} assert set(scene.camera.fixed_in_frame_mobjects) == {c} scene.add_fixed_orientation_mobjects(s) assert set(scene.camera.fixed_orientation_mobjects) == {s} scene.remove_fixed_orientation_mobjects(s) assert len(scene.camera.fixed_orientation_mobjects) == 0
def test_vmobject_different_num_points_and_submobjects_become(): a = Square() b = VGroup(Circle(), Square()) a.become(b) np.testing.assert_array_equal(a.points, b.points) assert len(a.submobjects) == len(b.submobjects)
def test_vmobject_same_num_submobjects_become(): a = Square() b = RegularPolygon(n=6) a.become(b) np.testing.assert_array_equal(a.points, b.points) assert len(a.submobjects) == len(b.submobjects)
def test_vmobject_same_points_become(): a = Square() b = Circle() a.become(b) np.testing.assert_array_equal(a.points, b.points) assert len(a.submobjects) == len(b.submobjects)
def construct(self): self.add(Square()) self.wait(1)
def test_intersection_3_mobjects(scene): a = Square() b = Circle().move_to([0.2, 0.2, 0]) c = Triangle() i = Intersection(a, b, c, fill_opacity=0.5, color=BLUE) scene.add(a, b, c, i)
def test_exclusion(scene): a = Square() b = Circle().move_to([0.3, 0.2, 0.0]) ex = Exclusion(a, b).next_to(a) scene.add(a, b, ex)
def test_difference(scene): a = Square() b = Circle().move_to([0.2, 0.3, 0.0]) di = Difference(a, b).next_to(b) scene.add(a, b, di)
def test_intersection(scene): a = Square() b = Circle().move_to([0.3, 0.3, 0.0]) i = Intersection(a, b).next_to(b) scene.add(a, b, i)
def test_union(scene): a = Square() b = Circle().move_to([0.2, 0.2, 0.0]) c = Rectangle() un = Union(a, b, c).next_to(b) scene.add(a, b, c, un)
def construct(self): self.add(Square()) self.add(Text("Prepare for unforeseen consequencesλ")) self.add(Tex(r"$\lambda$")) self.wait(1)
a = _BooleanOps() result = a._convert_2d_to_3d_array(test_input) assert len(result) == len(expected) for i in range(len(result)): assert (result[i] == expected[i]).all() def test_convert_2d_to_3d_array_zdim(): a = _BooleanOps() result = a._convert_2d_to_3d_array([(1.0, 2.0)], z_dim=1.0) assert (result[0] == np.array([1.0, 2.0, 1.0])).all() @pytest.mark.parametrize( "test_input", [ Square(), Circle(), Square(side_length=4), Circle(radius=3), ], ) def test_vmobject_to_skia_path_and_inverse(test_input): a = _BooleanOps() path = a._convert_vmobject_to_skia_path(test_input) assert len(list(path.segments)) > 1 new_vmobject = a._convert_skia_path_to_vmobject(path) # for some reason there is an extra 4 points in new vmobject than original np.testing.assert_allclose(new_vmobject.points[:-4], test_input.points)
def test_movingcamera_auto_zoom(): camera = MovingCamera() square = Square() margin = 0.5 camera.auto_zoom([square], margin=margin, animate=False) assert camera.frame.height == square.height + margin