예제 #1
0
 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_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
예제 #3
0
 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
예제 #4
0
 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__
예제 #5
0
 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
예제 #6
0
 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__
예제 #7
0
 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__
예제 #8
0
class TestRID:

    def test_base(self):
        v = RID()
        assert type(v) == RID

    def test_equal(self):
        v1 = RID()
        v2 = RID()
        assert v1 == v2
        # Environment is a Ressource which provides unique rid per instance
        res_a = Environment()
        v_a_1 = RID(res_a)
        assert v_a_1 != v1
        v_a_2 = RID(res_a)
        assert v_a_1 == v_a_2
        res_b = Environment()
        v_b = RID(res_b)
        assert not v_a_1 == v_b  # Force use of __eq__

    @pytest.mark.parametrize('arg', [
        None,
        0,
        'foo',
        RID(Environment()),
    ])
    def test_bad_equal(self, arg):
        arr = Environment(Environment())
        assert arr != arg

    def test_repr(self):
        v = RID()
        assert repr(v) == '<RID(id=0)>'

    @pytest.mark.parametrize('arg', [
        42,
        'dummy',
        Node(),  # Node doesn't inherit from Resource
        RID()
    ])
    def test_bad_instantiate(self, arg):
        with pytest.raises(TypeError):
            RID(arg)

    @pytest.mark.parametrize('args', [
        ['get_id', int, ()],
    ], ids=lambda x: x[0])
    def test_methods(self, args):
        v = RID()
        # 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
예제 #9
0
class TestColor:

    def test_base(self):
        v = Color()
        assert type(v) == Color

    @pytest.mark.parametrize('arg', [
        (),
        (0xFF,),
        (0xFF, 0x77),
        (0xFF, 0x77, 0x33),
        (0xFF, 0x77, 0x33, 0x11),
        {'r': 0xFF, 'g': 0x77, 'b': 0x33, 'a': 0x11},
    ])
    def test_initialize(self, arg):
        if isinstance(arg, dict):
            v1 = Color(**arg)
            v2 = Color(**arg)
        else:
            v1 = Color(*arg)
            v2 = Color(*arg)
        assert v1 == v2

    def test_equal(self):
        v1 = Color()
        v2 = Color()
        assert v1 == v2
        vrgba = Color(1, 2, 3, 4)
        vrgb = Color(1, 2, 3)
        assert not vrgb == vrgba  # Force use of __eq__

    @pytest.mark.parametrize('arg', [
        None,
        0,
        'foo',
        Color(1, 2, 3, 5),
    ])
    def test_bad_equal(self, arg):
        basis = Color(1, 2, 3, 4)
        assert basis != arg

    def test_repr(self):
        v = Color()
        assert repr(v) == '<Color(r=0.0, g=0.0, b=0.0, a=1.0)>'

    @pytest.mark.parametrize('arg', [
        (None, ),
        (1, None),
        (1, 2, None),
        ('dummy', ),
        (Node(), ),
        (Vector2(), )
    ])
    def test_bad_instantiate(self, arg):
        with pytest.raises(TypeError):
            Color(*arg)

    @pytest.mark.parametrize('args', [
        ['to_32', int, ()],
        ['to_ARGB32', int, ()],
        ['gray', float, ()],
        ['inverted', Color, ()],
        ['contrasted', Color, ()],
        ['linear_interpolate', Color, (Color(0xAA, 0xBB, 0xCC), 2.2)],
        ['blend', Color, (Color(0xAA, 0xBB, 0xCC), )],
        ['to_html', str, (True, )],
    ], ids=lambda x: x[0])
    def test_methods(self, args):
        v = Color()
        # 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', [
        (Color(0, 0, 0), Color(1, 0, 0)),
        (Color(0, 1, 0), Color(1, 0, 0)),
        (Color(1, 0, 0), Color(1, 0, 1)),
    ], ids=lambda x: x[0])
    def test_lt(self, arg):
        small, big = arg
        assert small < big

    @pytest.mark.parametrize('args', [
        ('r', float),
        ('r8', int),
        ('g', float),
        ('g8', int),
        ('b', float),
        ('b8', int),
        ('a', float),
        ('a8', int),
    ], ids=lambda x: x[0])
    def test_properties_rw(self, args):
        v = Color()
        field, ret_type = args
        assert hasattr(v, field)
        field_val = getattr(v, field)
        assert type(field_val) == ret_type
        if ret_type is float:
            vals = (0, 10, 10., 42.5)
        else:
            vals = (0, 10, 0xFF)
        for val in vals:
            setattr(v, field, val)
            field_val = getattr(v, field)
            assert field_val == val

    @pytest.mark.parametrize('args', [
        ('h', float),
        ('s', float),
        ('v', float),
    ], ids=lambda x: x[0])
    def test_properties_ro(self, args):
        v = Color(4.2)
        field, ret_type = args
        assert hasattr(v, field)
        field_val = getattr(v, field)
        assert type(field_val) == ret_type
        with pytest.raises(AttributeError):
            setattr(v, field, 0.5)

    @pytest.mark.parametrize('args', [
        ('r', 'Nan'),
        ('r8', 'Nan'),
        ('g', 'Nan'),
        ('g8', 'Nan'),
        ('b', 'Nan'),
        ('b8', 'Nan'),
        ('a', 'Nan'),
        ('a8', 'Nan'),
    ], ids=lambda x: x[0])
    def test_bad_properties(self, args):
        v = Color()
        field, bad_value = args
        with pytest.raises(TypeError):
            setattr(v, field, bad_value)
