From d5fab0b9f813396500acbf46d6b7fdf2c5bc5b73 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Mon, 14 Aug 2023 18:44:30 -0500 Subject: [PATCH] Add automated tests to verify some previous fixes --- test/project/main.gd | 43 ++++++++++++++++++++++++++++++++++++++ test/project/project.godot | 2 +- test/src/example.cpp | 43 ++++++++++++++++++++++++++++++++++++++ test/src/example.h | 13 ++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) diff --git a/test/project/main.gd b/test/project/main.gd index 9716bc06..473c15fe 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 eafcad30..3ed679b4 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 af3837a1..8a761a4c 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(p_object) != nullptr; +} + +bool Example::test_object_cast_to_control(Object *p_object) const { + return Object::cast_to(p_object) != nullptr; +} + +bool Example::test_object_cast_to_example(Object *p_object) const { + return Object::cast_to(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 &p_tileset) const { + p_tilemap->set_tileset(p_tileset); +} + BitField Example::test_bitfield(BitField flags) { return flags; } diff --git a/test/src/example.h b/test/src/example.h index ce64c31f..6e00b7f5 100644 --- a/test/src/example.h +++ b/test/src/example.h @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include #include @@ -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 &p_tileset) const; + BitField test_bitfield(BitField flags); // RPC