Files
anm2ed/tests/invariants.cpp
T
shweetandClaude Opus 5.5 201a7c7cfc Replace tinyxml2 with pugixml
Parsing a 60 MB save drops from about 350 ms to 30 ms (a whole load from about
620 ms to 200). The reader keeps tinyxml2's rules for numbers and booleans
("%d"/"%f" on the leading text; a number or true/false), so every golden is
unchanged, and parse errors now give a line. SVG composing (pack and merge)
builds, clones and prints through pugixml. Tests check escaping by reading a
save back instead of comparing against tinyxml2's printer, and cut
SourceDocument from saves as text. pugixml is a submodule at v1.16; the About
window lists it, and simdjson, instead of tinyxml2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 21:57:39 -04:00

104 lines
4.4 KiB
C++

#include "common.hpp"
using namespace anm2ed;
using namespace anm2ed::test;
TEST_CASE("per-fixture invariants")
{
for (const auto& fixture : fixtures_get())
{
INFO("fixture: ", fixture.filename().string());
invariants_check(model_load(file_load(fixture)));
}
}
TEST_CASE("saved text escapes what it holds, and reads back the same")
{
auto model = model_load(file_load(CORPUS_DIR / "11_unknown.anm2"));
model.extras.children.emplace_back(
0, model::XmlNode{.tag = "Note",
.attributes = {{"Quote", "it's \"x\" & <y>"}},
.text = "a & b > c 'd' \"e\"",
.children = {{.tag = "Inner", .children = {{.tag = "Leaf", .attributes = {{"A", "1"}}}}}}});
auto text = model::model_to_string(model);
CHECK(text.find("Quote=\"it&apos;s &quot;x&quot; &amp; &lt;y&gt;\"") != std::string::npos);
CHECK(text.find(">a &amp; b &gt; c 'd' \"e\"<") != std::string::npos);
auto reloaded = model_load(text).extras.children;
auto note = std::ranges::find(reloaded, std::string("Note"), [](const auto& child) { return child.second.tag; });
REQUIRE(note != reloaded.end());
CHECK(note->second == model.extras.children.back().second);
}
TEST_CASE("purged default colors read back the same")
{
for (const auto& fixture : fixtures_get())
{
INFO("fixture: ", fixture.string());
auto model = model_load(file_load(fixture));
auto text = model::model_to_string(model, {.isPurgeDefaultColors = true});
CHECK(text.find("Tint=\"255\"") == std::string::npos);
CHECK(text.find("Offset=\"0\"") == std::string::npos);
CHECK(text.size() <= model::model_to_string(model).size());
CHECK(model::model_hash(model_load(text)) == model::model_hash(model));
}
}
TEST_CASE("JSON documents read back as the editor's copy")
{
for (const auto& fixture : fixtures_get())
{
INFO("fixture: ", fixture.string());
auto model = model_load(file_load(fixture));
auto text = model::model_to_json(model);
auto reloaded = model_load(text);
CHECK(model::model_to_json(reloaded) == text);
// Unknown XML, element order, dangling references and repeated animation names are not kept.
auto isQuirky = fixture.filename() == "09_legacy_quirks.anm2" || fixture.filename() == "11_unknown.anm2";
if (!isQuirky)
CHECK(model::model_serialize(reloaded, SERIALIZE_ANM2ED_DEFAULT) ==
model::model_serialize(model, SERIALIZE_ANM2ED_DEFAULT));
}
}
TEST_CASE("JSON animations get unique names")
{
auto model = model_load(file_load(CORPUS_DIR / "01_minimal.anm2"));
model.animations.entries.push_back(model.animations.entries.front());
auto reloaded = model_load(model::model_to_json(model));
REQUIRE(reloaded.animations_count_get() == 2);
CHECK(reloaded.animation_get(1)->name == reloaded.animation_get(0)->name + " 2");
}
TEST_CASE("JSON dates are ISO 8601")
{
auto model = model_load(file_load(CORPUS_DIR / "01_minimal.anm2"));
for (auto [date, iso] : {std::pair{"01/01/2026 12:00:00 PM", "2026-01-01T12:00:00"},
{"03/04/2025 12:30:05 AM", "2025-03-04T00:30:05"},
{"12/31/2024 11:59:59 PM", "2024-12-31T23:59:59"}})
{
model.info.createdOn = date;
auto text = model::model_to_json(model);
CHECK(text.find(iso) != std::string::npos);
CHECK(model_load(text).info.createdOn == date);
}
}
TEST_CASE("JSON that can't be read is reported by where it is, and the rest still opens")
{
auto text = R"({ "anm2ed": 1, "colour": 1, "info": { "fps": "fast" },
"layers": [ { "name": "a", "sheet": 4 } ],
"animations": { "Idle": { "length": 3, "root": [ { "pos": [1], "interpolation": "bouncy", "delay": 3 } ] } } })";
model::Model model{};
std::string issues{};
REQUIRE(model::model_load_string(model, text, &issues));
CHECK(model.animation_get(0)->frameNum == 3);
CHECK(model.animation_get(0)->root.frames[0].duration == 3);
for (auto issue : {"colour: unknown key", "info.fps: expected a number", "layers[0].sheet: no item at index 4",
"animations.Idle.root[0].pos: expected an array of 2 numbers",
"animations.Idle.root[0].interpolation: unknown value \"bouncy\""})
CHECK_MESSAGE(issues.find(issue) != std::string::npos, issue);
CHECK_FALSE(model::model_load_string(model, "{ \"fps\": 30 }", &issues));
CHECK_FALSE(model::model_load_string(model, "{ \"anm2ed\": 1, ", &issues));
}