예제 #1
0
def test_vgroup_remove_dunder(using_opengl_renderer):
    """Test the VGroup __sub__ magic method."""
    a = OpenGLVMobject()
    c = OpenGLVMobject()
    b = VGroup(c)
    obj = VGroup(a, b)
    assert len(obj.submobjects) == 2
    assert len(b.submobjects) == 1
    assert len(obj - a) == 1
    assert len(obj.submobjects) == 2
    obj -= a
    b -= c
    assert len(obj.submobjects) == 1
    assert len(b.submobjects) == 0
    obj -= b
    assert len(obj.submobjects) == 0
예제 #2
0
def test_set_color(using_opengl_renderer):
    m = OpenGLMobject()
    assert m.color.hex == "#fff"
    np.alltrue(m.rgbas == np.array((0.0, 0.0, 0.0, 1.0)))

    m.set_color(BLACK)
    assert m.color.hex == "#000"
    np.alltrue(m.rgbas == np.array((1.0, 1.0, 1.0, 1.0)))

    m.set_color(PURE_GREEN, opacity=0.5)
    assert m.color.hex == "#0f0"
    np.alltrue(m.rgbas == np.array((0.0, 1.0, 0.0, 0.5)))

    m = OpenGLVMobject()
    assert m.color.hex == "#fff"
    np.alltrue(m.fill_rgba == np.array((0.0, 0.0, 0.0, 1.0)))
    np.alltrue(m.stroke_rgba == np.array((0.0, 0.0, 0.0, 1.0)))

    m.set_color(BLACK)
    assert m.color.hex == "#000"
    np.alltrue(m.fill_rgba == np.array((1.0, 1.0, 1.0, 1.0)))
    np.alltrue(m.stroke_rgba == np.array((1.0, 1.0, 1.0, 1.0)))

    m.set_color(PURE_GREEN, opacity=0.5)
    assert m.color.hex == "#0f0"
    np.alltrue(m.fill_rgba == np.array((0.0, 1.0, 0.0, 0.5)))
    np.alltrue(m.stroke_rgba == np.array((0.0, 1.0, 0.0, 0.5)))
예제 #3
0
def test_vgroup_add(using_opengl_renderer):
    """Test the VGroup add method."""
    obj = VGroup()
    assert len(obj.submobjects) == 0
    obj.add(OpenGLVMobject())
    assert len(obj.submobjects) == 1
    with pytest.raises(TypeError):
        obj.add(OpenGLMobject())
    assert len(obj.submobjects) == 1
    with pytest.raises(TypeError):
        # If only one of the added object is not an instance of OpenGLVMobject, none of them should be added
        obj.add(OpenGLVMobject(), OpenGLMobject())
    assert len(obj.submobjects) == 1
    with pytest.raises(ValueError):
        # a OpenGLMobject cannot contain itself
        obj.add(obj)
예제 #4
0
def test_vdict_remove(using_opengl_renderer):
    """Test the VDict remove method."""
    obj = VDict([("a", OpenGLVMobject())])
    assert len(obj.submob_dict) == 1
    obj.remove("a")
    assert len(obj.submob_dict) == 0
    with pytest.raises(KeyError):
        obj.remove("a")
예제 #5
0
def test_vdict_add(using_opengl_renderer):
    """Test the VDict add method."""
    obj = VDict()
    assert len(obj.submob_dict) == 0
    obj.add([("a", OpenGLVMobject())])
    assert len(obj.submob_dict) == 1
    with pytest.raises(TypeError):
        obj.add([("b", OpenGLMobject())])
예제 #6
0
def test_set_fill(using_opengl_renderer):
    m = OpenGLMobject()
    assert m.color.hex == "#fff"
    m.set_color(BLACK)
    assert m.color.hex == "#000"

    m = OpenGLVMobject()
    assert m.color.hex == "#fff"
    m.set_color(BLACK)
    assert m.color.hex == "#000"
