Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
Loading...
Searching...
No Matches
json-characterization.hh
Go to the documentation of this file.
1
#pragma once
3
4
#include <gtest/gtest.h>
5
#include <nlohmann/json.hpp>
6
7
#include "
nix/util/types.hh
"
8
#include "
nix/util/ref.hh
"
9
#include "
nix/util/file-system.hh
"
10
#include "
nix/util/nar-accessor.hh
"
11
12
#include "
nix/util/tests/characterization.hh
"
13
14
namespace
nix {
15
19
template
<
typename
T>
20
void
readJsonTest
(
CharacterizationTest
& test, std::string_view testStem,
const
T & expected,
auto
... args)
21
{
22
using namespace
nlohmann;
23
test.
readTest
(std::string{testStem} +
".json"
, [&](
const
auto
& encodedRaw) {
24
auto
encoded = json::parse(encodedRaw);
25
T decoded = adl_serializer<T>::from_json(encoded, args...);
26
ASSERT_EQ(decoded, expected);
27
});
28
}
29
33
template
<
typename
T>
34
void
writeJsonTest
(
CharacterizationTest
& test, std::string_view testStem,
const
T & value)
35
{
36
using namespace
nlohmann;
37
test.
writeTest
(
38
std::string{testStem} +
".json"
,
39
[&]() -> json {
return
static_cast<
json
>
(value); },
40
[](
const
auto
& file) {
return
json::parse(readFile(file)); },
41
[](
const
auto
& file,
const
auto
& got) {
return
writeFile(file, got.dump(2) +
"\n"
); });
42
}
43
52
template
<
typename
T>
53
void
writeJsonTest
(
CharacterizationTest
& test, std::string_view testStem,
const
ref<T>
& value)
54
{
55
using namespace
nlohmann;
56
test.
writeTest
(
57
std::string{testStem} +
".json"
,
58
[&]() -> json {
return
static_cast<
json
>
(*value); },
59
[](
const
auto
& file) {
return
json::parse(readFile(file)); },
60
[](
const
auto
& file,
const
auto
& got) {
return
writeFile(file, got.dump(2) +
"\n"
); });
61
}
62
66
template
<
typename
T>
67
void
checkpointJson
(
CharacterizationTest
& test, std::string_view testStem,
const
T & got)
68
{
69
using namespace
nlohmann;
70
71
auto
file = test.
goldenMaster
(std::string{testStem} +
".json"
);
72
73
json gotJson =
static_cast<
json
>
(got);
74
75
if
(testAccept()) {
76
createDirs(file.parent_path());
77
writeFile(file, gotJson.dump(2) +
"\n"
);
78
ADD_FAILURE() <<
"Updating golden master "
<< file;
79
}
else
{
80
json expectedJson = json::parse(readFile(file));
81
ASSERT_EQ(gotJson, expectedJson);
82
T expected = adl_serializer<T>::from_json(expectedJson);
83
ASSERT_EQ(got, expected);
84
}
85
}
86
91
template
<
typename
T>
92
void
checkpointJson
(
CharacterizationTest
& test, std::string_view testStem,
const
ref<T>
& got)
93
{
94
using namespace
nlohmann;
95
96
auto
file = test.
goldenMaster
(std::string{testStem} +
".json"
);
97
98
json gotJson =
static_cast<
json
>
(*got);
99
100
if
(testAccept()) {
101
createDirs(file.parent_path());
102
writeFile(file, gotJson.dump(2) +
"\n"
);
103
ADD_FAILURE() <<
"Updating golden master "
<< file;
104
}
else
{
105
json expectedJson = json::parse(readFile(file));
106
ASSERT_EQ(gotJson, expectedJson);
107
ref<T>
expected = adl_serializer<ref<T>>::from_json(expectedJson);
108
ASSERT_EQ(*got, *expected);
109
}
110
}
111
116
template
<
typename
T>
117
struct
JsonCharacterizationTest
:
virtual
CharacterizationTest
118
{
122
void
readJsonTest
(std::string_view testStem,
const
T & expected,
auto
... args)
123
{
124
nix::readJsonTest
(*
this
, testStem, expected, args...);
125
}
126
130
void
writeJsonTest
(std::string_view testStem,
const
T & value)
131
{
132
nix::writeJsonTest
(*
this
, testStem, value);
133
}
134
135
void
checkpointJson
(std::string_view testStem,
const
T & value)
136
{
137
nix::checkpointJson
(*
this
, testStem, value);
138
}
139
};
140
141
}
// namespace nix
characterization.hh
nix::ref
Definition
ref.hh:58
file-system.hh
Utilities for working with the file system and file paths.
nix::readJsonTest
void readJsonTest(CharacterizationTest &test, std::string_view testStem, const T &expected, auto... args)
Definition
json-characterization.hh:20
nix::writeJsonTest
void writeJsonTest(CharacterizationTest &test, std::string_view testStem, const T &value)
Definition
json-characterization.hh:34
nix::checkpointJson
void checkpointJson(CharacterizationTest &test, std::string_view testStem, const T &got)
Definition
json-characterization.hh:67
nar-accessor.hh
ref.hh
nix::CharacterizationTest
Definition
characterization.hh:26
nix::CharacterizationTest::readTest
void readTest(std::string_view testStem, auto &&test)
Definition
characterization.hh:39
nix::CharacterizationTest::writeTest
void writeTest(std::string_view testStem, auto &&test, auto &&readFile2, auto &&writeFile2)
Definition
characterization.hh:56
nix::CharacterizationTest::goldenMaster
virtual std::filesystem::path goldenMaster(std::string_view testStem) const =0
nix::JsonCharacterizationTest
Definition
json-characterization.hh:118
nix::JsonCharacterizationTest::writeJsonTest
void writeJsonTest(std::string_view testStem, const T &value)
Definition
json-characterization.hh:130
nix::JsonCharacterizationTest::readJsonTest
void readJsonTest(std::string_view testStem, const T &expected, auto... args)
Definition
json-characterization.hh:122
types.hh
libutil-test-support
include
nix
util
tests
json-characterization.hh
Generated by
1.18.0