Пример #1
0
class TestArray:
    def test_base(self):
        v = Array()
        assert type(v) == Array

    def test_equal(self):
        arr = Array()
        other = Array()
        for item in [1, "foo", Node(), Vector2()]:
            arr.append(item)
            other.append(item)
        assert arr == other
        bad = Array([0, 0, 0])
        assert not arr == bad  # Force use of __eq__

    @pytest.mark.parametrize(
        "arg",
        [
            None,
            0,
            "foo",
            Vector2(),
            Node(),
            [1],
            Array([1, 2]),
            PoolByteArray([1]),
            PoolIntArray([1]),
        ],
    )
    def test_bad_equal(self, arg):
        arr = Array([1])
        assert arr != arg

    def test_add(self):
        arr = Array([None])
        arr += Array([1, "two"])  # __iadd__
        assert arr == Array([None, 1, "two"])
        arr2 = arr + Array([3])  # __add__
        assert arr2 == Array([None, 1, "two", 3])

    def test_add_with_non_array(self):
        arr = Array([0])
        arr += [1, "two"]  # __iadd__
        assert arr == Array([0, 1, "two"])
        arr2 = arr + [3]  # __add__
        assert arr2 == Array([0, 1, "two", 3])
        # Also test list's __iadd__
        arr3 = ["-1"]
        arr3 += arr
        assert arr3 == ["-1", 0, 1, "two"]
        # list.__add__ only works with other lists
        with pytest.raises(TypeError):
            ["-1"] + arr
        arr4 = ["-1"] + list(arr)
        assert arr4 == ["-1", 0, 1, "two"]

    @pytest.mark.parametrize("arg", [None, 0, "foo", Vector2(), Node()])
    def test_bad_add(self, arg):
        with pytest.raises(TypeError):
            assert Array() + arg

    def test_repr(self):
        v = Array()
        assert repr(v) == "<Array([])>"
        v = Array([1, "foo", Vector2()])
        assert repr(v) == "<Array([1, 'foo', <Vector2(x=0.0, y=0.0)>])>"

    @pytest.mark.parametrize(
        "arg",
        [42, "dummy", Node(), Vector2(), [object()]])
    def test_bad_instantiate(self, arg):
        with pytest.raises(TypeError):
            Array(arg)

    @pytest.mark.parametrize(
        "arg",
        [
            Array(),
            PoolColorArray(),
            PoolVector3Array(),
            PoolVector2Array(),
            PoolStringArray(),
            PoolRealArray(),
            PoolIntArray(),
            PoolByteArray(),
            [],
            (),
            [42, 43, 44],
            ("foo", "bar", "spam"),
            (Node(), Resource(), Area2D()),
            [Vector2(), Vector2(), Vector2()],
            (Node(), Resource(), Area2D(), Vector2(), "foo",
             0),  # Enjoy the mix
        ],
    )
    def test_instantiate_from_copy(self, arg):
        arr = Array(arg)
        if hasattr(arg, "_gd_ptr"):
            assert arr._gd_ptr != arg._gd_ptr

    @pytest.mark.parametrize(
        "args",
        [
            ["append", type(None), ("bar", )],
            ["clear", type(None), ()],
            ["count", int, ("foo", )],
            ["empty", bool, ()],
            ["erase", type(None), ("foo", )],
            ["front", str, ()],
            ["back", str, ()],
            ["find", int, ("foo", 0)],
            ["find_last", int, ("foo", )],
            ["has", bool, ("foo", )],
            ["hash", int, ()],
            ["insert", type(None), (0, "bar")],
            ["invert", type(None), ()],
            ["pop_back", str, ()],
            ["pop_front", str, ()],
            ["push_back", type(None), ("bar", )],
            ["push_front", type(None), ("bar", )],
            ["resize", type(None), (2, )],
            ["rfind", int, ("foo", 0)],
            ["sort", type(None), ()],
            # ['sort_custom', type(None), (obj, func)],
        ],
        ids=lambda x: x[0],
    )
    def test_methods(self, args):
        v = Array(["foo"])
        # 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_len(self):
        v = Array()
        assert len(v) == 0
        v.append("foo")
        assert len(v) == 1

    def test_getitem(self):
        v = Array(["foo", 0, Node(), 0.42])
        assert v[0] == "foo"
        assert v[1] == 0
        assert v[-1] == 0.42

    def test_getitem_slice(self):
        v = Array(["foo", 0, Node()])
        assert isinstance(v[:-1], Array)
        assert v[1:] == Array([v[1], v[2]])

    def test_outofrange_getitem(self):
        v = Array(["foo", 0])
        with pytest.raises(IndexError):
            v[2]

    def test_setitem(self):
        v = Array(["foo", 0, Node()])
        v[0] = "bar"
        assert len(v) == 3
        assert v[0] == "bar"
        v[-1] = 4
        assert len(v) == 3
        assert v[2] == 4

    def test_outofrange_setitem(self):
        v = Array(["foo", 0])
        with pytest.raises(IndexError):
            v[2] = 42

    def test_delitem(self):
        v = Array(["foo", 0, Node()])
        del v[0]
        assert len(v) == 2
        assert v[0] == 0
        del v[-1]
        assert len(v) == 1
        v[0] == 0

    def test_outofrange_delitem(self):
        v = Array(["foo", 0])
        with pytest.raises(IndexError):
            del v[2]

    def test_iter(self):
        items = ["foo", 0, Node()]
        v = Array(items)
        items_from_v = [x for x in v]
        assert items_from_v == items

    def test_append(self):
        items = [1, "foo", Node()]
        v = Array()
        for item in items:
            v.append(item)
        assert len(v) == 3
        assert v == Array(items)
