From b2e38f3c4bffef854720cdbb8fe0d78f470503d7 Mon Sep 17 00:00:00 2001 From: jonas Date: Fri, 20 Sep 2024 11:19:53 +0200 Subject: [PATCH] Add unit tests for PhysicsMaterial --- tests/scene/test_physics_material.h | 107 ++++++++++++++++++++++++++++ tests/test_main.cpp | 1 + 2 files changed, 108 insertions(+) create mode 100644 tests/scene/test_physics_material.h diff --git a/tests/scene/test_physics_material.h b/tests/scene/test_physics_material.h new file mode 100644 index 00000000000..a078166f424 --- /dev/null +++ b/tests/scene/test_physics_material.h @@ -0,0 +1,107 @@ +/**************************************************************************/ +/* test_physics_material.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef TEST_PHYSICS_MATERIAL_H +#define TEST_PHYSICS_MATERIAL_H + +#include "scene/resources/physics_material.h" +#include "tests/test_macros.h" + +namespace TestPhysics_material { + +TEST_CASE("[Physics_material] Defaults") { + Ref physics_material; + physics_material.instantiate(); + + CHECK(physics_material->get_friction() == 1.); + CHECK(physics_material->is_rough() == false); + CHECK(physics_material->get_bounce() == 0.); + CHECK(physics_material->is_absorbent() == false); +} + +TEST_CASE("[Physics_material] Friction") { + Ref physics_material; + physics_material.instantiate(); + + real_t friction = 0.314; + physics_material->set_friction(friction); + CHECK(physics_material->get_friction() == friction); +} + +TEST_CASE("[Physics_material] Rough") { + Ref physics_material; + physics_material.instantiate(); + + bool rough = true; + physics_material->set_rough(rough); + CHECK(physics_material->is_rough() == rough); + + real_t friction = 0.314; + physics_material->set_friction(friction); + CHECK(physics_material->computed_friction() == -friction); + + rough = false; + physics_material->set_rough(rough); + CHECK(physics_material->is_rough() == rough); + + CHECK(physics_material->computed_friction() == friction); +} + +TEST_CASE("[Physics_material] Bounce") { + Ref physics_material; + physics_material.instantiate(); + + real_t bounce = 0.271; + physics_material->set_bounce(bounce); + CHECK(physics_material->get_bounce() == bounce); +} + +TEST_CASE("[Physics_material] Absorbent") { + Ref physics_material; + physics_material.instantiate(); + + bool absorbent = true; + physics_material->set_absorbent(absorbent); + CHECK(physics_material->is_absorbent() == absorbent); + + real_t bounce = 0.271; + physics_material->set_bounce(bounce); + CHECK(physics_material->computed_bounce() == -bounce); + + absorbent = false; + physics_material->set_absorbent(absorbent); + CHECK(physics_material->is_absorbent() == absorbent); + + CHECK(physics_material->computed_bounce() == bounce); +} + +} // namespace TestPhysics_material + +#endif // TEST_PHYSICS_MATERIAL_H diff --git a/tests/test_main.cpp b/tests/test_main.cpp index 14a78558322..cc93c20d7f1 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -125,6 +125,7 @@ #include "tests/scene/test_parallax_2d.h" #include "tests/scene/test_path_2d.h" #include "tests/scene/test_path_follow_2d.h" +#include "tests/scene/test_physics_material.h" #include "tests/scene/test_sprite_frames.h" #include "tests/scene/test_style_box_texture.h" #include "tests/scene/test_theme.h"