#include #include #include "lix/libutil/strings.hh" #include "tests/libstore.hh" #include "tests/characterization.hh" namespace nix { template class ProtoTest : public LibStoreTest { protected: Path unitTestData = getUnitTestData() + "/libstore/" + protocolDir; Path goldenMaster(std::string_view testStem) { return unitTestData + "/" + testStem + ".bin"; } }; template class VersionedProtoTest : public ProtoTest { public: /** * Golden test for `T` reading */ template void readTest(PathView testStem, typename Proto::Version version, T value) { if (testAccept()) { GTEST_SKIP() << cannotReadGoldenMaster; } else { auto expected = readFile(ProtoTest::goldenMaster(testStem)); T got = ({ StringSource from { expected }; Proto::template Serialise::read( *LibStoreTest::store, typename Proto::ReadConn {from, version} ); }); ASSERT_EQ(got, value); } } /** * Golden test for `T` write */ template void writeTest(PathView testStem, typename Proto::Version version, const T & value) { auto file = ProtoTest::goldenMaster(testStem); StringSink to; to << Proto::write( *LibStoreTest::store, typename Proto::WriteConn {version}, value); if (testAccept()) { createDirs(dirOf(file)); writeFile(file, to.s); GTEST_SKIP() << updatingGoldenMaster; } else { auto expected = readFile(file); ASSERT_EQ(to.s, expected); } } }; #define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \ TEST_F(FIXTURE, NAME ## _read) { \ readTest(STEM, VERSION, VALUE); \ } \ TEST_F(FIXTURE, NAME ## _write) { \ writeTest(STEM, VERSION, VALUE); \ } }