mirror of
https://github.com/NixOS/nix.git
synced 2025-11-21 17:59:36 +01:00
JSON alongside binary proto serialization test data
This makes the proto serializer characterisation test data be accompanied by JSON data. This is arguably useful for a reasons: - The JSON data is human-readable while the binary data is not, so it provides some indication of what the test data means beyond the C++ literals. - The JSON data is language-agnostic, and so can be used to quickly rig up tests for implementation in other languages, without having source code literals at all (just go back and forth between the JSON and the binary). - Even though we have no concrete plans to place the binary protocol 1-1 or with JSON, it is still nice to ensure that the JSON serializers and binary protocols have (near) equal coverage over data types, to help ensure we didn't forget a JSON (de)serializer.
This commit is contained in:
parent
f5390e76e4
commit
204749270b
60 changed files with 1097 additions and 42 deletions
|
|
@ -11,6 +11,34 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* Golden test for JSON reading
|
||||
*/
|
||||
template<typename T>
|
||||
void readJsonTest(CharacterizationTest & test, PathView testStem, const T & expected, auto... args)
|
||||
{
|
||||
using namespace nlohmann;
|
||||
test.readTest(Path{testStem} + ".json", [&](const auto & encodedRaw) {
|
||||
auto encoded = json::parse(encodedRaw);
|
||||
T decoded = adl_serializer<T>::from_json(encoded, args...);
|
||||
ASSERT_EQ(decoded, expected);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Golden test for JSON writing
|
||||
*/
|
||||
template<typename T>
|
||||
void writeJsonTest(CharacterizationTest & test, PathView testStem, const T & value)
|
||||
{
|
||||
using namespace nlohmann;
|
||||
test.writeTest(
|
||||
Path{testStem} + ".json",
|
||||
[&]() -> json { return static_cast<json>(value); },
|
||||
[](const auto & file) { return json::parse(readFile(file)); },
|
||||
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); });
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin class for writing characterization tests for `nlohmann::json`
|
||||
* conversions for a given type.
|
||||
|
|
@ -26,12 +54,7 @@ struct JsonCharacterizationTest : virtual CharacterizationTest
|
|||
*/
|
||||
void readJsonTest(PathView testStem, const T & expected, auto... args)
|
||||
{
|
||||
using namespace nlohmann;
|
||||
readTest(Path{testStem} + ".json", [&](const auto & encodedRaw) {
|
||||
auto encoded = json::parse(encodedRaw);
|
||||
T decoded = adl_serializer<T>::from_json(encoded, args...);
|
||||
ASSERT_EQ(decoded, expected);
|
||||
});
|
||||
nix::readJsonTest(*this, testStem, expected, args...);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -42,12 +65,7 @@ struct JsonCharacterizationTest : virtual CharacterizationTest
|
|||
*/
|
||||
void writeJsonTest(PathView testStem, const T & value)
|
||||
{
|
||||
using namespace nlohmann;
|
||||
writeTest(
|
||||
Path{testStem} + ".json",
|
||||
[&]() -> json { return static_cast<json>(value); },
|
||||
[](const auto & file) { return json::parse(readFile(file)); },
|
||||
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); });
|
||||
nix::writeJsonTest(*this, testStem, value);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue