-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade client active object mgr tests to Catch2 #13609
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,13 @@ with this program; if not, write to the Free Software Foundation, Inc., | |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
#include "client/activeobjectmgr.h" | ||
#include <algorithm> | ||
#include "test.h" | ||
|
||
#include "profiler.h" | ||
#include "client/activeobjectmgr.h" | ||
|
||
#include <catch.hpp> | ||
|
||
#include <utility> | ||
|
||
class TestClientActiveObject : public ClientActiveObject | ||
{ | ||
|
@@ -58,85 +60,74 @@ class TestClientActiveObjectMgr : public TestBase | |
|
||
void runTests(IGameDef *gamedef); | ||
|
||
void testFreeID(); | ||
void testRegisterObject(); | ||
void testRemoveObject(); | ||
void testGetActiveSelectableObjects(); | ||
}; | ||
|
||
static TestClientActiveObjectMgr g_test_instance; | ||
|
||
void TestClientActiveObjectMgr::runTests(IGameDef *gamedef) | ||
{ | ||
TEST(testFreeID); | ||
TEST(testRegisterObject) | ||
TEST(testRemoveObject) | ||
TEST(testGetActiveSelectableObjects) | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
void TestClientActiveObjectMgr::testFreeID() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the plan if any converted tests need to access the gamedef? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. The plan is to move the |
||
{ | ||
client::ActiveObjectMgr caomgr; | ||
std::vector<u16> aoids; | ||
|
||
u16 aoid = caomgr.getFreeId(); | ||
// Ensure it's not the same id | ||
UASSERT(caomgr.getFreeId() != aoid); | ||
|
||
aoids.push_back(aoid); | ||
|
||
// Register basic objects, ensure we never found | ||
for (u8 i = 0; i < UINT8_MAX; i++) { | ||
// Register an object | ||
auto tcao_u = std::make_unique<TestClientActiveObject>(); | ||
auto tcao = tcao_u.get(); | ||
caomgr.registerObject(std::move(tcao_u)); | ||
aoids.push_back(tcao->getId()); | ||
|
||
// Ensure next id is not in registered list | ||
UASSERT(std::find(aoids.begin(), aoids.end(), caomgr.getFreeId()) == | ||
aoids.end()); | ||
} | ||
|
||
caomgr.clear(); | ||
static TestClientActiveObject* register_default_test_object( | ||
client::ActiveObjectMgr &caomgr) { | ||
auto test_obj = std::make_unique<TestClientActiveObject>(); | ||
auto result = test_obj.get(); | ||
REQUIRE(caomgr.registerObject(std::move(test_obj))); | ||
return result; | ||
} | ||
|
||
void TestClientActiveObjectMgr::testRegisterObject() | ||
TEST_CASE("test client active object manager") | ||
{ | ||
client::ActiveObjectMgr caomgr; | ||
auto tcao_u = std::make_unique<TestClientActiveObject>(); | ||
auto tcao = tcao_u.get(); | ||
UASSERT(caomgr.registerObject(std::move(tcao_u))); | ||
|
||
u16 id = tcao->getId(); | ||
|
||
auto tcaoToCompare = caomgr.getActiveObject(id); | ||
UASSERT(tcaoToCompare->getId() == id); | ||
UASSERT(tcaoToCompare == tcao); | ||
|
||
tcao_u = std::make_unique<TestClientActiveObject>(); | ||
tcao = tcao_u.get(); | ||
UASSERT(caomgr.registerObject(std::move(tcao_u))); | ||
UASSERT(caomgr.getActiveObject(tcao->getId()) == tcao); | ||
UASSERT(caomgr.getActiveObject(tcao->getId()) != tcaoToCompare); | ||
|
||
caomgr.clear(); | ||
} | ||
|
||
void TestClientActiveObjectMgr::testRemoveObject() | ||
{ | ||
client::ActiveObjectMgr caomgr; | ||
auto tcao_u = std::make_unique<TestClientActiveObject>(); | ||
auto tcao = tcao_u.get(); | ||
UASSERT(caomgr.registerObject(std::move(tcao_u))); | ||
auto tcao1 = register_default_test_object(caomgr); | ||
|
||
SECTION("When we register a client object, " | ||
"then it should be assigned a unique ID.") | ||
{ | ||
for (int i = 0; i < UINT8_MAX; ++i) { | ||
auto other_tcao = register_default_test_object(caomgr); | ||
CHECK(other_tcao->getId() != tcao1->getId()); | ||
} | ||
} | ||
|
||
u16 id = tcao->getId(); | ||
UASSERT(caomgr.getActiveObject(id) != nullptr) | ||
SECTION("two registered objects") | ||
{ | ||
auto tcao2 = register_default_test_object(caomgr); | ||
auto tcao2_id = tcao2->getId(); | ||
|
||
auto obj1 = caomgr.getActiveObject(tcao1->getId()); | ||
REQUIRE(obj1 != nullptr); | ||
|
||
auto obj2 = caomgr.getActiveObject(tcao2_id); | ||
REQUIRE(obj2 != nullptr); | ||
|
||
SECTION("When we query an object by its ID, " | ||
"then we should get back an object with that ID.") | ||
{ | ||
CHECK(obj1->getId() == tcao1->getId()); | ||
CHECK(obj2->getId() == tcao2->getId()); | ||
} | ||
|
||
SECTION("When we register and query for an object, " | ||
"its memory location should not have changed.") | ||
{ | ||
CHECK(obj1 == tcao1); | ||
CHECK(obj2 == tcao2); | ||
} | ||
} | ||
|
||
caomgr.removeObject(tcao->getId()); | ||
UASSERT(caomgr.getActiveObject(id) == nullptr) | ||
SECTION("Given an object has been removed, " | ||
"when we query for it, " | ||
"then we should get nullptr.") | ||
{ | ||
auto id = tcao1->getId(); | ||
caomgr.removeObject(tcao1->getId()); // may invalidate tcao1 | ||
CHECK(caomgr.getActiveObject(id) == nullptr); | ||
} | ||
|
||
caomgr.clear(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this just runs the other tests unconditionally after the normal ones with no integration with
--test-module