Process each invalid example separately in picojson_format_test

This commit is contained in:
Tristan Penman 2023-08-24 20:52:18 +10:00
parent 0b7efb3763
commit 373576a3d2

View File

@ -31,7 +31,7 @@ constexpr auto validStr = R"JSON([
"2023-07-18T14:46:22Z" "2023-07-18T14:46:22Z"
])JSON"; ])JSON";
constexpr auto invalidStr = R"JSON([ constexpr auto invalidStrs = R"JSON([
["um 12", "um 12"], ["um 12", "um 12"],
["2023-07-18T14:46:22Z"], ["2023-07-18T14:46:22Z"],
["2023-07-18T14:46:22Z", "2023-07-18T14:46:22Z", "2023-07-18T14:46:22Z", "2023-07-18T14:46:22Z"] ["2023-07-18T14:46:22Z", "2023-07-18T14:46:22Z", "2023-07-18T14:46:22Z", "2023-07-18T14:46:22Z"]
@ -80,30 +80,33 @@ int main(int argc, char **argv)
{ {
// invalid // invalid
auto targetJson = Parse(invalidStr, picojson::value{}); auto targetJsonArray = Parse(invalidStrs, picojson::value{});
auto targetAdapter = valijson::adapters::PicoJsonAdapter(targetJson); std::cout << "Invalid Targets:" << std::endl << invalidStrs << std::endl << std::endl;
std::cout << "Invalid Target:" << std::endl << invalidStr << std::endl << std::endl;
valijson::ValidationResults results; for (auto &&testCase : targetJsonArray.get<picojson::array>()) {
auto validator = valijson::Validator(); auto targetAdapter = valijson::adapters::PicoJsonAdapter(testCase);
auto isValid = validator.validate(
*validatorSchema,
targetAdapter,
&results);
std::cout << "Is valid: " << (isValid ? "YES" : "NO") << std::endl << std::endl; valijson::ValidationResults results;
auto validator = valijson::Validator();
auto isValid = validator.validate(
*validatorSchema,
targetAdapter,
&results);
valijson::ValidationResults::Error error; std::cout << "Is valid: " << (isValid ? "YES" : "NO") << std::endl << std::endl;
unsigned int errorNum = 1;
while (results.popError(error)) { valijson::ValidationResults::Error error;
std::cerr << "Error #" << errorNum << std::endl; unsigned int errorNum = 1;
std::cerr << " "; while (results.popError(error)) {
for (const std::string &contextElement : error.context) { std::cerr << "Error #" << errorNum << std::endl;
std::cerr << contextElement << " "; std::cerr << " ";
for (const std::string &contextElement : error.context) {
std::cerr << contextElement << " ";
}
std::cerr << std::endl;
std::cerr << " - " << error.description << std::endl << std::endl;
++errorNum;
} }
std::cerr << std::endl;
std::cerr << " - " << error.description << std::endl;
++errorNum;
} }
} }
} }