Test that internal classes work as expected

This commit is contained in:
David Snopek
2025-07-14 14:04:14 -05:00
parent 134e8b756d
commit 309b17b6eb
8 changed files with 61 additions and 5 deletions

View File

@@ -287,6 +287,13 @@ func _ready():
assert_equal(library_path, ProjectSettings.globalize_path(library_path))
assert_equal(FileAccess.file_exists(library_path), true)
# Test that internal classes work as expected (at least for Godot 4.5+).
assert_equal(ClassDB.can_instantiate("ExampleInternal"), false)
assert_equal(ClassDB.instantiate("ExampleInternal"), null)
var internal_class = example.test_get_internal_class()
assert_equal(internal_class.get_the_answer(), 42)
assert_equal(internal_class.get_class(), "ExampleInternal")
# Test a class with a unicode name.
var przykład = ExamplePrzykład.new()
assert_equal(przykład.get_the_word(), "słowo to przykład")

View File

@@ -252,6 +252,8 @@ void Example::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_use_engine_singleton"), &Example::test_use_engine_singleton);
ClassDB::bind_method(D_METHOD("test_get_internal_class"), &Example::test_get_internal_class);
ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
@@ -744,6 +746,12 @@ String Example::test_library_path() {
return library_path;
}
Ref<RefCounted> Example::test_get_internal_class() const {
Ref<ExampleInternal> it;
it.instantiate();
return it;
}
int64_t Example::test_get_internal(const Variant &p_input) const {
if (p_input.get_type() != Variant::INT) {
return -1;
@@ -779,3 +787,11 @@ void ExamplePrzykład::_bind_methods() {
String ExamplePrzykład::get_the_word() const {
return U"słowo to przykład";
}
void ExampleInternal::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_the_answer"), &ExampleInternal::get_the_answer);
}
int ExampleInternal::get_the_answer() const {
return 42;
}

View File

@@ -29,6 +29,8 @@
using namespace godot;
class ExampleInternal;
class ExampleRef : public RefCounted {
GDCLASS(ExampleRef, RefCounted);
@@ -203,6 +205,8 @@ public:
String test_use_engine_singleton() const;
static String test_library_path();
Ref<RefCounted> test_get_internal_class() const;
};
VARIANT_ENUM_CAST(Example::Constants);
@@ -288,3 +292,13 @@ protected:
public:
String get_the_word() const;
};
class ExampleInternal : public RefCounted {
GDCLASS(ExampleInternal, RefCounted);
protected:
static void _bind_methods();
public:
int get_the_answer() const;
};

View File

@@ -31,6 +31,7 @@ void initialize_example_module(ModuleInitializationLevel p_level) {
GDREGISTER_CLASS(ExampleChild);
GDREGISTER_RUNTIME_CLASS(ExampleRuntime);
GDREGISTER_CLASS(ExamplePrzykład);
GDREGISTER_INTERNAL_CLASS(ExampleInternal);
}
void uninitialize_example_module(ModuleInitializationLevel p_level) {