Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
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"
11
13
14namespace nix {
15
19template<typename T>
20void 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
33template<typename T>
34void 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
52template<typename T>
53void 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
66template<typename T>
67void 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
91template<typename T>
92void 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
116template<typename T>
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
Definition ref.hh:58
Utilities for working with the file system and file paths.
void readJsonTest(CharacterizationTest &test, std::string_view testStem, const T &expected, auto... args)
Definition json-characterization.hh:20
void writeJsonTest(CharacterizationTest &test, std::string_view testStem, const T &value)
Definition json-characterization.hh:34
void checkpointJson(CharacterizationTest &test, std::string_view testStem, const T &got)
Definition json-characterization.hh:67
Definition characterization.hh:26
void readTest(std::string_view testStem, auto &&test)
Definition characterization.hh:39
void writeTest(std::string_view testStem, auto &&test, auto &&readFile2, auto &&writeFile2)
Definition characterization.hh:56
virtual std::filesystem::path goldenMaster(std::string_view testStem) const =0
Definition json-characterization.hh:118
void writeJsonTest(std::string_view testStem, const T &value)
Definition json-characterization.hh:130
void readJsonTest(std::string_view testStem, const T &expected, auto... args)
Definition json-characterization.hh:122