mirror of
https://github.com/tristanpenman/valijson.git
synced 2025-03-02 20:30:11 +01:00
Add tests for date/time formats
This commit is contained in:
parent
4e2dfd1390
commit
203ca0de13
@ -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
|
||||
|
22
tests/data/documents/date_time_format.json
Normal file
22
tests/data/documents/date_time_format.json
Normal file
@ -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"
|
||||
}
|
||||
]
|
@ -1 +1 @@
|
||||
{}
|
||||
{}
|
||||
|
7
tests/data/schemas/date_time_format.schema.json
Normal file
7
tests/data/schemas/date_time_format.schema.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"properties": {
|
||||
"timestamp": {
|
||||
"format": "date-time"
|
||||
}
|
||||
}
|
||||
}
|
69
tests/test_date_time_format.cpp
Normal file
69
tests/test_date_time_format.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <valijson/adapters/rapidjson_adapter.hpp>
|
||||
#include <valijson/utils/rapidjson_utils.hpp>
|
||||
#include <valijson/schema.hpp>
|
||||
#include <valijson/schema_parser.hpp>
|
||||
#include <valijson/validation_results.hpp>
|
||||
#include <valijson/validator.hpp>
|
||||
|
||||
#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) );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user