MetaCore/tests/MetaCoreEditorPreferencesTests.cpp
2026-08-07 10:57:07 +08:00

56 lines
2.0 KiB
C++

#include "MetaCoreEditor/MetaCoreEditorPreferences.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string_view>
namespace {
int Failures = 0;
void Expect(bool condition, const char* message) {
if (!condition) {
std::cerr << "FAILED: " << message << '\n';
++Failures;
}
}
void WriteText(const std::filesystem::path& path, std::string_view value) {
std::filesystem::create_directories(path.parent_path());
std::ofstream output(path, std::ios::binary);
output << value;
}
} // namespace
int main() {
const auto temporary = std::filesystem::temp_directory_path() / "MetaCoreEditorPreferencesTests";
std::error_code fileError;
std::filesystem::remove_all(temporary, fileError);
const auto path = temporary / "editor_preferences.json";
const auto defaults = MetaCore::MetaCoreReadEditorPreferences(path);
Expect(defaults && defaults->GraphicsBackend == MetaCore::MetaCoreGraphicsBackend::OpenGL,
"missing preferences default to OpenGL");
MetaCore::MetaCoreEditorPreferences preferences;
preferences.GraphicsBackend = MetaCore::MetaCoreGraphicsBackend::Vulkan;
std::string error;
Expect(MetaCore::MetaCoreWriteEditorPreferences(path, preferences, &error), "write Vulkan preferences");
const auto roundTrip = MetaCore::MetaCoreReadEditorPreferences(path, &error);
Expect(roundTrip && roundTrip->GraphicsBackend == MetaCore::MetaCoreGraphicsBackend::Vulkan,
"Vulkan preferences round trip");
WriteText(path, R"({"format_version":1,"graphics_backend":"DirectX12"})");
Expect(!MetaCore::MetaCoreReadEditorPreferences(path, &error) && !error.empty(),
"unknown preferences backend rejected");
WriteText(path, R"({"format_version":0,"graphics_backend":"OpenGL"})");
Expect(!MetaCore::MetaCoreReadEditorPreferences(path, &error), "unsupported preferences version rejected");
std::filesystem::remove_all(temporary, fileError);
if (Failures == 0) std::cout << "MetaCoreEditorPreferencesTests passed\n";
return Failures == 0 ? 0 : 1;
}