test(datamodel): destruction

This commit is contained in:
maelstrom 2025-12-15 22:37:09 +01:00
parent fc52e2625c
commit b61362cc07
2 changed files with 31 additions and 0 deletions

View file

@ -15,12 +15,15 @@
#include <memory> #include <memory>
#include <optional> #include <optional>
int _dbgDataModelDestroyCount = 0;
DataModel::DataModel() DataModel::DataModel()
: Instance(&TYPE) { : Instance(&TYPE) {
this->name = "Place"; this->name = "Place";
} }
DataModel::~DataModel() { DataModel::~DataModel() {
_dbgDataModelDestroyCount++;
#ifndef NDEBUG #ifndef NDEBUG
printf("Datamodel successfully destroyed\n"); printf("Datamodel successfully destroyed\n");
#endif #endif

View file

@ -0,0 +1,28 @@
#include "objects/datamodel.h"
#include "objects/script.h"
#include <catch2/catch_test_macros.hpp>
extern int _dbgDataModelDestroyCount;
TEST_CASE("Datamodel destruction") {
// Ensure no cyclic-dependency causing datamodel to not be destructed
auto root = DataModel::New();
root->Init(true);
SECTION("Empty model") {
int prevCount = _dbgDataModelDestroyCount;
root = nullptr;
REQUIRE(_dbgDataModelDestroyCount == prevCount + 1);
}
SECTION("Model with script") {
auto s = Script::New();
root->AddChild(s);
s->source = "local x = game; wait(0)";
s->Run();
int prevCount = _dbgDataModelDestroyCount;
root = nullptr;
REQUIRE(_dbgDataModelDestroyCount == prevCount + 1);
}
}