Пример #2
0
 def test_keys(self):
     v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
     keys = v.keys()
     assert set(keys) == set(['a', 2, 0.5])
Пример #3
0
 def test_bad_equal(self, arg):
     arr = Transform2D(1, Vector2(1, 2))
     assert arr != arg
Пример #4
0
 def test_bad_equal(self, arg):
     arr = Vector2(1, 2)
     assert arr != arg
Пример #5
0
 def test_contains(self):
     v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
     assert 'a' in v
     assert 'dummy' not in v
Пример #6
0
 def test_bad_mult(self, arg):
     with pytest.raises(TypeError):
         Vector2(2, 3) * arg
Пример #7
0
 def test_div(self, args):
     param, result = args
     calc = Vector2(2, 3) / param
     assert calc == result
Пример #8
0
class BaseTestPoolArray:
    def _expand_arg(self, arg):
        return arg(self) if isfunction(arg) else arg

    def test_base(self):
        v = self.acls()
        assert type(v) == self.acls

    @pytest.mark.parametrize("arg", [Array, lambda s: s.acls, list])
    def test_equal(self, arg):
        arg = self._expand_arg(arg)
        arr = self.acls()
        other = arg()
        for item in [self.vg() for _ in range(4)]:
            arr.append(item)
            other.append(item)
        assert arr == other
        bad = self.acls([self.vg()])
        assert not arr == bad  # Force use of __eq__

    @pytest.mark.parametrize(
        "arg",
        [
            None,
            0,
            "foo",
            Vector2(),
            Node(),
            lambda s: s.vg(1),
            lambda s: s.acls(s.vg(1)),
            lambda s: Array(s.vg(1)),
        ],
    )
    def test_bad_equal(self, arg):
        arg = self._expand_arg(arg)
        arr = self.acls()
        assert arr != arg

    def test_add(self):
        v0 = self.vg(1)
        arr = self.acls(v0)
        v1 = self.vg(2)
        arr += self.acls(v1)  # __iadd__
        assert arr == self.acls(v0 + v1)
        v2 = self.vg(1)
        arr2 = arr + self.acls(v2)  # __add__
        assert arr2 == self.acls(v0 + v1 + v2)

    def test_add_with_non_PoolBytearray(self):
        v0 = self.vg(1)
        arr = self.acls(v0)
        v1 = self.vg(2)
        arr += v1  # __iadd__
        assert arr == self.acls(v0 + v1)
        v2 = self.vg(1)
        arr2 = arr + v2  # __add__
        assert arr2 == self.acls(v0 + v1 + v2)
        # Test __radd__ as well
        v3 = self.vg(1)
        arr3 = v3 + arr2
        assert arr3 == self.acls(v3 + v0 + v1 + v2)

    @pytest.mark.parametrize("arg", [None, 0, "foo", Vector2(), Node()])
    def test_bad_add(self, arg):
        with pytest.raises(TypeError):
            assert self.acls() + arg

    def test_repr(self):
        name = self.acls.__name__
        v = self.acls()
        assert repr(v) == "<%s([])>" % name
        items = self.vg(3)
        v = self.acls(items)
        assert repr(v) == "<%s(%s)>" % (name, items)

    @pytest.mark.parametrize(
        "arg",
        [42, "dummy",
         Node(),
         Vector2(), [object()],
         Array(["not", "bytes"])])
    def test_bad_instantiate(self, arg):
        with pytest.raises(TypeError):
            PoolByteArray(arg)

    @pytest.mark.parametrize("arg", [
        lambda s: s.acls(), lambda s: Array(s.vg(2)), [], (), lambda s: s.vg(3)
    ])
    def test_instantiate_from_copy(self, arg):
        arg = self._expand_arg(arg)
        arr = self.acls(arg)
        if hasattr(arg, "_gd_ptr"):
            assert arr._gd_ptr != arg._gd_ptr

    @pytest.mark.parametrize(
        "args",
        [
            ["append", type(None), lambda s: (s.vg(), )],
            ["insert", type(None), lambda s: (0, s.vg())],
            ["push_back", type(None), lambda s: (s.vg(), )],
            ["resize", type(None), (2, )],
        ],
        ids=lambda x: x[0],
    )
    def test_methods(self, args):
        v = self.acls(self.vg(1))
        # Don't test methods' validity but bindings one
        field, ret_type, params = args
        params = self._expand_arg(params)
        assert hasattr(v, field)
        method = getattr(v, field)
        assert callable(method)
        ret = method(*params)
        assert type(ret) == ret_type

    def test_len(self):
        arr = self.acls()
        assert len(arr) == 0
        arr.append(self.vg())
        assert len(arr) == 1

    def test_getitem(self):
        v = self.vg(3)
        arr = self.acls(v)
        assert arr[0] == v[0]
        assert arr[1] == v[1]
        assert arr[-1] == v[-1]

    def test_getitem_slice(self):
        arr = self.acls(self.vg(3))
        assert isinstance(arr[:-1], self.acls)
        assert arr[1:] == self.acls([arr[1], arr[2]])

    def test_outofrange_getitem(self):
        arr = self.acls(self.vg(2))
        with pytest.raises(IndexError):
            arr[2]

    def test_setitem(self):
        arr = self.acls(self.vg(3))
        v = self.vg()
        arr[0] = v
        assert len(arr) == 3
        assert arr[0] == v
        arr[-1] = v
        assert len(arr) == 3
        assert arr[-1] == v

    def test_outofrange_setitem(self):
        arr = self.acls(self.vg(2))
        v = self.vg()
        with pytest.raises(IndexError):
            arr[2] = v

    def test_delitem(self):
        items = self.vg(3)
        arr = self.acls(items)
        del arr[0]
        assert len(arr) == 2
        assert arr[0] == items[1]
        assert arr[1] == items[2]
        del arr[-1]
        assert len(arr) == 1
        assert arr[-1] == items[1]

    def test_outofrange_delitem(self):
        arr = self.acls(self.vg(2))
        with pytest.raises(IndexError):
            del arr[2]

    def test_iter(self):
        items = self.vg(3)
        arr = self.acls(items)
        items_from_v = [x for x in arr]
        assert items_from_v == items

    def test_append(self):
        items = self.vg(3)
        arr = self.acls()
        for item in items:
            arr.append(item)
        assert len(arr) == 3
        assert arr == self.acls(items)
