Files
hdk-sdl/test/Properties_test.cpp
2026-04-26 14:39:01 +00:00

96 lines
2.7 KiB
C++

#include <doctest/doctest.h>
#include <SDL3/SDL.h>
#include <hdk/sdl/Properties.hpp>
#include <string>
#include <vector>
#include "SDL_headless_fixture.hpp"
namespace {
struct CleanupState {
int calls = 0;
};
void SDLCALL CleanupPointerProperty(void* userdata, void* value) {
auto* state = static_cast<CleanupState*>(userdata);
if (state) {
++state->calls;
}
delete static_cast<int*>(value);
}
void SDLCALL CollectPropertyNames(void* userdata, SDL_PropertiesID, const char* name) {
auto* names = static_cast<std::vector<std::string>*>(userdata);
if (names && name) {
names->emplace_back(name);
}
}
} // namespace
TEST_CASE("Properties bindings are exercised") {
SDLSession sdl;
if (!sdl.IsInitialized()) {
INFO(SDL_GetError());
CHECK(false);
return;
}
auto props = hdk::sdl::Properties::Create();
auto second = hdk::sdl::Properties::Create();
REQUIRE(static_cast<SDL_PropertiesID>(props) != 0);
REQUIRE(static_cast<SDL_PropertiesID>(second) != 0);
CHECK(props.SetBooleanProperty("hdk.bool", true));
CHECK(props.GetBooleanProperty("hdk.bool", false));
CHECK(props.SetFloatProperty("hdk.float", 1.5f));
CHECK(props.GetFloatProperty("hdk.float", 0.0f) == doctest::Approx(1.5f));
CHECK(props.SetNumberProperty("hdk.number", 44));
CHECK(props.GetNumberProperty("hdk.number", 0) == 44);
int pointerValue = 77;
CHECK(props.SetPointerProperty("hdk.pointer", &pointerValue));
CHECK(props.GetPointerProperty("hdk.pointer", nullptr) == &pointerValue);
CHECK(props.SetStringProperty("hdk.string", "value"));
CHECK(std::string(props.GetStringProperty("hdk.string", "missing")) == "value");
CHECK(props.HasProperty("hdk.string"));
CHECK(props.GetPropertyType("hdk.string") == SDL_PROPERTY_TYPE_STRING);
CleanupState cleanupState;
CHECK(props.SetPointerPropertyWithCleanup("hdk.cleanup", new int(9), CleanupPointerProperty, &cleanupState));
CHECK(props.GetPropertyType("hdk.cleanup") == SDL_PROPERTY_TYPE_POINTER);
CHECK(props.CopyAllTo(second));
CHECK(std::string(second.GetStringProperty("hdk.string", "missing")) == "value");
CHECK(second.ClearProperty("hdk.string"));
CHECK_FALSE(second.HasProperty("hdk.string"));
CHECK(second.CopyAllFrom(props));
CHECK(second.HasProperty("hdk.string"));
std::vector<std::string> names;
CHECK(props.EnumerateProperties(CollectPropertyNames, &names));
CHECK_FALSE(names.empty());
CHECK(props.LockProperties());
props.UnlockProperties();
auto global = hdk::sdl::Properties::GetGlobalProperties();
CHECK(static_cast<SDL_PropertiesID>(global) != 0);
CHECK(props.ClearProperty("hdk.cleanup"));
CHECK(cleanupState.calls >= 1);
props.Destroy();
CHECK(static_cast<SDL_PropertiesID>(props) == 0);
second.Destroy();
}