Skip to content

Commit 04e55e7

Browse files
committed
Add automated tests to verify some previous fixes
1 parent 5eebc6b commit 04e55e7

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

test/project/main.gd

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,49 @@ func _ready():
9797
example.group_subgroup_custom_position = Vector2(50, 50)
9898
assert_equal(example.group_subgroup_custom_position, Vector2(50, 50))
9999

100+
# Test Object::cast_to<>() and that correct wrappers are being used.
101+
var control = Control.new()
102+
var sprite = Sprite2D.new()
103+
var example_ref = ExampleRef.new()
104+
105+
assert_equal(example.test_object_cast_to_node(control), true)
106+
assert_equal(example.test_object_cast_to_control(control), true)
107+
assert_equal(example.test_object_cast_to_example(control), false)
108+
109+
assert_equal(example.test_object_cast_to_node(example), true)
110+
assert_equal(example.test_object_cast_to_control(example), true)
111+
assert_equal(example.test_object_cast_to_example(example), true)
112+
113+
assert_equal(example.test_object_cast_to_node(sprite), true)
114+
assert_equal(example.test_object_cast_to_control(sprite), false)
115+
assert_equal(example.test_object_cast_to_example(sprite), false)
116+
117+
assert_equal(example.test_object_cast_to_node(example_ref), false)
118+
assert_equal(example.test_object_cast_to_control(example_ref), false)
119+
assert_equal(example.test_object_cast_to_example(example_ref), false)
120+
121+
control.queue_free()
122+
sprite.queue_free()
123+
124+
# Test conversions to and from Variant.
125+
assert_equal(example.test_variant_vector2i_conversion(Vector2i(1, 1)), Vector2i(1, 1))
126+
assert_equal(example.test_variant_vector2i_conversion(Vector2(1.0, 1.0)), Vector2i(1, 1))
127+
assert_equal(example.test_variant_int_conversion(10), 10)
128+
assert_equal(example.test_variant_int_conversion(10.0), 10)
129+
assert_equal(example.test_variant_float_conversion(10.0), 10.0)
130+
assert_equal(example.test_variant_float_conversion(10), 10.0)
131+
132+
# Test that ptrcalls from GDExtension to the engine are correctly encoding Object and RefCounted.
133+
var new_node = Node.new()
134+
example.test_add_child(new_node)
135+
assert_equal(new_node.get_parent(), example)
136+
137+
var new_tileset = TileSet.new()
138+
var new_tilemap = TileMap.new()
139+
example.test_set_tileset(new_tilemap, new_tileset)
140+
assert_equal(new_tilemap.tile_set, new_tileset)
141+
new_tilemap.queue_free()
142+
100143
# Constants.
101144
assert_equal(Example.FIRST, 0)
102145
assert_equal(Example.ANSWER_TO_EVERYTHING, 42)

test/project/project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ config_version=5
1212

1313
config/name="GDExtension Test Project"
1414
run/main_scene="res://main.tscn"
15-
config/features=PackedStringArray("4.1")
15+
config/features=PackedStringArray("4.2")
1616
config/icon="res://icon.png"
1717

1818
[native_extensions]

test/src/example.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ void Example::_bind_methods() {
142142
ClassDB::bind_method(D_METHOD("test_string_resize"), &Example::test_string_resize);
143143
ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
144144

145+
ClassDB::bind_method(D_METHOD("test_object_cast_to_node", "object"), &Example::test_object_cast_to_node);
146+
ClassDB::bind_method(D_METHOD("test_object_cast_to_control", "object"), &Example::test_object_cast_to_control);
147+
ClassDB::bind_method(D_METHOD("test_object_cast_to_example", "object"), &Example::test_object_cast_to_example);
148+
149+
ClassDB::bind_method(D_METHOD("test_variant_vector2i_conversion", "variant"), &Example::test_variant_vector2i_conversion);
150+
ClassDB::bind_method(D_METHOD("test_variant_int_conversion", "variant"), &Example::test_variant_int_conversion);
151+
ClassDB::bind_method(D_METHOD("test_variant_float_conversion", "variant"), &Example::test_variant_float_conversion);
152+
153+
ClassDB::bind_method(D_METHOD("test_add_child", "node"), &Example::test_add_child);
154+
ClassDB::bind_method(D_METHOD("test_set_tileset", "tilemap", "tileset"), &Example::test_set_tileset);
155+
145156
ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield);
146157

147158
ClassDB::bind_method(D_METHOD("test_rpc", "value"), &Example::test_rpc);
@@ -359,6 +370,38 @@ Example *Example::test_node_argument(Example *p_node) const {
359370
return p_node;
360371
}
361372

373+
bool Example::test_object_cast_to_node(Object *p_object) const {
374+
return Object::cast_to<Node>(p_object) != nullptr;
375+
}
376+
377+
bool Example::test_object_cast_to_control(Object *p_object) const {
378+
return Object::cast_to<Control>(p_object) != nullptr;
379+
}
380+
381+
bool Example::test_object_cast_to_example(Object *p_object) const {
382+
return Object::cast_to<Example>(p_object) != nullptr;
383+
}
384+
385+
Vector2i Example::test_variant_vector2i_conversion(const Variant &p_variant) const {
386+
return p_variant;
387+
}
388+
389+
int Example::test_variant_int_conversion(const Variant &p_variant) const {
390+
return p_variant;
391+
}
392+
393+
float Example::test_variant_float_conversion(const Variant &p_variant) const {
394+
return p_variant;
395+
}
396+
397+
void Example::test_add_child(Node *p_node) {
398+
add_child(p_node);
399+
}
400+
401+
void Example::test_set_tileset(TileMap *p_tilemap, const Ref<TileSet> &p_tileset) const {
402+
p_tilemap->set_tileset(p_tileset);
403+
}
404+
362405
BitField<Example::Flags> Example::test_bitfield(BitField<Flags> flags) {
363406
return flags;
364407
}

test/src/example.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <godot_cpp/classes/image.hpp>
2020
#include <godot_cpp/classes/input_event_key.hpp>
2121
#include <godot_cpp/classes/viewport.hpp>
22+
#include <godot_cpp/classes/tile_map.hpp>
23+
#include <godot_cpp/classes/tile_set.hpp>
2224

2325
#include <godot_cpp/core/binder_common.hpp>
2426

@@ -121,6 +123,17 @@ class Example : public Control {
121123
String test_string_resize(String p_original) const;
122124
int test_vector_ops() const;
123125

126+
bool test_object_cast_to_node(Object *p_object) const;
127+
bool test_object_cast_to_control(Object *p_object) const;
128+
bool test_object_cast_to_example(Object *p_object) const;
129+
130+
Vector2i test_variant_vector2i_conversion(const Variant &p_variant) const;
131+
int test_variant_int_conversion(const Variant &p_variant) const;
132+
float test_variant_float_conversion(const Variant &p_variant) const;
133+
134+
void test_add_child(Node *p_node);
135+
void test_set_tileset(TileMap *p_tilemap, const Ref<TileSet> &p_tileset) const;
136+
124137
BitField<Flags> test_bitfield(BitField<Flags> flags);
125138

126139
// RPC

0 commit comments

Comments
 (0)