Пример #9
0
 def _ready(self):
     self.direction = Vector2(1, 0)
     self.ball_speed = DEFAULT_SPEED
     self.stopped = False
     self.screen_size = self.get_viewport_rect().size
     self.set_process(True)  # REMOVE ME
Пример #10
0
 def test_keys(self):
     v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
     keys = v.keys()
     assert set(keys) == set(["a", 2, 0.5])
Пример #11
0
class TestDictionary:
    def test_base(self):
        v = Dictionary()
        assert type(v) == Dictionary

    @pytest.mark.xfail(
        reason="Godot Dictionary equal does lame comparison by pointer so far..."
    )
    def test_equal(self):
        arr = Dictionary()
        other = Dictionary()
        for key, value in [("a", 1), ("b", "foo"), ("c", Node()), ("d", Vector2())]:
            other[key] = arr[key] = value
        assert arr == other
        bad = Dictionary({"a": 1})
        assert not arr == bad  # Force use of __eq__

    @pytest.mark.parametrize(
        "arg", [None, 0, "foo", Vector2(), Node(), {"a": 1}, Dictionary({"b": 2})]
    )
    def test_bad_equal(self, arg):
        arr = Dictionary({"a": 1})
        assert arr != arg

    def test_repr(self):
        v = Dictionary()
        assert repr(v) == "<Dictionary({})>"
        v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
        assert repr(v).startswith("<Dictionary({")
        for item in ["'a': 1", "2: 'foo'", "0.5: <Vector2(x=0.0, y=0.0)>"]:
            assert item in repr(v)

    @pytest.mark.parametrize(
        "arg",
        [42, "dummy", Node(), Vector2(), [object()], {object(): 1}, {1: object()}],
    )
    def test_bad_instantiate(self, arg):
        with pytest.raises(TypeError):
            Dictionary(arg)

    @pytest.mark.parametrize(
        "arg",
        [
            Dictionary(),
            {},
            {"a": 1, 2: "foo", 0.5: Vector2()},
            Dictionary({"a": 1, 2: "foo", 0.5: Vector2()}),
        ],
    )
    def test_instantiate_from_copy(self, arg):
        arr = Dictionary(arg)
        if hasattr(arg, "_gd_ptr"):
            assert arr._gd_ptr != arg._gd_ptr

    def test_len(self):
        v = Dictionary()
        assert len(v) == 0
        v["foo"] = "bar"
        assert len(v) == 1

    def test_getitem(self):
        v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
        assert v["a"] == 1
        assert v[0.5] == Vector2()
        # Missing items are stored as None
        assert v["dummy"] is None
        # Cannot store non Godot types
        with pytest.raises(TypeError):
            v[object()]

    def test_setitem(self):
        v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
        v[0] = "bar"
        assert len(v) == 4
        assert v[0] == "bar"
        v["a"] = 4
        assert len(v) == 4
        assert v["a"] == 4
        # Cannot store non Godot types
        with pytest.raises(TypeError):
            v[object()] = 4
        with pytest.raises(TypeError):
            v[4] = object()

    def test_delitem(self):
        v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
        del v["a"]
        assert len(v) == 2
        del v[0.5]
        assert len(v) == 1
        v[2] == "foo"
        # Missing items can be deleted without error
        del v["missing"]
        # Cannot store non Godot types
        with pytest.raises(TypeError):
            del v[object()]

    def test_update(self):
        v = Dictionary({"a": 1, "b": 2, "c": 3})
        v.update({"a": "one", "d": "four"})
        v.update(Dictionary({"b": "two", "e": "five"}))
        assert set(v.keys()) == {"a", "b", "c", "d", "e"}
        assert set(v.values()) == {"one", "two", 3, "four", "five"}

    def test_contains(self):
        v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
        assert "a" in v
        assert "dummy" not in v

    def test_iter(self):
        v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
        items = ["a", 2, 0.5]
        items_from_v = [x for x in v]
        assert set(items_from_v) == set(items)

    def test_keys(self):
        v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
        keys = v.keys()
        assert set(keys) == set(["a", 2, 0.5])

    def test_values(self):
        v = Dictionary({"a": 1, 2: "foo"})
        values = v.values()
        assert set(values) == set([1, "foo"])

    def test_items(self):
        v = Dictionary({"a": 1, 2: "foo"})
        items = v.items()
        assert set(items) == set([("a", 1), (2, "foo")])

    def test_empty_and_clear(self):
        v = Dictionary({"a": 1, 2: "foo"})
        assert not v.empty()
        v.clear()
        assert len(v) == 0
        assert v.empty()

    def test_in(self):
        v = Dictionary({"a": 1, 2: "foo"})
        assert "a" in v
        assert "dummy" not in v

    def test_hash(self):
        v = Dictionary({"a": 1, 2: "foo"})
        v.hash()

    def test_has_all(self):
        v = Dictionary({"a": 1, 2: "foo", None: None})
        elems = Array(["a", None])
        assert v.has_all(elems)
        bad_elems = Array(["a", 42])
        assert not v.has_all(bad_elems)

    def test_to_json(self):
        v = Dictionary({"a": 1, "b": "foo"})
        jsoned = v.to_json()
        v2 = json.loads(jsoned)
        assert v2 == {"a": 1, "b": "foo"}
        assert json
