def test_base(self):
     v = Rect2(4, 3, 2, 1)
     assert type(v) == Rect2
     v2 = Rect2(1, 2, 3, 4)
     assert type(v) == Rect2
     assert v2 == Rect2(1, 2, 3, 4)
     assert v != v2
 def test_methods(self, args):
     v = Rect2()
     # Don't test methods' validity but bindings one
     field, ret_type, params = args
     assert hasattr(v, field)
     method = getattr(v, field)
     assert callable(method)
     ret = method(*params)
     assert type(ret) == ret_type
 def test_properties(self, args):
     v = Rect2()
     field, ret_type = args
     assert hasattr(v, field)
     field_val = getattr(v, field)
     assert type(field_val) == ret_type
     for val in (Vector2(), Vector2(0.1, -0.1)):
         setattr(v, field, val)
         field_val = getattr(v, field)
         assert field_val == val
Exemple #4
0
 def test_instantiate(self):
     # Can build it with int or float or nothing
     msg_tmpl = "%s vs (expected) %s (args=%s)"
     for args, expected_pos, expected_size in (
         [(), Vector2(0, 0), Vector2(0, 0)],
         [(0.5, 0.5), Vector2(0.5, 0.5), Vector2(0, 0)],
         [(1, 2, 1, 2), Vector2(1, 2), Vector2(1, 2)],
     ):
         v = Rect2(*args)
         assert v.position == expected_pos, msg_tmpl % (
             v.position,
             expected_pos,
             args,
         )
         assert v.size == expected_size, msg_tmpl % (v.size, expected_size, args)
     with pytest.raises(TypeError):
         Rect2("a", 2, 3, 4)
     with pytest.raises(TypeError):
         Rect2(1, "b", 3, 4)
     with pytest.raises(TypeError):
         Rect2(1, 2, "c", 4)
     with pytest.raises(TypeError):
         Rect2(1, 2, 3, "d")
     with pytest.raises(TypeError):
         Rect2(None, 2)
 def test_bad_properties(self, args):
     v = Rect2()
     field, bad_value = args
     with pytest.raises(TypeError):
         setattr(v, field, bad_value)
class TestRect2:

    def test_base(self):
        v = Rect2(4, 3, 2, 1)
        assert type(v) == Rect2
        v2 = Rect2(1, 2, 3, 4)
        assert type(v) == Rect2
        assert v2 == Rect2(1, 2, 3, 4)
        assert v != v2

    def test_repr(self):
        v = Rect2(1, 2)
        assert repr(v) == "<Rect2(1, 2, 0, 0)>"

    def test_instantiate(self):
        # Can build it with int or float or nothing
        msg_tmpl = "%s vs (expected) %s (args=%s)"
        for args, expected_pos, expected_size in (
            [(), Vector2(0, 0), Vector2(0, 0)],
            [(0.5, 0.5), Vector2(0.5, 0.5), Vector2(0, 0)],
            [(1, 2, 1, 2), Vector2(1, 2), Vector2(1, 2)],
        ):
            v = Rect2(*args)
            assert v.position == expected_pos, msg_tmpl % (
                v.position, expected_pos, args
            )
            assert v.size == expected_size, msg_tmpl % (v.size, expected_size, args)
        with pytest.raises(TypeError):
            Rect2("a", 2, 3, 4)
        with pytest.raises(TypeError):
            Rect2(1, "b", 3, 4)
        with pytest.raises(TypeError):
            Rect2(1, 2, "c", 4)
        with pytest.raises(TypeError):
            Rect2(1, 2, 3, "d")
        with pytest.raises(TypeError):
            Rect2(None, 2)

    @pytest.mark.parametrize(
        "args",
        [
            ["clip", Rect2, (Rect2(),)],
            ["encloses", bool, (Rect2(),)],
            ["expand", Rect2, (Vector2(),)],
            ["get_area", float, ()],
            ["grow", Rect2, (0.5,)],
            ["has_no_area", bool, ()],
            ["has_point", bool, (Vector2(),)],
            ["intersects", bool, (Rect2(),)],
            ["merge", Rect2, (Rect2(),)],
        ],
        ids=lambda x: x[0],
    )
    def test_methods(self, args):
        v = Rect2()
        # Don't test methods' validity but bindings one
        field, ret_type, params = args
        assert hasattr(v, field)
        method = getattr(v, field)
        assert callable(method)
        ret = method(*params)
        assert type(ret) == ret_type

    @pytest.mark.parametrize(
        "args", [("position", Vector2), ("size", Vector2)], ids=lambda x: x[0]
    )
    def test_properties(self, args):
        v = Rect2()
        field, ret_type = args
        assert hasattr(v, field)
        field_val = getattr(v, field)
        assert type(field_val) == ret_type
        for val in (Vector2(), Vector2(0.1, -0.1)):
            setattr(v, field, val)
            field_val = getattr(v, field)
            assert field_val == val

    @pytest.mark.parametrize(
        "args",
        [
            ("position", "dummy"),
            ("size", "dummy"),
            ("position", None),
            ("size", None),
            ("position", 42),
            ("size", 42),
        ],
        ids=lambda x: x[0],
    )
    def test_bad_properties(self, args):
        v = Rect2()
        field, bad_value = args
        with pytest.raises(TypeError):
            setattr(v, field, bad_value)

    def test_equal(self):
        arr = Rect2(0.1, 1, 2, 3)
        other = Rect2(0.1, 1, 2, 3)
        assert arr == other
        bad = Rect2(0.1, 1, 2, 4)
        assert not arr == bad  # Force use of __eq__

    @pytest.mark.parametrize("arg", [None, 0, "foo", Rect2(0.1, 1, 2, 4)])
    def test_bad_equal(self, arg):
        arr = Rect2(0.1, 1, 2, 3)
        assert arr != arg
 def test_repr(self):
     v = Rect2(1, 2)
     assert repr(v) == "<Rect2(1, 2, 0, 0)>"
 def test_bad_equal(self, arg):
     arr = Rect2(0.1, 1, 2, 3)
     assert arr != arg
 def test_equal(self):
     arr = Rect2(0.1, 1, 2, 3)
     other = Rect2(0.1, 1, 2, 3)
     assert arr == other
     bad = Rect2(0.1, 1, 2, 4)
     assert not arr == bad  # Force use of __eq__