예제 #7
0
def test_set_stroke_handles_lists_of_color_objects(using_opengl_renderer):
    m = OpenGLVMobject()
    assert m.stroke_color.hex == "#fff"
    m.set_stroke([Color(PURE_BLUE), Color(PURE_GREEN), Color(PURE_RED)])
    assert m.get_stroke_colors()[0].hex == "#00f"
    assert m.get_stroke_colors()[1].hex == "#0f0"
    assert m.get_stroke_colors()[2].hex == "#f00"
예제 #8
0
def test_set_stroke_handles_lists_of_strs(using_opengl_renderer):
    m = OpenGLVMobject()
    assert m.stroke_color.hex == "#fff"
    m.set_stroke([BLACK, BLUE, GREEN])
    assert m.get_stroke_colors()[0] == Color(BLACK)
    assert m.get_stroke_colors()[1] == Color(BLUE)
    assert m.get_stroke_colors()[2] == Color(GREEN)
예제 #9
0
def test_vdict_init(using_opengl_renderer):
    """Test the VDict instantiation."""
    # Test empty VDict
    VDict()
    # Test VDict made from list of pairs
    VDict([("a", OpenGLVMobject()), ("b", OpenGLVMobject()),
           ("c", OpenGLVMobject())])
    # Test VDict made from a python dict
    VDict({
        "a": OpenGLVMobject(),
        "b": OpenGLVMobject(),
        "c": OpenGLVMobject()
    })
    # Test VDict made using zip
    VDict(
        zip(["a", "b", "c"],
            [OpenGLVMobject(),
             OpenGLVMobject(),
             OpenGLVMobject()]))
    # If the value is of type OpenGLMobject, must raise a TypeError
    with pytest.raises(TypeError):
        VDict({"a": OpenGLMobject()})
예제 #10
0
def test_stroke_props_in_ctor(using_opengl_renderer):
    m = OpenGLVMobject(stroke_color=C.ORANGE, stroke_width=10)
    assert m.stroke_color == Color(C.ORANGE)
    assert m.stroke_width == 10
예제 #11
0
def test_set_stroke(using_opengl_renderer):
    m = OpenGLVMobject()
    m.set_stroke(color=C.ORANGE, width=2, opacity=0.8)
    assert m.stroke_width == 2
    assert m.stroke_opacity == 0.8
    assert m.stroke_color == Color(C.ORANGE)
예제 #12
0
def test_vgroup_item_assignment_only_allows_vmobjects(using_opengl_renderer):
    """Test VGroup item-assignment raises TypeError when invalid type is passed"""
    vgroup = VGroup(OpenGLVMobject())
    with pytest.raises(TypeError,
                       match="All submobjects must be of type VMobject"):
        vgroup[0] = "invalid object"
예제 #13
0
def test_vmob_add_to_back(using_opengl_renderer):
    """Test the OpenGLMobject add_to_back method."""
    a = OpenGLVMobject()
    b = Line()
    c = "text"
    with pytest.raises(ValueError):
        # OpenGLMobject cannot contain self
        a.add_to_back(a)
    with pytest.raises(TypeError):
        # All submobjects must be of type OpenGLMobject
        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
예제 #14
0
def test_opengl_vmobject_point_from_propotion(using_opengl_renderer):
    obj = OpenGLVMobject()

    # One long line, one short line
    obj.set_points_as_corners([
        np.array([0, 0, 0]),
        np.array([4, 0, 0]),
        np.array([4, 2, 0]),
    ], )

    # Total length of 6, so halfway along the object
    # would be at length 3, which lands in the first, long line.
    assert np.all(obj.point_from_proportion(0.5) == np.array([3, 0, 0]))

    with pytest.raises(ValueError, match="between 0 and 1"):
        obj.point_from_proportion(2)

    obj.clear_points()
    with pytest.raises(Exception, match="with no points"):
        obj.point_from_proportion(0)