1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-28 13:11:00 +01:00

Create infrastructure for "checkpoint" characterization tests

These do a read/write test in the middles of some computation. They are
an imperative way to test intermediate values rather than functionally
testing end outputs.
This commit is contained in:
John Ericson 2025-11-13 02:27:19 -05:00
parent 70b9fbd76c
commit 294acfd807
2 changed files with 30 additions and 3 deletions

View file

@ -31,16 +31,14 @@ static inline bool testAccept()
/** /**
* Mixin class for writing characterization tests * Mixin class for writing characterization tests
*/ */
class CharacterizationTest : public virtual ::testing::Test struct CharacterizationTest : virtual ::testing::Test
{ {
protected:
/** /**
* While the "golden master" for this characterization test is * While the "golden master" for this characterization test is
* located. It should not be shared with any other test. * located. It should not be shared with any other test.
*/ */
virtual std::filesystem::path goldenMaster(PathView testStem) const = 0; virtual std::filesystem::path goldenMaster(PathView testStem) const = 0;
public:
/** /**
* Golden test for reading * Golden test for reading
* *

View file

@ -39,6 +39,30 @@ void writeJsonTest(CharacterizationTest & test, PathView testStem, const T & val
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); [](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); });
} }
/**
* Golden test in the middle of something
*/
template<typename T>
void checkpointJson(CharacterizationTest & test, PathView testStem, const T & got)
{
using namespace nlohmann;
auto file = test.goldenMaster(Path{testStem} + ".json");
json gotJson = static_cast<json>(got);
if (testAccept()) {
std::filesystem::create_directories(file.parent_path());
writeFile(file, gotJson.dump(2) + "\n");
ADD_FAILURE() << "Updating golden master " << file;
} else {
json expectedJson = json::parse(readFile(file));
ASSERT_EQ(gotJson, expectedJson);
T expected = adl_serializer<T>::from_json(expectedJson);
ASSERT_EQ(got, expected);
}
}
/** /**
* Mixin class for writing characterization tests for `nlohmann::json` * Mixin class for writing characterization tests for `nlohmann::json`
* conversions for a given type. * conversions for a given type.
@ -67,6 +91,11 @@ struct JsonCharacterizationTest : virtual CharacterizationTest
{ {
nix::writeJsonTest(*this, testStem, value); nix::writeJsonTest(*this, testStem, value);
} }
void checkpointJson(PathView testStem, const T & value)
{
nix::checkpointJson(*this, testStem, value);
}
}; };
} // namespace nix } // namespace nix