예제 #10
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
예제 #11
0
 def test_getitem(self):
     v = Array(['foo', 0, Node(), 0.42])
     assert v[0] == 'foo'
     assert v[1] == 0
     assert v[-1] == 0.42
예제 #12
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
예제 #13
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])
        # Test __radd__ as well
        arr3 = ['-1'] + arr
        assert arr3 == Array(['-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)
예제 #14
0
 def test_iter(self):
     items = ["foo", 0, Node()]
     v = Array(items)
     items_from_v = [x for x in v]
     assert items_from_v == items
예제 #15
0
 def test_getitem(self):
     v = Array(["foo", 0, Node(), 0.42])
     assert v[0] == "foo"
     assert v[1] == 0
     assert v[-1] == 0.42
예제 #16
0
 def test_getitem_slice(self):
     v = Array(["foo", 0, Node()])
     assert isinstance(v[:-1], Array)
     assert v[1:] == Array([v[1], v[2]])
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)
예제 #18
0
class TestColor:
    def test_base(self):
        v = Color()
        assert type(v) == Color

    @pytest.mark.parametrize(
        "arg",
        [
            (),
            (0xFF, ),
            (0xFF, 0x77),
            (0xFF, 0x77, 0x33),
            (0xFF, 0x77, 0x33, 0x11),
            {
                "r": 0xFF,
                "g": 0x77,
                "b": 0x33,
                "a": 0x11
            },
        ],
    )
    def test_initialize(self, arg):
        if isinstance(arg, dict):
            v1 = Color(**arg)
            v2 = Color(**arg)
        else:
            v1 = Color(*arg)
            v2 = Color(*arg)
        assert v1 == v2

    def test_equal(self):
        v1 = Color()
        v2 = Color()
        assert v1 == v2
        vrgba = Color(1, 2, 3, 4)
        vrgb = Color(1, 2, 3)
        assert not vrgb == vrgba  # Force use of __eq__

    @pytest.mark.parametrize("arg", [None, 0, "foo", Color(1, 2, 3, 5)])
    def test_bad_equal(self, arg):
        basis = Color(1, 2, 3, 4)
        assert basis != arg

    def test_repr(self):
        v = Color()
        assert repr(v) == "<Color(r=0.0, g=0.0, b=0.0, a=1.0)>"

    @pytest.mark.parametrize("arg", [(None, ), (1, None), (1, 2, None),
                                     ("dummy", ), (Node(), ), (Vector2(), )])
    def test_bad_instantiate(self, arg):
        with pytest.raises(TypeError):
            Color(*arg)

    @pytest.mark.parametrize(
        "args",
        [
            ["to_rgba32", int, ()],
            ["to_argb32", int, ()],
            ["gray", float, ()],
            ["inverted", Color, ()],
            ["contrasted", Color, ()],
            ["linear_interpolate", Color, (Color(0xAA, 0xBB, 0xCC), 2.2)],
            ["blend", Color, (Color(0xAA, 0xBB, 0xCC), )],
            ["to_html", str, (True, )],
        ],
        ids=lambda x: x[0],
    )
    def test_methods(self, args):
        v = Color()
        # 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",
        [
            (Color(0, 0, 0), Color(1, 0, 0)),
            (Color(0, 1, 0), Color(1, 0, 0)),
            (Color(1, 0, 0), Color(1, 0, 1)),
        ],
        ids=lambda x: x[0],
    )
    def test_lt(self, arg):
        small, big = arg
        assert small < big

    @pytest.mark.parametrize(
        "args",
        [
            ("r", float),
            ("r8", int),
            ("g", float),
            ("g8", int),
            ("b", float),
            ("b8", int),
            ("a", float),
            ("a8", int),
        ],
        ids=lambda x: x[0],
    )
    def test_properties_rw(self, args):
        v = Color()
        field, ret_type = args
        assert hasattr(v, field)
        field_val = getattr(v, field)
        assert type(field_val) == ret_type
        if ret_type is float:
            vals = (0, 10, 10., 42.5)
        else:
            vals = (0, 10, 0xFF)
        for val in vals:
            setattr(v, field, val)
            field_val = getattr(v, field)
            assert field_val == val

    @pytest.mark.parametrize("args", [("h", float), ("s", float),
                                      ("v", float)],
                             ids=lambda x: x[0])
    def test_properties_ro(self, args):
        v = Color(4.2)
        field, ret_type = args
        assert hasattr(v, field)
        field_val = getattr(v, field)
        assert type(field_val) == ret_type
        with pytest.raises(AttributeError):
            setattr(v, field, 0.5)

    @pytest.mark.parametrize(
        "args",
        [
            ("r", "Nan"),
            ("r8", "Nan"),
            ("g", "Nan"),
            ("g8", "Nan"),
            ("b", "Nan"),
            ("b8", "Nan"),
            ("a", "Nan"),
            ("a8", "Nan"),
        ],
        ids=lambda x: x[0],
    )
    def test_bad_properties(self, args):
        v = Color()
        field, bad_value = args
        with pytest.raises(TypeError):
            setattr(v, field, bad_value)
예제 #19
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)