diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a1bd41..b9380a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,6 +113,7 @@ if(valijson_BUILD_TESTS) set(TEST_SOURCES tests/test_adapter_comparison.cpp + tests/test_date_time_format.cpp tests/test_fetch_absolute_uri_document_callback.cpp tests/test_fetch_urn_document_callback.cpp tests/test_json_pointer.cpp diff --git a/tests/data/documents/date_time_format.json b/tests/data/documents/date_time_format.json new file mode 100644 index 0000000..44248e4 --- /dev/null +++ b/tests/data/documents/date_time_format.json @@ -0,0 +1,22 @@ +[ + { + "timestamp": "AAA", + "validity": "invalid" + }, + { + "timestamp": "2000", + "validity": "invalid" + }, + { + "timestamp": "2000-01-01T00:00:00", + "validity": "permissive" + }, + { + "timestamp": "2000-01-01T00:00:00Z", + "validity": "strict" + }, + { + "timestamp": "2000-01-01T00:00:00+02:00", + "validity": "strict" + } +] diff --git a/tests/data/documents/object_empty.json b/tests/data/documents/object_empty.json index 9e26dfe..0967ef4 100644 --- a/tests/data/documents/object_empty.json +++ b/tests/data/documents/object_empty.json @@ -1 +1 @@ -{} \ No newline at end of file +{} diff --git a/tests/data/schemas/date_time_format.schema.json b/tests/data/schemas/date_time_format.schema.json new file mode 100644 index 0000000..f332c86 --- /dev/null +++ b/tests/data/schemas/date_time_format.schema.json @@ -0,0 +1,7 @@ +{ + "properties": { + "timestamp": { + "format": "date-time" + } + } +} diff --git a/tests/test_date_time_format.cpp b/tests/test_date_time_format.cpp new file mode 100644 index 0000000..eeda1cc --- /dev/null +++ b/tests/test_date_time_format.cpp @@ -0,0 +1,69 @@ +#include + +#include + +#include +#include +#include +#include +#include +#include + +#define TEST_DATA_DIR "../tests/data" + +using std::string; + +using valijson::adapters::AdapterTraits; +using valijson::adapters::RapidJsonAdapter; +using valijson::utils::loadDocument; +using valijson::Schema; +using valijson::SchemaParser; +using valijson::Validator; +using valijson::ValidationResults; + +class TestDateTimeFormat : public ::testing::Test +{ + +}; + +TEST_F(TestDateTimeFormat, StrictAndPermissiveDateTimes) +{ + // Load schema document + rapidjson::Document schemaDocument; + ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/schemas/date_time_format.schema.json", schemaDocument) ); + RapidJsonAdapter schemaAdapter(schemaDocument); + + // Parse schema document + Schema schema; + SchemaParser schemaParser; +#if VALIJSON_USE_EXCEPTIONS + ASSERT_NO_THROW(schemaParser.populateSchema(schemaAdapter, schema)); +#else + schemaParser.populateSchema(schemaAdapter, schema); +#endif + + // Load test document + rapidjson::Document testDocument; + ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/documents/date_time_format.json", testDocument) ); + RapidJsonAdapter testAdapter(testDocument); + + // Setup validators + Validator strictValidator(Validator::kStrongTypes, Validator::kStrictDateTime); + Validator permissiveValidator(Validator::kStrongTypes, Validator::kPermissiveDateTime); + + const RapidJsonAdapter::Array examples = testAdapter.asArray(); + for (auto &&example : examples) { + + auto validity = example.asObject().find("validity")->second.asString(); + if (validity == "strict") { + EXPECT_TRUE( strictValidator.validate(schema, example, NULL) ); + EXPECT_TRUE( permissiveValidator.validate(schema, example, NULL) ); + } else if (validity == "permissive") { + EXPECT_FALSE( strictValidator.validate(schema, example, NULL) ); + EXPECT_TRUE( permissiveValidator.validate(schema, example, NULL) ); + } else { + EXPECT_FALSE( strictValidator.validate(schema, example, NULL) ); + EXPECT_FALSE( permissiveValidator.validate(schema, example, NULL) ); + } + } +}