def test_is_off_screen(): m = Mobject() m.points = np.array([[3, 4, 0]]) assert not m.is_off_screen() m.points = np.array([[0, const.FRAME_Y_RADIUS + 1, 0]]) m.points = np.array([[const.FRAME_X_RADIUS + 1, 0, 0]]) assert m.is_off_screen()
def test_align_to(): m1_points = np.random.rand(1, 3) m1 = Mobject() m1.points = m1_points m2_points = np.random.rand(1, 3) m2 = Mobject() m2.points = m2_points m1.align_to(m2) assert m1.get_critical_point(const.UP)[1] == m2.get_critical_point( const.UP)[1]
def test_shift(): submob = Mobject() m = Mobject(submob) mob_points = np.random.rand(10, 3) submob_points = np.random.rand(5, 3) m.points = mob_points submob.points = submob_points m.shift(2 * const.RIGHT - 1 * const.UP) assert m.points == approx(mob_points + np.array([2, -1, 0])) assert submob.points == approx(submob_points + np.array([2, -1, 0]))
def test_repeat(): m_points = np.random.rand(10, 3) s_points = np.random.rand(10, 3) s = Mobject() s.points = s_points.copy() m = Mobject(s) m.points = m_points.copy() m.repeat(3) m_points_expected = np.tile(m_points, (3, 1)) s_points_expected = np.tile(s_points, (3, 1)) assert np.allclose(m.points, m_points_expected) assert np.allclose(s.points, s_points_expected)
def test_apply_to_family(): submob1 = Mobject() submob1.points = np.zeros((3, 3)) submob2 = Mobject() submob2.points = np.zeros((3, 3)) m = Mobject(submob1, submob2) def func(mob): mob.points = np.ones((3, 3)) m.apply_to_family(func) for mob in m.family_members_with_points(): assert mob.points == approx(np.ones((3, 3)))
def test_align_on_border(mocker): mock_dir = np.random.rand(3) mock_point_to_align = np.random.rand(3) mock_buff = np.random.rand(3) mock_offset = np.random.rand(3) mocker.patch.object(mobject.mobject.Mobject, 'get_critical_point', return_value=mock_point_to_align) mocker.patch.object(mobject.mobject.Mobject, 'shift') m = Mobject() m.points = np.random.rand(10, 3) m.align_on_border(mock_dir, buff=mock_buff, initial_offset=mock_offset) mock_target_point = \ np.sign(mock_dir) * (const.FRAME_X_RADIUS, const.FRAME_Y_RADIUS, 0) mock_shift_val = mock_target_point - mock_point_to_align - mock_buff * np.array( mock_dir) mock_shift_val = mock_shift_val * abs(np.sign(mock_dir)) m.get_critical_point.assert_called() args, kwargs = m.get_critical_point.call_args assert np.allclose(args[0], mock_dir) assert kwargs == {} m.shift.assert_called() args, kwargs = m.shift.call_args assert np.allclose(args[0], mock_shift_val + mock_offset) assert kwargs == {}
def test_reverse_points(): points = np.random.rand(10, 3) m = Mobject() m.points = points.copy() m.reverse_points() expected = points.copy() assert (np.allclose(m.points, np.flip(expected, axis=0)))
def test_shift_onto_screen(): m = Mobject() m.points = np.array( [[const.FRAME_X_RADIUS + 1, const.FRAME_Y_RADIUS + 1, 0]]) m.shift_onto_screen() assert np.dot(m.get_top( ), const.UP) == const.FRAME_Y_RADIUS - const.DEFAULT_MOBJECT_TO_EDGE_BUFFER assert np.dot( m.get_right(), const.RIGHT ) == const.FRAME_X_RADIUS - const.DEFAULT_MOBJECT_TO_EDGE_BUFFER
def test_flip(mocker): mocker.spy(mobject.mobject.Mobject, "rotate") m = Mobject() m.points = np.array([ [1, 1, 0], [-1, -1, 0], [2, 2, 0], [-2, -2, 0], ]) m.flip() m.rotate.assert_called_once_with(m, const.TAU / 2, const.UP) expected = np.array([[-1, 1, 0], [1, -1, 0], [-2, 2, 0], [2, -2, 0]]) assert (np.allclose(m.points, expected))
def test_wag(): points = np.random.rand(10, 3) m = Mobject() m.points = points.copy() m.wag() expected = points.copy() alphas = np.dot(expected, np.transpose(const.DOWN)) alphas -= min(alphas) alphas /= max(alphas) # alphas = alphas**wag_factor expected += np.dot(alphas.reshape((len(alphas), 1)), np.array(const.RIGHT).reshape((1, m.dim))) assert (np.allclose(m.points, expected))
def test_apply_matrix(): points = np.random.rand(10, 3) matrix = np.random.rand(3, 3) m = Mobject() m.points = points.copy() m.apply_matrix(matrix) expected = points.copy() expected -= const.ORIGIN expected = np.dot(points, matrix.T) expected += const.ORIGIN assert (np.allclose(m.points, expected)) m.points = points about_point = np.random.rand(1, 3) expected = points.copy() expected -= about_point expected = np.dot(expected, matrix.T) expected += about_point m.apply_matrix(matrix, about_point=about_point) assert (np.allclose(m.points, expected))
def test_replace(): m1_points = np.random.rand(10, 3) m1 = Mobject() m1.points = m1_points.copy() m2_points = np.random.rand(10, 3) m2 = Mobject() m2.points = m2_points.copy() def get_ratio(mob): return mob.length_over_dim(0) / mob.length_over_dim(1) m1_orig_ratio = get_ratio(m1) assert m1.length_over_dim(0) != m2.length_over_dim(0) assert (not np.allclose(m1.get_center(), m2.get_center())) m1.replace(m2) assert get_ratio(m1) == approx(m1_orig_ratio) assert m1.length_over_dim(0) == approx(m2.length_over_dim(0)) assert np.allclose(m1.get_center(), m2.get_center()) m1.points = m1_points.copy() m1.replace(m2, stretch=True) assert get_ratio(m1) != approx(m1_orig_ratio) assert m1.length_over_dim(0) == approx(m2.length_over_dim(0)) assert np.allclose(m1.get_center(), m2.get_center())
def test_rescale_to_fit(mocker): mocker.spy(mobject.mobject.Mobject, 'length_over_dim') mocker.patch.object(mobject.mobject.Mobject, 'stretch') mocker.patch.object(mobject.mobject.Mobject, 'scale') mock_dim = 1 mock_length = 3 points = np.random.rand(10, 3) m = Mobject() m.points = points m.rescale_to_fit(mock_length, mock_dim, stretch=True) m.length_over_dim.assert_called_once() assert m.length_over_dim.call_args == call(m, mock_dim) length = m.length_over_dim(mock_dim) m.stretch.assert_called_once_with(mock_length / length, mock_dim)
def test_stretch(mocker): mocker.spy(mobject.mobject.Mobject, "apply_points_function_about_point") m = Mobject() m.points = np.array([ [0, 1, 0], [0, 0, 0], [0, -1, 0], ]) m.stretch(3, 1) m.apply_points_function_about_point.assert_called_once() args, kwargs = m.apply_points_function_about_point.call_args assert args[0] is m assert callable(args[1]) assert (inspect.getsource(args[1]).strip() == "def func(points):\n" " points[:, dim] *= factor\n" " return points") assert kwargs == {} assert (np.allclose(m.points, [[0, 3, 0], [0, 0, 0], [0, -3, 0]]))
def test_apply_points_function_about_point(mocker): mocker.patch.object( mobject.mobject.Mobject, 'get_critical_point', return_value=const.ORIGIN, ) m_points = np.random.rand(10, 3) func_return_points = np.random.rand(10, 3) mock_func = mocker.Mock(return_value=func_return_points) m = Mobject() m.points = m_points.copy() m.apply_points_function_about_point(mock_func, about_edge=const.ORIGIN) m.get_critical_point.assert_called_once() args, kwargs = m.get_critical_point.call_args assert (np.allclose(args, const.ORIGIN) and kwargs == {}) mock_func.assert_called_once() args, kwargs = mock_func.call_args assert (np.allclose(args, m_points) and kwargs == {})
def get_random_mobject(num_points=10, depth=False): m = Mobject() m.points = np.random.rand(num_points, 3) if not depth: m.points[:, 2] = 0 return m