Пример #12
0
 def test_iter(self):
     v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
     items = ["a", 2, 0.5]
     items_from_v = [x for x in v]
     assert set(items_from_v) == set(items)
Пример #13
0
 def test_contains(self):
     v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()})
     assert "a" in v
     assert "dummy" not in v
Пример #14
0
 def test_repr(self):
     v = Array()
     assert repr(v) == "<Array([])>"
     v = Array([1, "foo", Vector2()])
     assert repr(v) == "<Array([1, 'foo', <Vector2(x=0.0, y=0.0)>])>"
Пример #15
0
 def test_bad_div(self, arg):
     with pytest.raises(TypeError):
         Vector2(2, 3) / arg
Пример #16
0
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
Пример #17
0
 def test_zero_div(self, arg):
     with pytest.raises(ZeroDivisionError):
         Vector2(2, 3) / arg
Пример #18
0
 def test_bad_properties(self, args):
     v = Vector2()
     field, bad_value = args
     with pytest.raises(TypeError):
         setattr(v, field, bad_value)
Пример #19
0
 def test_mult(self, args):
     param, result = args
     calc = Vector2(2, 3) * param
     assert calc == result
Пример #20
0
 def test_add(self, args):
     param, result = args
     calc = Vector2(2, 3) + param
     assert calc == result
