Skip to content

Commit

Permalink
Add automated tests to verify some previous fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dsnopek committed Aug 15, 2023
1 parent 5eebc6b commit 04e55e7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
43 changes: 43 additions & 0 deletions test/project/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/project/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
43 changes: 43 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions test/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <godot_cpp/classes/image.hpp>
#include <godot_cpp/classes/input_event_key.hpp>
#include <godot_cpp/classes/viewport.hpp>
#include <godot_cpp/classes/tile_map.hpp>
#include <godot_cpp/classes/tile_set.hpp>

#include <godot_cpp/core/binder_common.hpp>

Expand Down Expand Up @@ -121,6 +123,17 @@ class Example : public Control {
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
Expand Down

0 comments on commit 04e55e7

Please sign in to comment.