This repository was archived by the owner on Sep 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathself-tests.cpp
More file actions
165 lines (118 loc) · 4.41 KB
/
Copy pathself-tests.cpp
File metadata and controls
165 lines (118 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "JsonGenerator.h"
#include <filesystem>
#include <iostream>
#include <format>
#include <backward.hpp>
#include <cxxopts.hpp>
#include <rapidcheck.h>
#include <nlohmann/json.hpp>
#include <nlohmann/json-schema.hpp>
// @author Dániel Lukács (https://github.com/daniel-lukacs), ELTE IK, 2025.
namespace nj = nlohmann;
class CmdArgs {
public:
CmdArgs(int argc, char* argv[]){
cxxopts::Options options("Tests the JSON configuration tester");
std::string curr_path = std::filesystem::current_path().string();
options.add_options()
("h,help", "Shows this help and exit.")
("testDir", "Custom path to folder containing test files listing JSON schemas",
cxxopts::value<std::string>()->default_value(""))
;
options.parse_positional({"testDir"});
auto result{options.parse(argc, argv)};
if(result.count("help") > 0){
std::cout << options.help() << std::endl;
exit(0);
}
this->testDir = result["testDir"].as<std::string>();
}
std::string testDir;
};
class ValidationAgainstSchemaFailed : public std::runtime_error {
public:
ValidationAgainstSchemaFailed(std::string message) throw()
: std::runtime_error(message) { }
};
bool validateJson(const nj::json & schema, const nj::json & result, jg::LogSet &){
nj::json_schema::json_validator validator;
try {
validator.set_root_schema(schema);
} catch (const std::exception &e) {
throw ValidationAgainstSchemaFailed(std::format("set_root_schema({}). Error: {}", nj::to_string(schema), e.what()));
return false;
}
try {
validator.validate(result);
} catch (const std::exception &e) {
throw ValidationAgainstSchemaFailed(std::format("validate({}). Schema: {}. Error: {}", nj::to_string(result), nj::to_string(schema), e.what()));
return false;
}
return true;
}
bool testSchema(std::string testName, const nj::json & schemas, const bool negativeTests=false){
int noItems = schemas.size();
int testIdx = 1;
for(const auto & schema : schemas){
auto cit = schema.find("$comment");
std::string desc = cit == schema.end() ? "unnamed" : *cit;
std::string msg = std::format("Testing schema '{}' ({}/{} in {}).", desc, testIdx, noItems, testName);
// std::cout << desc << "\n";
jg::LogSet log;
try{
bool b = jg::check(msg, schema,
[&schema, &log](const nj::json & doc){
bool b = validateJson(schema, doc, log);
return b;
}, log);
if(negativeTests && b){
std::cout << "Self-test failed. Reason: Expected test to fail, but it did not happen.\n";
return false;
} else if(!negativeTests && !b){
return false;
} // otherwise: keep going
} catch(const jg::GenerationFailed & e){
std::cout << log.str() << std::flush;
std::cout << "Generation failed. Reason: " << e.what() << "\n";
if(!negativeTests){
return false;
} // otherwise: keep going
}
testIdx += 1;
}
return true;
}
const std::string TEST_DIR = "tests/positive";
const std::string FAILING_TEST_DIR = "tests/negative";
// TODO rc does not generate empty by default. add elementOf([], myGen) for strings and arrays;
int main(int argc, char* argv[]){
backward::SignalHandling sh{};
const CmdArgs args(argc, argv);
std::string posTestDir = (args.testDir.empty() ? TEST_DIR : args.testDir);
std::list<std::pair<std::string, bool>> testDirs =
{ { posTestDir, false }
, { FAILING_TEST_DIR, true }
};
for(const auto & [testDir, isNegative] : testDirs){
std::set<std::string> sortedPaths;
for (const auto & entry : std::filesystem::directory_iterator(testDir)){
sortedPaths.insert(entry.path());
}
for (const auto & path : sortedPaths){
nj::json schemas;
try {
std::ifstream ifs(path);
schemas = nj::json::parse(ifs);
} catch(const std::exception &e){
throw jg::JsonParsingFailed(std::format("Failed to parse '{}'. {}", path, e.what()));
}
bool b = testSchema(path, schemas, isNegative);
if(!b){
std::cout << "Verdict: There were failed tests :(\n";
return 1;
}
}
}
std::cout << "\nVerdict: All tests passed :)\n";
return 0;
}