Files
anm2ed/tests/common.hpp
T
shweetandClaude Opus 5.5 8ef089d306 Isaac compatibility: Convert and Save bridges what the game can't show
The Isaac prompt offers Convert and Save, Ignore and Save, or Cancel; the
document remembers the choice (IsaacIgnored / "isaacIgnored", editor-only)
for saves that don't ask (the warning off, or `convert`). Converting:
- depth: a copy of the layer per depth ("body (depth +1)"), standing where
  the editor draws it and showing only that depth's frames
- alpha offsets join the alpha tint (the same on opaque pixels)
- skew, blur and shaders: each layer frame using one is drawn alone at 1x
  into <name>.baked.png beside the save (each image once), on a copy of its
  layer ("body (baked)") right after it, one frame per step; a skewed root
  or group root goes into its layers' images. `convert` makes a headless
  OpenGL context for it; without one (or if the sheet can't be written)
  these are dropped with a warning. Autosaves don't bake.
Tests check the bridged copy draws as the editor does, frame by frame.

Purge Default Color Values is gone: XML saves always leave out tints of 255
and color offsets of 0, in both copies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 22:59:35 -04:00

119 lines
4.7 KiB
C++

#pragma once
#include <doctest.h>
#include <algorithm>
#include <cstdlib>
#include <format>
#include <fstream>
#include <sstream>
#include "edit/edit.hpp"
#include "model/json.hpp"
#include "model/xml.hpp"
namespace anm2ed::test
{
inline const std::filesystem::path TEST_DIR = ANM2ED_TEST_DIR;
inline const std::filesystem::path CORPUS_DIR = TEST_DIR / "corpus";
inline const std::filesystem::path GOLDEN_DIR = TEST_DIR / "golden";
inline constexpr std::pair<const char*, Flags> PRESETS[] = {{"default", SERIALIZE_DEFAULT},
{"editor", SERIALIZE_EDITOR_DEFAULT},
{"isaac", SERIALIZE_ISAAC_DEFAULT},
{"anm2ed", SERIALIZE_ANM2ED_DEFAULT}};
inline constexpr std::pair<const char*, types::merge::Type> MERGES[] = {{"merge_prepend", types::merge::PREPEND},
{"merge_append", types::merge::APPEND},
{"merge_replace", types::merge::REPLACE},
{"merge_ignore", types::merge::IGNORE}};
using Variant = std::pair<std::string, std::string>;
inline std::string file_load(const std::filesystem::path& path)
{
std::ifstream file(path, std::ios::binary);
return {std::istreambuf_iterator<char>(file), {}};
}
inline void file_save(const std::filesystem::path& path, const std::string& text)
{
std::filesystem::create_directories(path.parent_path());
std::ofstream(path, std::ios::binary) << text;
}
inline bool is_env_set(const char* name) { return std::getenv(name) != nullptr; }
inline std::vector<std::filesystem::path> fixtures_get()
{
std::vector<std::filesystem::path> fixtures{};
for (const auto& entry : std::filesystem::directory_iterator(CORPUS_DIR))
if (entry.path().extension() == ".anm2") fixtures.push_back(entry.path());
std::ranges::sort(fixtures);
return fixtures;
}
inline model::Model model_load(const std::string& text)
{
model::Model model{};
REQUIRE(model::model_load_string(model, text));
return model;
}
// Every serialization and merge the editor can produce for a document; goldens lock each one down.
inline std::vector<Variant> variants_get(const model::Model& model)
{
std::vector<Variant> variants{};
for (auto [name, flags] : PRESETS)
variants.emplace_back(name, model::model_serialize(model, flags));
variants.emplace_back("save", model::model_to_string(model));
for (auto [name, type] : MERGES)
{
auto merged = model;
std::set<int> sources{0, 1, 2};
edit::animations_merge(merged, 0, sources, type, type == types::merge::PREPEND || type == types::merge::REPLACE,
{});
variants.emplace_back(name, model::model_serialize(merged, SERIALIZE_ANM2ED_DEFAULT));
}
return variants;
}
// The outer document of a save with its SourceDocument (the root's last child, on lines of its own) removed: what
// the game reads.
inline std::string game_document_get(std::string text)
{
constexpr std::string_view END = "</SourceDocument>";
auto start = text.find("\n <SourceDocument");
if (start != std::string::npos) text.erase(start, text.find(END, start) + END.size() - start);
return text;
}
// Save/reload properties every document must hold.
inline void invariants_check(const model::Model& model)
{
// The first save may repair invalid input (dangling ids, duplicates); after that, saves must be fixed points.
auto text = model::model_to_string(model);
auto reloaded = model_load(text);
auto resaved = model::model_to_string(reloaded);
CHECK(resaved == text);
CHECK(model::model_hash(model_load(resaved)) == model::model_hash(reloaded));
// Loading only the game's view and re-saving it for the game must not change it.
auto game = game_document_get(model::model_to_string(model));
CHECK(model::model_serialize(model_load(game), SERIALIZE_ISAAC_DEFAULT | SERIALIZE_PURGE_DEFAULT_COLORS) == game);
}
// Compares against a stored golden; ANM2ED_UPDATE_GOLDEN (or a missing golden when isBootstrap) rewrites it.
inline void golden_check(const std::filesystem::path& path, const std::string& text, bool isBootstrap = false)
{
if (is_env_set("ANM2ED_UPDATE_GOLDEN") || (isBootstrap && !std::filesystem::exists(path)))
{
file_save(path, text);
return;
}
INFO("golden: ", path.string());
REQUIRE(std::filesystem::exists(path));
CHECK(file_load(path) == text);
}
}