Пример #21
0
 def test_equal(self):
     arr = Vector2(1, 2)
     other = Vector2(1, 2)
     assert arr == other
     bad = Vector2(1, 3)
     assert not arr == bad  # Force use of __eq__
Пример #22
0
 def test_repr(self):
     v = Vector2(1, 2)
     assert repr(v) == "<Vector2(x=1.0, y=2.0)>"
Пример #23
0
class TestVector2:
    def test_base(self):
        v = Vector2()
        assert type(v) == Vector2
        v2 = Vector2(1, -2)
        assert type(v) == Vector2
        assert v2 == Vector2(1, -2)
        assert v != v2

    def test_repr(self):
        v = Vector2(1, 2)
        assert repr(v) == "<Vector2(x=1.0, y=2.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_x, expected_y in (
            [(), 0, 0],
            [(0.5, 0.5), 0.5, 0.5],
            [(1, 2), 1, 2],
            [(1, ), 1, 0],
        ):
            v = Vector2(*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):
            Vector2("a", 2)
        with pytest.raises(TypeError):
            Vector2("a", 2)
        with pytest.raises(TypeError):
            Vector2(1, "b")
        with pytest.raises(TypeError):
            Vector2(None, 2)

    @pytest.mark.parametrize(
        "args",
        [
            ["abs", Vector2, ()],
            ["angle", float, ()],
            ["angle_to", float, (Vector2(), )],
            ["angle_to_point", float, (Vector2(), )],
            ["clamped", Vector2, (0.5, )],
            [
                "cubic_interpolate", Vector2,
                (Vector2(), Vector2(), Vector2(), 0.5)
            ],
            ["distance_squared_to", float, (Vector2(), )],
            ["distance_to", float, (Vector2(), )],
            ["dot", float, (Vector2(), )],
            ["floor", Vector2, ()],
            ["aspect", float, ()],
            ["length", float, ()],
            ["length_squared", float, ()],
            ["linear_interpolate", Vector2, (Vector2(), 0.5)],
            ["normalized", Vector2, ()],
            ["reflect", Vector2, (Vector2(), )],
            ["rotated", Vector2, (0.5, )],
            ["slide", Vector2, (Vector2(), )],
            ["snapped", Vector2, (Vector2(), )],
            ["tangent", Vector2, ()],
        ],
        ids=lambda x: x[0],
    )
    def test_methods(self, args):
        v = Vector2()
        # 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",
        [("height", float), ("width", float), ("x", float), ("y", float)],
        ids=lambda x: x[0],
    )
    def test_properties(self, args):
        v = Vector2()
        field, ret_type = args
        assert hasattr(v, field)
        field_val = getattr(v, field)
        assert type(field_val) == ret_type
        for val in (0, 10, 10., 42.5):
            setattr(v, field, val)
            field_val = getattr(v, field)
            assert field_val == val

    @pytest.mark.parametrize(
        "args",
        [("height", "NaN"), ("width", "NaN"), ("x", "NaN"), ("y", "NaN")],
        ids=lambda x: x[0],
    )
    def test_bad_properties(self, args):
        v = Vector2()
        field, bad_value = args
        with pytest.raises(TypeError):
            setattr(v, field, bad_value)

    def test_unary(self):
        v = Vector2(1, 2)
        v2 = -v
        assert v2.x == -1
        assert v2.y == -2
        v3 = +v
        assert v3.x == 1
        assert v3.y == 2
        v = Vector2(1.5, 2.5)
        v2 = -v
        assert v2.x == -1.5
        assert v2.y == -2.5
        v3 = +v
        assert v3.x == 1.5
        assert v3.y == 2.5

    @pytest.mark.parametrize(
        "args",
        [
            (Vector2(0, 0), Vector2(2, 3)),
            (Vector2(3, 2), Vector2(5, 5)),
            (Vector2(-1, -4), Vector2(1, -1)),
        ],
        ids=lambda x: x[0],
    )
    def test_add(self, args):
        param, result = args
        calc = Vector2(2, 3) + param
        assert calc == result

    @pytest.mark.parametrize(
        "args",
        [
            (Vector2(0, 0), Vector2(2, 3)),
            (Vector2(3, 2), Vector2(-1, 1)),
            (Vector2(-1, -1), Vector2(3, 4)),
        ],
        ids=lambda x: x[0],
    )
    def test_sub(self, args):
        param, result = args
        calc = Vector2(2, 3) - param
        assert calc == result

    @pytest.mark.parametrize("arg", [None, 1, "dummy"], ids=lambda x: x[0])
    def test_bad_add(self, arg):
        with pytest.raises(TypeError):
            Vector2(2, 3) + arg

    @pytest.mark.parametrize("arg", [None, 1, "dummy"], ids=lambda x: x[0])
    def test_bad_sub(self, arg):
        with pytest.raises(TypeError):
            Vector2(2, 3) - arg

    @pytest.mark.parametrize("arg", [None, "dummy"], ids=lambda x: x[0])
    def test_bad_div(self, arg):
        with pytest.raises(TypeError):
            Vector2(2, 3) / arg

    @pytest.mark.parametrize(
        "arg", [0, Vector2(0, 1),
                Vector2(1, 0), Vector2(0, 0)],
        ids=lambda x: x[0])
    def test_zero_div(self, arg):
        with pytest.raises(ZeroDivisionError):
            Vector2(2, 3) / arg

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

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

    @pytest.mark.parametrize(
        "args",
        [
            (1, Vector2(2, 3)),
            (.5, Vector2(4, 6)),
            (2, Vector2(1, 1.5)),
            (Vector2(1, 1), Vector2(2, 3)),
            (Vector2(2, 3), Vector2(1, 1)),
        ],
        ids=lambda x: x[0],
    )
    def test_div(self, args):
        param, result = args
        calc = Vector2(2, 3) / param
        assert calc == result

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

    @pytest.mark.parametrize("arg", [None, 0, "foo", Vector2(1, 3)])
    def test_bad_equal(self, arg):
        arr = Vector2(1, 2)
        assert arr != arg
