#include #include #include #include #include #include "SDL_headless_fixture.hpp" namespace { struct CleanupState { int calls = 0; }; void SDLCALL CleanupPointerProperty(void* userdata, void* value) { auto* state = static_cast(userdata); if (state) { ++state->calls; } delete static_cast(value); } void SDLCALL CollectPropertyNames(void* userdata, SDL_PropertiesID, const char* name) { auto* names = static_cast*>(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(props) != 0); REQUIRE(static_cast(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 names; CHECK(props.EnumerateProperties(CollectPropertyNames, &names)); CHECK_FALSE(names.empty()); CHECK(props.LockProperties()); props.UnlockProperties(); auto global = hdk::sdl::Properties::GetGlobalProperties(); CHECK(static_cast(global) != 0); CHECK(props.ClearProperty("hdk.cleanup")); CHECK(cleanupState.calls >= 1); props.Destroy(); CHECK(static_cast(props) == 0); second.Destroy(); }