summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--test/project/main.gd43
-rw-r--r--test/project/project.godot2
-rw-r--r--test/src/example.cpp43
-rw-r--r--test/src/example.h13
4 files changed, 100 insertions, 1 deletions
diff --git a/test/project/main.gd b/test/project/main.gd
index 9716bc0..473c15f 100644
--- a/test/project/main.gd
+++ b/test/project/main.gd
@@ -97,6 +97,49 @@ func _ready():
example.group_subgroup_custom_position = Vector2(50, 50)
assert_equal(example.group_subgroup_custom_position, Vector2(50, 50))
+ # Test Object::cast_to<>() and that correct wrappers are being used.
+ var control = Control.new()
+ var sprite = Sprite2D.new()
+ var example_ref = ExampleRef.new()
+
+ assert_equal(example.test_object_cast_to_node(control), true)
+ assert_equal(example.test_object_cast_to_control(control), true)
+ assert_equal(example.test_object_cast_to_example(control), false)
+
+ assert_equal(example.test_object_cast_to_node(example), true)
+ assert_equal(example.test_object_cast_to_control(example), true)
+ assert_equal(example.test_object_cast_to_example(example), true)
+
+ assert_equal(example.test_object_cast_to_node(sprite), true)
+ assert_equal(example.test_object_cast_to_control(sprite), false)
+ assert_equal(example.test_object_cast_to_example(sprite), false)
+
+ assert_equal(example.test_object_cast_to_node(example_ref), false)
+ assert_equal(example.test_object_cast_to_control(example_ref), false)
+ assert_equal(example.test_object_cast_to_example(example_ref), false)
+
+ control.queue_free()
+ sprite.queue_free()
+
+ # Test conversions to and from Variant.
+ assert_equal(example.test_variant_vector2i_conversion(Vector2i(1, 1)), Vector2i(1, 1))
+ assert_equal(example.test_variant_vector2i_conversion(Vector2(1.0, 1.0)), Vector2i(1, 1))
+ assert_equal(example.test_variant_int_conversion(10), 10)
+ assert_equal(example.test_variant_int_conversion(10.0), 10)
+ assert_equal(example.test_variant_float_conversion(10.0), 10.0)
+ assert_equal(example.test_variant_float_conversion(10), 10.0)
+
+ # Test that ptrcalls from GDExtension to the engine are correctly encoding Object and RefCounted.
+ var new_node = Node.new()
+ example.test_add_child(new_node)
+ assert_equal(new_node.get_parent(), example)
+
+ var new_tileset = TileSet.new()
+ var new_tilemap = TileMap.new()
+ example.test_set_tileset(new_tilemap, new_tileset)
+ assert_equal(new_tilemap.tile_set, new_tileset)
+ new_tilemap.queue_free()
+
# Constants.
assert_equal(Example.FIRST, 0)
assert_equal(Example.ANSWER_TO_EVERYTHING, 42)
diff --git a/test/project/project.godot b/test/project/project.godot
index eafcad3..3ed679b 100644
--- a/test/project/project.godot
+++ b/test/project/project.godot
@@ -12,7 +12,7 @@ config_version=5
config/name="GDExtension Test Project"
run/main_scene="res://main.tscn"
-config/features=PackedStringArray("4.1")
+config/features=PackedStringArray("4.2")
config/icon="res://icon.png"
[native_extensions]
diff --git a/test/src/example.cpp b/test/src/example.cpp
index af3837a..8a761a4 100644
--- a/test/src/example.cpp
+++ b/test/src/example.cpp
@@ -142,6 +142,17 @@ void Example::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_string_resize"), &Example::test_string_resize);
ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
+ ClassDB::bind_method(D_METHOD("test_object_cast_to_node", "object"), &Example::test_object_cast_to_node);
+ ClassDB::bind_method(D_METHOD("test_object_cast_to_control", "object"), &Example::test_object_cast_to_control);
+ ClassDB::bind_method(D_METHOD("test_object_cast_to_example", "object"), &Example::test_object_cast_to_example);
+
+ ClassDB::bind_method(D_METHOD("test_variant_vector2i_conversion", "variant"), &Example::test_variant_vector2i_conversion);
+ ClassDB::bind_method(D_METHOD("test_variant_int_conversion", "variant"), &Example::test_variant_int_conversion);
+ ClassDB::bind_method(D_METHOD("test_variant_float_conversion", "variant"), &Example::test_variant_float_conversion);
+
+ ClassDB::bind_method(D_METHOD("test_add_child", "node"), &Example::test_add_child);
+ ClassDB::bind_method(D_METHOD("test_set_tileset", "tilemap", "tileset"), &Example::test_set_tileset);
+
ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield);
ClassDB::bind_method(D_METHOD("test_rpc", "value"), &Example::test_rpc);
@@ -359,6 +370,38 @@ Example *Example::test_node_argument(Example *p_node) const {
return p_node;
}
+bool Example::test_object_cast_to_node(Object *p_object) const {
+ return Object::cast_to<Node>(p_object) != nullptr;
+}
+
+bool Example::test_object_cast_to_control(Object *p_object) const {
+ return Object::cast_to<Control>(p_object) != nullptr;
+}
+
+bool Example::test_object_cast_to_example(Object *p_object) const {
+ return Object::cast_to<Example>(p_object) != nullptr;
+}
+
+Vector2i Example::test_variant_vector2i_conversion(const Variant &p_variant) const {
+ return p_variant;
+}
+
+int Example::test_variant_int_conversion(const Variant &p_variant) const {
+ return p_variant;
+}
+
+float Example::test_variant_float_conversion(const Variant &p_variant) const {
+ return p_variant;
+}
+
+void Example::test_add_child(Node *p_node) {
+ add_child(p_node);
+}
+
+void Example::test_set_tileset(TileMap *p_tilemap, const Ref<TileSet> &p_tileset) const {
+ p_tilemap->set_tileset(p_tileset);
+}
+
BitField<Example::Flags> Example::test_bitfield(BitField<Flags> flags) {
return flags;
}
diff --git a/test/src/example.h b/test/src/example.h
index ce64c31..6e00b7f 100644
--- a/test/src/example.h
+++ b/test/src/example.h
@@ -18,6 +18,8 @@
#include <godot_cpp/classes/global_constants.hpp>
#include <godot_cpp/classes/image.hpp>
#include <godot_cpp/classes/input_event_key.hpp>
+#include <godot_cpp/classes/tile_map.hpp>
+#include <godot_cpp/classes/tile_set.hpp>
#include <godot_cpp/classes/viewport.hpp>
#include <godot_cpp/core/binder_common.hpp>
@@ -121,6 +123,17 @@ public:
String test_string_resize(String p_original) const;
int test_vector_ops() const;
+ bool test_object_cast_to_node(Object *p_object) const;
+ bool test_object_cast_to_control(Object *p_object) const;
+ bool test_object_cast_to_example(Object *p_object) const;
+
+ Vector2i test_variant_vector2i_conversion(const Variant &p_variant) const;
+ int test_variant_int_conversion(const Variant &p_variant) const;
+ float test_variant_float_conversion(const Variant &p_variant) const;
+
+ void test_add_child(Node *p_node);
+ void test_set_tileset(TileMap *p_tilemap, const Ref<TileSet> &p_tileset) const;
+
BitField<Flags> test_bitfield(BitField<Flags> flags);
// RPC