Пример #24
0
 def test_sub(self, args):
     param, result = args
     calc = Vector2(2, 3) - param
     assert calc == result
Пример #25
0
 def test_iter(self):
     v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
     items = ['a', 2, 0.5]
     items_from_v = [x for x in v]
     assert set(items_from_v) == set(items)
Пример #26
0
 def test_bad_add(self, arg):
     with pytest.raises(TypeError):
         Vector2(2, 3) + arg
Пример #27
0
class TestDictionary:
    def test_base(self):
        v = Dictionary()
        assert type(v) == Dictionary

    @pytest.mark.xfail(
        reason="Godot Dictionary equal does lame comparison by pointer so far..."
    )
    def test_equal(self):
        arr = Dictionary()
        other = Dictionary()
        for key, value in [('a', 1), ('b', 'foo'), ('c', Node()),
                           ('d', Vector2())]:
            other[key] = arr[key] = value
        assert arr == other
        bad = Dictionary({'a': 1})
        assert not arr == bad  # Force use of __eq__

    @pytest.mark.parametrize('arg', [
        None,
        0,
        'foo',
        Vector2(),
        Node(),
        {
            'a': 1
        },
        Dictionary({'b': 2}),
    ])
    def test_bad_equal(self, arg):
        arr = Dictionary({'a': 1})
        assert arr != arg

    def test_repr(self):
        v = Dictionary()
        assert repr(v) == '<Dictionary({})>'
        v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
        assert repr(v).startswith("<Dictionary({")
        for item in ["'a': 1", "2: 'foo'", "0.5: <Vector2(x=0.0, y=0.0)>"]:
            assert item in repr(v)

    @pytest.mark.parametrize('arg', [
        42, 'dummy',
        Node(),
        Vector2(), [object()], {
            object(): 1
        }, {
            1: object()
        }
    ])
    def test_bad_instantiate(self, arg):
        with pytest.raises(TypeError):
            Dictionary(arg)

    @pytest.mark.parametrize('arg', [
        Dictionary(), {}, {
            'a': 1,
            2: 'foo',
            0.5: Vector2()
        },
        Dictionary({
            'a': 1,
            2: 'foo',
            0.5: Vector2()
        })
    ])
    def test_instantiate_from_copy(self, arg):
        arr = Dictionary(arg)
        if hasattr(arg, '_gd_ptr'):
            assert arr._gd_ptr != arg._gd_ptr

    def test_len(self):
        v = Dictionary()
        assert len(v) == 0
        v['foo'] = 'bar'
        assert len(v) == 1

    def test_getitem(self):
        v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
        assert v['a'] == 1
        assert v[0.5] == Vector2()
        # Missing items are stored as None
        assert v['dummy'] is None
        # Cannot store non Godot types
        with pytest.raises(TypeError):
            v[object()]

    def test_setitem(self):
        v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
        v[0] = 'bar'
        assert len(v) == 4
        assert v[0] == 'bar'
        v['a'] = 4
        assert len(v) == 4
        assert v['a'] == 4
        # Cannot store non Godot types
        with pytest.raises(TypeError):
            v[object()] = 4
        with pytest.raises(TypeError):
            v[4] = object()

    def test_delitem(self):
        v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
        del v['a']
        assert len(v) == 2
        del v[0.5]
        assert len(v) == 1
        v[2] == 'foo'
        # Missing items can be deleted without error
        del v['missing']
        # Cannot store non Godot types
        with pytest.raises(TypeError):
            del v[object()]

    def test_contains(self):
        v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
        assert 'a' in v
        assert 'dummy' not in v

    def test_iter(self):
        v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
        items = ['a', 2, 0.5]
        items_from_v = [x for x in v]
        assert set(items_from_v) == set(items)

    def test_keys(self):
        v = Dictionary({'a': 1, 2: 'foo', 0.5: Vector2()})
        keys = v.keys()
        assert set(keys) == set(['a', 2, 0.5])

    def test_values(self):
        v = Dictionary({'a': 1, 2: 'foo'})
        values = v.values()
        assert set(values) == set([1, 'foo'])

    def test_items(self):
        v = Dictionary({'a': 1, 2: 'foo'})
        items = v.items()
        assert set(items) == set([('a', 1), (2, 'foo')])

    def test_empty_and_clear(self):
        v = Dictionary({'a': 1, 2: 'foo'})
        assert not v.empty()
        v.clear()
        assert len(v) == 0
        assert v.empty()

    def test_in(self):
        v = Dictionary({'a': 1, 2: 'foo'})
        assert 'a' in v
        assert 'dummy' not in v

    def test_hash(self):
        v = Dictionary({'a': 1, 2: 'foo'})
        v.hash()

    def test_has_all(self):
        v = Dictionary({'a': 1, 2: 'foo', None: None})
        elems = Array(['a', None])
        assert v.has_all(elems)
        bad_elems = Array(['a', 42])
        assert not v.has_all(bad_elems)

    def test_to_json(self):
        v = Dictionary({'a': 1, 'b': 'foo'})
        jsoned = v.to_json()
        v2 = json.loads(jsoned)
        assert v2 == {'a': 1, 'b': 'foo'}
        assert json
Пример #28
0
 def test_bad_sub(self, arg):
     with pytest.raises(TypeError):
         Vector2(2, 3) - arg
Пример #29
0
 def test_repr(self):
     v = Transform2D(1, Vector2(1, 2))
     assert repr(v).startswith('<Transform2D(')
 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__