diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_command_queue.h | 16 | ||||
-rw-r--r-- | tests/test_object.h | 213 | ||||
-rw-r--r-- | tests/test_physics_3d.cpp | 14 |
3 files changed, 226 insertions, 17 deletions
diff --git a/tests/test_command_queue.h b/tests/test_command_queue.h index 2f0b75760d..b4fa63ad2b 100644 --- a/tests/test_command_queue.h +++ b/tests/test_command_queue.h @@ -122,8 +122,8 @@ public: int message_count_to_read = 0; bool exit_threads = false; - Thread *reader_thread = nullptr; - Thread *writer_thread = nullptr; + Thread reader_thread; + Thread writer_thread; int func1_count = 0; @@ -221,20 +221,16 @@ public: } void init_threads() { - reader_thread = Thread::create(&SharedThreadState::static_reader_thread_loop, this); - writer_thread = Thread::create(&SharedThreadState::static_writer_thread_loop, this); + reader_thread.start(&SharedThreadState::static_reader_thread_loop, this); + writer_thread.start(&SharedThreadState::static_writer_thread_loop, this); } void destroy_threads() { exit_threads = true; reader_threadwork.main_start_work(); writer_threadwork.main_start_work(); - Thread::wait_to_finish(reader_thread); - memdelete(reader_thread); - reader_thread = nullptr; - Thread::wait_to_finish(writer_thread); - memdelete(writer_thread); - writer_thread = nullptr; + reader_thread.wait_to_finish(); + writer_thread.wait_to_finish(); } }; diff --git a/tests/test_object.h b/tests/test_object.h index 7f310fc096..142d76553d 100644 --- a/tests/test_object.h +++ b/tests/test_object.h @@ -31,12 +31,103 @@ #ifndef TEST_OBJECT_H #define TEST_OBJECT_H +#include "core/core_string_names.h" #include "core/object/object.h" #include "thirdparty/doctest/doctest.h" +// Declared in global namespace because of GDCLASS macro warning (Windows): +// "Unqualified friend declaration referring to type outside of the nearest enclosing namespace +// is a Microsoft extension; add a nested name specifier". +class _TestDerivedObject : public Object { + GDCLASS(_TestDerivedObject, Object); + + int property_value; + +protected: + static void _bind_methods() { + ClassDB::bind_method(D_METHOD("set_property", "property"), &_TestDerivedObject::set_property); + ClassDB::bind_method(D_METHOD("get_property"), &_TestDerivedObject::get_property); + ADD_PROPERTY(PropertyInfo(Variant::INT, "property"), "set_property", "get_property"); + } + +public: + void set_property(int value) { property_value = value; } + int get_property() const { return property_value; } +}; + namespace TestObject { +class _MockScriptInstance : public ScriptInstance { + StringName property_name = "NO_NAME"; + Variant property_value; + +public: + bool set(const StringName &p_name, const Variant &p_value) override { + property_name = p_name; + property_value = p_value; + return true; + } + bool get(const StringName &p_name, Variant &r_ret) const override { + if (property_name == p_name) { + r_ret = property_value; + return true; + } + return false; + } + void get_property_list(List<PropertyInfo> *p_properties) const override { + } + Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid) const override { + return Variant::PACKED_FLOAT32_ARRAY; + } + void get_method_list(List<MethodInfo> *p_list) const override { + } + bool has_method(const StringName &p_method) const override { + return false; + } + Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) override { + return Variant(); + } + void notification(int p_notification) override { + } + Ref<Script> get_script() const override { + return Ref<Script>(); + } + Vector<ScriptNetData> get_rpc_methods() const override { + return Vector<ScriptNetData>(); + } + uint16_t get_rpc_method_id(const StringName &p_method) const override { + return 0; + } + StringName get_rpc_method(uint16_t p_id) const override { + return StringName(); + } + MultiplayerAPI::RPCMode get_rpc_mode_by_id(uint16_t p_id) const override { + return MultiplayerAPI::RPC_MODE_PUPPET; + } + MultiplayerAPI::RPCMode get_rpc_mode(const StringName &p_method) const override { + return MultiplayerAPI::RPC_MODE_PUPPET; + } + Vector<ScriptNetData> get_rset_properties() const override { + return Vector<ScriptNetData>(); + } + uint16_t get_rset_property_id(const StringName &p_variable) const override { + return 0; + } + StringName get_rset_property(uint16_t p_id) const override { + return StringName(); + } + MultiplayerAPI::RPCMode get_rset_mode_by_id(uint16_t p_id) const override { + return MultiplayerAPI::RPC_MODE_PUPPET; + } + MultiplayerAPI::RPCMode get_rset_mode(const StringName &p_variable) const override { + return MultiplayerAPI::RPC_MODE_PUPPET; + } + ScriptLanguage *get_language() override { + return nullptr; + } +}; + TEST_CASE("[Object] Core getters") { Object object; @@ -55,6 +146,15 @@ TEST_CASE("[Object] Core getters") { CHECK_MESSAGE( object.get_save_class() == "Object", "The returned save class should match the expected value."); + + List<String> inheritance_list; + object.get_inheritance_list_static(&inheritance_list); + CHECK_MESSAGE( + inheritance_list.size() == 1, + "The inheritance list should consist of Object only"); + CHECK_MESSAGE( + inheritance_list[0] == "Object", + "The inheritance list should consist of Object only"); } TEST_CASE("[Object] Metadata") { @@ -87,6 +187,119 @@ TEST_CASE("[Object] Metadata") { meta_list2.size() == 0, "The metadata list should contain 0 items after removing all metadata items."); } + +TEST_CASE("[Object] Construction") { + Object object; + + CHECK_MESSAGE( + !object.is_reference(), + "Object is not a Reference."); + + Object *p_db = ObjectDB::get_instance(object.get_instance_id()); + CHECK_MESSAGE( + p_db == &object, + "The database pointer returned by the object id should reference same object."); +} + +TEST_CASE("[Object] Script instance property setter") { + Object object; + _MockScriptInstance *script_instance = memnew(_MockScriptInstance); + object.set_script_instance(script_instance); + + bool valid = false; + object.set("some_name", 100, &valid); + CHECK(valid); + Variant actual_value; + CHECK_MESSAGE( + script_instance->get("some_name", actual_value), + "The assigned script instance should successfully retrieve value by name."); + CHECK_MESSAGE( + actual_value == Variant(100), + "The returned value should equal the one which was set by the object."); +} + +TEST_CASE("[Object] Script instance property getter") { + Object object; + _MockScriptInstance *script_instance = memnew(_MockScriptInstance); + script_instance->set("some_name", 100); // Make sure script instance has the property + object.set_script_instance(script_instance); + + bool valid = false; + const Variant &actual_value = object.get("some_name", &valid); + CHECK(valid); + CHECK_MESSAGE( + actual_value == Variant(100), + "The returned value should equal the one which was set by the script instance."); +} + +TEST_CASE("[Object] Built-in property setter") { + ClassDB::register_class<_TestDerivedObject>(); + _TestDerivedObject derived_object; + + bool valid = false; + derived_object.set("property", 100, &valid); + CHECK(valid); + CHECK_MESSAGE( + derived_object.get_property() == 100, + "The property value should equal the one which was set with built-in setter."); +} + +TEST_CASE("[Object] Built-in property getter") { + ClassDB::register_class<_TestDerivedObject>(); + _TestDerivedObject derived_object; + derived_object.set_property(100); + + bool valid = false; + const Variant &actual_value = derived_object.get("property", &valid); + CHECK(valid); + CHECK_MESSAGE( + actual_value == Variant(100), + "The returned value should equal the one which was set with built-in setter."); +} + +TEST_CASE("[Object] Script property setter") { + Object object; + Variant script; + + bool valid = false; + object.set(CoreStringNames::get_singleton()->_script, script, &valid); + CHECK(valid); + CHECK_MESSAGE( + object.get_script() == script, + "The object script should be equal to the assigned one."); +} + +TEST_CASE("[Object] Script property getter") { + Object object; + Variant script; + object.set_script(script); + + bool valid = false; + const Variant &actual_value = object.get(CoreStringNames::get_singleton()->_script, &valid); + CHECK(valid); + CHECK_MESSAGE( + actual_value == script, + "The returned value should be equal to the assigned script."); +} + +TEST_CASE("[Object] Absent name setter") { + Object object; + + bool valid = true; + object.set("absent_name", 100, &valid); + CHECK(!valid); +} + +TEST_CASE("[Object] Absent name getter") { + Object object; + + bool valid = true; + const Variant &actual_value = object.get("absent_name", &valid); + CHECK(!valid); + CHECK_MESSAGE( + actual_value == Variant(), + "The returned value should equal nil variant."); +} } // namespace TestObject #endif // TEST_OBJECT_H diff --git a/tests/test_physics_3d.cpp b/tests/test_physics_3d.cpp index a11140cfc3..f991fd7c86 100644 --- a/tests/test_physics_3d.cpp +++ b/tests/test_physics_3d.cpp @@ -59,7 +59,7 @@ class TestPhysics3DMainLoop : public MainLoop { RID character; - float ofs_x, ofs_y; + real_t ofs_x, ofs_y; Point2 joy_direction; @@ -115,7 +115,7 @@ protected: return b; } - void configure_body(RID p_body, float p_mass, float p_friction, float p_bounce) { + void configure_body(RID p_body, real_t p_mass, real_t p_friction, real_t p_bounce) { PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_MASS, p_mass); ps->body_set_param(p_body, PhysicsServer3D::BODY_PARAM_FRICTION, p_friction); @@ -211,8 +211,8 @@ protected: vs->instance_set_transform(triins, tritrans); } - void make_grid(int p_width, int p_height, float p_cellsize, float p_cellheight, const Transform &p_xform = Transform()) { - Vector<Vector<float>> grid; + void make_grid(int p_width, int p_height, real_t p_cellsize, real_t p_cellheight, const Transform &p_xform = Transform()) { + Vector<Vector<real_t>> grid; grid.resize(p_width); @@ -253,8 +253,8 @@ public: } if (mm.is_valid() && mm->get_button_mask() & 1) { - float y = -mm->get_relative().y / 20.0; - float x = mm->get_relative().x / 20.0; + real_t y = -mm->get_relative().y / 20.0; + real_t x = mm->get_relative().x / 20.0; if (mover.is_valid()) { PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); @@ -312,7 +312,7 @@ public: } virtual bool physics_process(float p_time) override { if (mover.is_valid()) { - static float joy_speed = 10; + static real_t joy_speed = 10; PhysicsServer3D *ps = PhysicsServer3D::get_singleton(); Transform t = ps->body_get_state(mover, PhysicsServer3D::BODY_STATE_TRANSFORM); t.origin += Vector3(joy_speed * joy_direction.x * p_time, -joy_speed * joy_direction.y * p_time, 0); |