mirror of
https://github.com/NixOS/nix.git
synced 2025-11-20 17:29:36 +01:00
Minimize the use of C Macros for characterization tests
Fewer macros is better! Introduce a new `JsonChacterizationTest` mixin class to help with this. Also, avoid some needless copies with `GetParam`. Part of my effort shoring up the JSON formats with #13570.
This commit is contained in:
parent
3bf1268ac6
commit
01b2037bc0
15 changed files with 364 additions and 252 deletions
|
|
@ -0,0 +1,54 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "nix/util/types.hh"
|
||||
#include "nix/util/file-system.hh"
|
||||
|
||||
#include "nix/util/tests/characterization.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* Mixin class for writing characterization tests for `nlohmann::json`
|
||||
* conversions for a given type.
|
||||
*/
|
||||
template<typename T>
|
||||
struct JsonCharacterizationTest : virtual CharacterizationTest
|
||||
{
|
||||
/**
|
||||
* Golden test for reading
|
||||
*
|
||||
* @param test hook that takes the contents of the file and does the
|
||||
* actual work
|
||||
*/
|
||||
void readJsonTest(PathView testStem, const T & expected)
|
||||
{
|
||||
using namespace nlohmann;
|
||||
readTest(Path{testStem} + ".json", [&](const auto & encodedRaw) {
|
||||
auto encoded = json::parse(encodedRaw);
|
||||
T decoded = adl_serializer<T>::from_json(encoded);
|
||||
ASSERT_EQ(decoded, expected);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Golden test for writing
|
||||
*
|
||||
* @param test hook that produces contents of the file and does the
|
||||
* actual work
|
||||
*/
|
||||
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"); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace nix
|
||||
|
|
@ -7,6 +7,7 @@ headers = files(
|
|||
'gmock-matchers.hh',
|
||||
'gtest-with-params.hh',
|
||||
'hash.hh',
|
||||
'json-characterization.hh',
|
||||
'nix_api_util.hh',
|
||||
'string_callback.hh',
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue