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)
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'])
def _build_signal_info(signal): methinfo = Dictionary() methinfo["name"] = signal.name # Dummy data, only name is important here methinfo["args"] = Array() methinfo["default_args"] = Array() methinfo["return"] = None methinfo["flags"] = lib.METHOD_FLAG_FROM_SCRIPT return methinfo
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__
def test_as_both(self): a = Array() a.resize(9) pa = PoolVector3Array() pa.resize(1000) assert len(pa) == 1000 a[0] = pa assert len(pa) == 1000 pa.resize(2000) assert len(pa) == 2000 assert len(a[0]) == 2000 a[0].resize(3000) assert len(a[0]) == 3000
def _build_method_info(meth, methname): spec = inspect.getfullargspec(meth) methinfo = Dictionary() methinfo["name"] = methname # TODO: Handle classmethod/staticmethod methinfo["args"] = Array(spec.args) methinfo["default_args"] = Array() # TODO # TODO: use annotation to determine return type ? methinfo["return"] = None methinfo["flags"] = lib.METHOD_FLAG_FROM_SCRIPT methinfo["rpc_mode"] = getattr(meth, "__rpc", lib.GODOT_METHOD_RPC_MODE_DISABLED) return methinfo
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"]
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_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_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_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_bad_add(self, arg): with pytest.raises(TypeError): assert Array() + arg
def test_instantiate_from_copy(self, arg): arr = Array(arg) if hasattr(arg, "_gd_ptr"): assert arr._gd_ptr != arg._gd_ptr
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_bad_equal(self, arg): arr = Array([1]) assert arr != arg
def test_getitem(self): v = Array(['foo', 0, Node(), 0.42]) assert v[0] == 'foo' assert v[1] == 0 assert v[-1] == 0.42
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)
def test_size_in_array(self): a = Array() a.resize(9) a[0] = PoolVector3Array() a[0].resize(1000) assert len(a[0]) == 1000
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_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)>])>"
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)
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_getitem(self): v = Array(["foo", 0, Node(), 0.42]) assert v[0] == "foo" assert v[1] == 0 assert v[-1] == 0.42
def test_len(self): v = Array() assert len(v) == 0 v.append("foo") assert len(v) == 1
def test_bad_instantiate(self, arg): with pytest.raises(TypeError): Array(arg)
def test_outofrange_delitem(self): v = Array(["foo", 0]) with pytest.raises(IndexError): del 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)
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_base(self): v = Array() assert type(v) == Array
def test_outofrange_setitem(self): v = Array(['foo', 0]) with pytest.raises(IndexError): v[2] = 42