json-schema-validator/test/string-format-check-test.cpp
Luke Kersting 8a7d1d3fde Adapt CMake project name to be coherent with nlohmann::json's naming
Now when json-schema-validator is installed CMake config files are installed in the lib/cmake/json-schema-validator directory.
The install json-schema-validatorTargets.cmake file properly imports the json-hpp and json-schema-validator libraries.
The install json-schema-validatorConfig.cmake file is used by CMake find_package function to include the json-schema-validatorTargets.cmake file and to set the variable JSON_SCHEMA_VALIDATOR_INCLUDE_DIRS to the install include directory.
To use find_package to find the json-schema-validator simply include.
A new test (test_cmake_install) has been added.

When NLohmann's JSON is install with CMake, it follows a certain
naming convention.

As we learned to do proper CMake-install thanks to @lkersting's work
this project now adapts to the way NLohmann is doing it. Namely:

- json-schema.hpp is now located (and installed)
  in a nlohmann/-subdirectory
- the CMake library and project's name is now
  nlohmann_json_schema_validator

Instead of doing non-standard acrobatics to find the json.hpp
now find_package is used in order to find NLohmann's package

Co-Authored-By: Patrick Boettcher <p@yai.se>
2019-12-05 11:12:23 +01:00

86 lines
2.7 KiB
C++

#include <iostream>
#include <nlohmann/json-schema.hpp>
/** @return number of failed tests */
size_t
testStringFormat(const std::string &format,
const std::vector<std::pair<std::string, bool>> &stringValidTests)
{
size_t numberOfErrors = 0;
for (auto stringValid = stringValidTests.begin(); stringValid != stringValidTests.end(); ++stringValid) {
std::cout << "[INFO] Testing " << format << ": " << stringValid->first << "\n";
try {
nlohmann::json_schema::default_string_format_check(format, stringValid->first);
if (!stringValid->second) {
++numberOfErrors;
std::cerr << "[ERROR] String with " << format << " format '" << stringValid->first
<< "' validated even though it should NOT!\n";
}
} catch (std::exception &exception) {
std::cout << "[INFO] Validation failed with: " << exception.what() << "\n";
if (stringValid->second) {
++numberOfErrors;
std::cerr << "[ERROR] String with " << format << " format '" << stringValid->first
<< "' did NOT validate even though it should!\n";
}
}
}
return numberOfErrors;
}
int main()
{
size_t numberOfErrors = 0;
const std::vector<std::pair<std::string, bool>> dateTimeChecks{
{"1985-04-12T23:20:50.52Z", true},
{"1996-12-19T16:39:57-08:00", true},
{"1990-12-31T23:59:60Z", true},
{"1990-12-31T15:59:60-08:00", true},
{"1937-01-01T12:00:27.87+00:20", true},
{"1985-4-12T23:20:50.52Z", false},
{"1985-04-12T23:20:50.52", false},
{"1985-04-12T24:00:00", false},
{"", false},
{"2019-04-30T11:11:11+01:00", true},
{"2019-04-31T11:11:11+01:00", false},
{"2019-02-28T11:11:11+01:00", true},
{"2019-02-29T11:11:11+01:00", false},
{"2020-02-29T11:11:11+01:00", true},
{"2020-02-30T11:11:11+01:00", false},
{"2020-02-29T23:59:59+01:00", true},
{"2020-02-29T23:59:60+01:00", true},
{"2020-02-29T23:60:59+01:00", false},
{"2019-09-30T11:11:11+01:00", true},
{"2019-09-31T11:11:11+01:00", false},
{"2019-09-30T11:11:11+23:59", true},
{"2019-09-30T11:11:11+24:00", false}};
numberOfErrors += testStringFormat("date-time", dateTimeChecks);
const std::vector<std::pair<std::string, bool>> ipv4Checks{
{"", false},
{"x99.99.99.99", false},
{"99.99.99.99x", false},
{"192.168.0.1", true},
{"127.0.0", false},
{"127.0.0.1", true},
{"127.0.0.0.1", false},
{"255.255.255.255", true},
{"255.255.255.256", false},
{"255.255.256.255", false},
{"255.256.255.255", false},
{"256.255.255.255", false},
{"256.256.256.256", false},
{"0x7f000001", false}};
numberOfErrors += testStringFormat("ipv4", ipv4Checks);
return numberOfErrors;
}