def test_unicode(char): # Godot supports UCS2 on Windows and UCS4 on other platforms if len(char.encode("utf8")) > 2 and sys.platform == "win32": pytest.skip("Windows only supports UCS2") gdchar = GDString(char) assert str(gdchar) == char assert gdchar.length() == len(char)
def test_overloaded_property(pynode, pysubnode): # Not supported by GDScript # Parent property pynode.overloaded_by_child_prop = "foo" value = pynode.overloaded_by_child_prop assert value == GDString("foo") # Overloaded property pysubnode.overloaded_by_child_prop = "foo" value = pysubnode.overloaded_by_child_prop assert value == GDString("sub:foo")
def test_setitem(): v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()}) v[0] = GDString("bar") assert len(v) == 4 assert v[0] == GDString("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_getitem_slice(slice_): vals = [GDString("foo"), 0, OS, False] arr = Array(vals) expected = vals[slice_] sub_arr = arr[slice_] assert isinstance(sub_arr, Array) assert list(sub_arr) == expected
def _process(self, delta): motion = 0 if Input.is_action_pressed(self.action_prefix + GDString("_move_up")): motion -= 1 elif Input.is_action_pressed(self.action_prefix + GDString("_move_down")): motion += 1 motion *= MOTION_SPEED if self.can_move: self.translate(Vector2(0, motion * delta)) # set screen limits if self.position.y < 0: self.position.y = 0 elif self.position.y > self.screen_size.y: self.position.y = self.screen_size.y
def test_setitem(): v = Array(["foo", 0, OS]) v[0] = "bar" assert len(v) == 3 assert v[0] == GDString("bar") v[-1] = 4 assert len(v) == 3 assert v[2] == 4
def test_native_method(node): original_name = node.get_name() try: node.set_name("foo") name = node.get_name() assert name == GDString("foo") finally: node.set_name(original_name)
def test_virtual_to_string_customize(generate_obj): node = generate_obj(virtualtestbedcls) # Object.to_string() can be customized when defining _to_string() expected = GDString("<Main Node>") assert node._to_string() == expected assert node.to_string() == expected # Try to access undefined _to_string node = generate_obj(Node) with pytest.raises(AttributeError): node._to_string()
def test_update(): v = Dictionary({"a": 1, "b": 2, "c": 3}) v.update({"a": "one", "d": "four"}) v.update(Dictionary({"b": "two", "e": "five"})) assert list(v.keys()) == [ GDString("a"), GDString("b"), GDString("c"), GDString("d"), GDString("e"), ] assert list(v.values()) == [ GDString("one"), GDString("two"), 3, GDString("four"), GDString("five"), ]
def test_delitem(): 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] == GDString("foo") # Delete on missing items should raise error with pytest.raises(KeyError): del v["missing"] # Cannot store non Godot types with pytest.raises(TypeError): del v[object()]
def test_add_with_non_array(): arr = Array([0]) arr += [1, "two"] # __iadd__ assert arr == Array([0, 1, "two"]) arr2 = arr + [3] # __add__ assert arr2 == Array([0, 1, "two", 3]) assert arr == Array([0, 1, "two"]) # arr shouldn't have been modified # list.__iadd__ only works with other lists arr3 = ["-1"] with pytest.raises(TypeError): arr3 += arr # list.__add__ only works with other lists with pytest.raises(TypeError): ["-1"] + arr arr4 = ["-1"] + list(arr) assert arr4 == ["-1", 0, 1, GDString("two")]
def test_method_call(anynode): ret = anynode.meth("foo") assert ret == GDString("foo")
def test_static_method_call(node): value = node.static_meth("foo") assert value == GDString("static:foo")
def test_iter(): items = [GDString("foo"), 0, OS] v = Array(items) items_from_v = [x for x in v] assert items_from_v == items
def test_getitem(): v = Array(["foo", 0, OS, 0.42]) assert v[0] == GDString("foo") assert v[1] == 0 assert v[-1] == 0.42
def _to_string(self): # Implemented for test_bindings::test_virtual_to_string_customize return GDString("<Main Node>")
def generate_value(self): return GDString(str(self.random.random()))
@pytest.mark.parametrize( "arg", [ Array(), PoolColorArray(), PoolVector3Array(), PoolVector2Array(), PoolStringArray(), PoolRealArray(), PoolIntArray(), PoolByteArray(), [], (), [42, 43, 44], (GDString("foo"), GDString("bar"), GDString("spam")), (OS, ), [Vector2(), Vector2(), Vector2()], (OS, Vector2(), GDString("foo"), 0), # Enjoy the mix ], ) def test_instantiate_from_copy(arg): v = Array(arg) assert list(v) == list(arg) original_len = len(arg) v.append(42) assert len(arg) == original_len assert len(v) == original_len + 1 @pytest.mark.parametrize(
def test_values(): v = Dictionary({"a": 1, 2: "foo"}) values = v.values() assert list(values) == [1, GDString("foo")]
def test_items(): v = Dictionary({"a": 1, 2: "foo"}) items = v.items() assert list(items) == [(GDString("a"), 1), (2, GDString("foo"))]
def test_keys(): v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()}) keys = v.keys() assert list(keys) == [GDString("a"), 2, 0.5]
def test_iter(): v = Dictionary({"a": 1, 2: "foo", 0.5: Vector2()}) items = [GDString("a"), 2, 0.5] items_from_v = [x for x in v] assert items_from_v == items
def test_base(): assert GDString().empty() # Todo later: GDString creation from GD types: Vector2/3, Transform, Plane, Quat, AABB, Color, ... s = GDString("12") assert s.begins_with(GDString("1")) assert s.bigrams().size() == 1 assert GDString("\ta").dedent() == GDString("a") assert s.ends_with(GDString("2")) abc = GDString("abc") abc.erase(1, 1) assert abc == GDString("ac") assert GDString("abc").capitalize() == GDString("Abc") assert GDString("abc").find(GDString("b")) == 1 assert GDString("file.ext").get_extension() == GDString("ext") assert GDString("127.0.0.1").is_valid_ip_address() assert not GDString("127.0.0.1.xxx").is_valid_ip_address() assert GDString("abc").length() == 3 assert GDString("3.14").to_float() == pytest.approx(3.14) assert GDString("42").to_int() == 42
def test_overloaded_method_call(subnode): ret = subnode.overloaded_by_child_meth("foo") assert ret == GDString("sub:foo")
def test_init(): v1 = NodePath("parent/child") v2 = NodePath(GDString("parent/child")) assert v1 == v2