Skip to content

Commit 36ee8e4

Browse files
committed
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.
1 parent 72dbd43 commit 36ee8e4

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/libutil-test-support/include/nix/util/tests/characterization.hh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@ static inline bool testAccept()
3131
/**
3232
* Mixin class for writing characterization tests
3333
*/
34-
class CharacterizationTest : public virtual ::testing::Test
34+
struct CharacterizationTest : virtual ::testing::Test
3535
{
36-
protected:
3736
/**
3837
* While the "golden master" for this characterization test is
3938
* located. It should not be shared with any other test.
4039
*/
4140
virtual std::filesystem::path goldenMaster(PathView testStem) const = 0;
4241

43-
public:
4442
/**
4543
* Golden test for reading
4644
*

src/libutil-test-support/include/nix/util/tests/json-characterization.hh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ void writeJsonTest(CharacterizationTest & test, PathView testStem, const T & val
3939
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); });
4040
}
4141

42+
/**
43+
* Golden test in the middle of something
44+
*/
45+
template<typename T>
46+
void checkpointJson(CharacterizationTest & test, PathView testStem, const T & got)
47+
{
48+
using namespace nlohmann;
49+
50+
auto file = test.goldenMaster(Path{testStem} + ".json");
51+
52+
json gotJson = static_cast<json>(got);
53+
54+
if (testAccept()) {
55+
std::filesystem::create_directories(file.parent_path());
56+
writeFile(file, gotJson.dump(2) + "\n");
57+
ADD_FAILURE() << "Updating golden master " << file;
58+
} else {
59+
json expectedJson = json::parse(readFile(file));
60+
ASSERT_EQ(gotJson, expectedJson);
61+
T expected = adl_serializer<T>::from_json(expectedJson);
62+
ASSERT_EQ(got, expected);
63+
}
64+
}
65+
4266
/**
4367
* Mixin class for writing characterization tests for `nlohmann::json`
4468
* conversions for a given type.
@@ -67,6 +91,11 @@ struct JsonCharacterizationTest : virtual CharacterizationTest
6791
{
6892
nix::writeJsonTest(*this, testStem, value);
6993
}
94+
95+
void checkpointJson(PathView testStem, const T & value)
96+
{
97+
nix::checkpointJson(*this, testStem, value);
98+
}
7099
};
71100

72101
} // namespace nix

0 commit comments

Comments
 (0)