class TestTransform2D:
    def test_base(self):
        v = Transform2D()
        assert type(v) == Transform2D
        v2 = Transform2D(1, Vector2(1, 2))
        assert type(v) == Transform2D
        assert v2 == Transform2D(1, Vector2(1, 2))
        assert v != v2

    def test_repr(self):
        v = Transform2D(1, Vector2(1, 2))
        assert repr(v).startswith('<Transform2D(')

    # def test_instantiate(self):
    #     # Can build it with int or float or nothing
    #     msg_tmpl = "%s vs (expected) %s (args=%s)"
    #     for args, expected_x, expected_y in (
    #             [(), 0, 0],
    #             [(0.5, 0.5), 0.5, 0.5],
    #             [(1, 2), 1, 2],
    #             [(1,), 1, 0]):
    #         v = Transform2D(*args)
    #         assert v.x == expected_x, msg_tmpl % (v.x, expected_x, args)
    #         assert v.y == expected_y, msg_tmpl % (v.y, expected_y, args)
    #         assert v.width == expected_x, msg_tmpl % (v.width, expected_y, args)
    #         assert v.height == expected_y, msg_tmpl % (v.height, expected_x, args)
    #     with pytest.raises(TypeError):
    #         Transform2D("a", 2)
    #     with pytest.raises(TypeError):
    #         Transform2D("a", 2)
    #     with pytest.raises(TypeError):
    #         Transform2D(1, "b")
    #     with pytest.raises(TypeError):
    #         Transform2D(None, 2)

    @pytest.mark.parametrize('args', [
        ['inverse', Transform2D, ()],
        ['affine_inverse', Transform2D, ()],
        ['get_rotation', float, ()],
        ['get_origin', Vector2, ()],
        ['get_scale', Vector2, ()],
        ['orthonormalized', Transform2D, ()],
        ['rotated', Transform2D, (1.0, )],
        ['scaled', Transform2D, (Vector2(), )],
        ['translated', Transform2D, (Vector2(), )],
        ['xform', Vector2, (Vector2(), )],
        ['xform_inv', Vector2, (Vector2(), )],
        ['basis_xform', Vector2, (Vector2(), )],
        ['basis_xform_inv', Vector2, (Vector2(), )],
        ['interpolate_with', Transform2D, (Transform2D(), 1.0)],
        ['xform', Rect2, (Rect2(), )],
        ['xform_inv', Rect2, (Rect2(), )],
    ],
                             ids=lambda x: x[0])
    def test_methods(self, args):
        v = Transform2D()
        # Don't test methods' validity but bindings one
        field, ret_type, params = args
        assert hasattr(v, field)
        method = getattr(v, field)
        assert callable(method)
        ret = method(*params)
        assert type(ret) == ret_type

    # @pytest.mark.parametrize('arg', [
    #     None, 'dummy'
    # ], ids=lambda x: x[0])
    # def test_bad_mult(self, arg):
    #     with pytest.raises(TypeError):
    #         Transform2D(2, 3) * arg

    # @pytest.mark.parametrize('args', [
    #     (0, Transform2D(0, 0)),
    #     (1, Transform2D(2, 3)),
    #     (2.5, Transform2D(5, 7.5)),
    #     (Transform2D(1, 1), Transform2D(2, 3)),
    #     (Transform2D(2, 3), Transform2D(4, 9))
    # ], ids=lambda x: x[0])
    # def test_mult(self, args):
    #     param, result = args
    #     calc = Transform2D(2, 3) * param
    #     assert calc == result

    def test_equal(self):
        arr = Transform2D(1, Vector2(1, 2))
        other = Transform2D(1, Vector2(1, 2))
        assert arr == other
        bad = Transform2D(1, Vector2(1, 3))
        assert not arr == bad  # Force use of __eq__

    @pytest.mark.parametrize('arg', [
        None,
        0,
        'foo',
        Transform2D(1, Vector2(1, 3)),
    ])
    def test_bad_equal(self, arg):
        arr = Transform2D(1, Vector2(1, 2))
        assert arr != arg