2016-04-06 15:31:06 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2016-04-07 01:55:13 +02:00
|
|
|
#include <valijson/adapters/rapidjson_adapter.hpp>
|
2016-04-06 15:31:06 +02:00
|
|
|
#include <valijson/schema.hpp>
|
|
|
|
#include <valijson/validation_results.hpp>
|
|
|
|
#include <valijson/validator.hpp>
|
|
|
|
|
|
|
|
namespace {
|
2016-04-07 01:55:13 +02:00
|
|
|
|
|
|
|
using valijson::adapters::Adapter;
|
2016-04-06 15:31:06 +02:00
|
|
|
using valijson::Schema;
|
|
|
|
using valijson::Validator;
|
|
|
|
using valijson::ValidationResults;
|
2016-04-07 01:55:13 +02:00
|
|
|
using valijson::adapters::RapidJsonAdapter;
|
2016-04-06 15:31:06 +02:00
|
|
|
|
2016-04-07 01:55:13 +02:00
|
|
|
class StubPolyConstraint : public valijson::constraints::PolyConstraint
|
2016-04-06 15:31:06 +02:00
|
|
|
{
|
2020-07-06 03:47:58 +02:00
|
|
|
bool m_shouldValidate;
|
2016-04-06 15:31:06 +02:00
|
|
|
|
2016-04-07 01:55:13 +02:00
|
|
|
public:
|
2020-07-06 03:47:58 +02:00
|
|
|
explicit StubPolyConstraint(bool shouldValidate)
|
|
|
|
: m_shouldValidate(shouldValidate) { }
|
2016-04-06 15:31:06 +02:00
|
|
|
|
2020-07-06 03:47:58 +02:00
|
|
|
Constraint * cloneInto(void *ptr) const override
|
2016-04-07 01:55:13 +02:00
|
|
|
{
|
2020-07-06 03:47:58 +02:00
|
|
|
return new (ptr) StubPolyConstraint(m_shouldValidate);
|
2016-04-07 01:55:13 +02:00
|
|
|
}
|
2016-04-06 15:31:06 +02:00
|
|
|
|
2020-07-06 03:47:58 +02:00
|
|
|
size_t sizeOf() const override
|
2016-04-07 01:55:13 +02:00
|
|
|
{
|
|
|
|
return sizeof(StubPolyConstraint);
|
2016-04-06 15:31:06 +02:00
|
|
|
}
|
2016-04-07 01:55:13 +02:00
|
|
|
|
2020-07-06 03:47:58 +02:00
|
|
|
bool validate(
|
2021-05-17 05:57:29 +02:00
|
|
|
const Adapter &,
|
2016-04-07 01:55:13 +02:00
|
|
|
const std::vector<std::string> &context,
|
2020-07-06 03:47:58 +02:00
|
|
|
ValidationResults *results) const override
|
2016-04-06 15:31:06 +02:00
|
|
|
{
|
2020-07-06 03:47:58 +02:00
|
|
|
if (m_shouldValidate) {
|
2016-04-07 01:55:13 +02:00
|
|
|
return true;
|
2016-04-06 15:31:06 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 01:55:13 +02:00
|
|
|
if (results) {
|
|
|
|
results->pushError(context,
|
|
|
|
"StubPolyConstraint intentionally failed validation");
|
2016-04-06 15:31:06 +02:00
|
|
|
}
|
2016-04-07 01:55:13 +02:00
|
|
|
|
|
|
|
return false;
|
2016-04-06 15:31:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-07 01:55:13 +02:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
class TestPolyConstraint : public testing::Test
|
|
|
|
{
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TestPolyConstraint, ValidationCanPass)
|
|
|
|
{
|
|
|
|
StubPolyConstraint stubPolyConstraint(true);
|
|
|
|
|
|
|
|
Schema schema;
|
|
|
|
schema.addConstraintToSubschema(stubPolyConstraint, schema.root());
|
|
|
|
|
|
|
|
rapidjson::Document document;
|
|
|
|
RapidJsonAdapter adapter(document);
|
2016-04-06 15:31:06 +02:00
|
|
|
|
2016-04-07 01:55:13 +02:00
|
|
|
ValidationResults results;
|
|
|
|
|
|
|
|
Validator validator;
|
|
|
|
EXPECT_TRUE(validator.validate(schema, adapter, &results));
|
|
|
|
EXPECT_EQ(size_t(0), results.numErrors());
|
|
|
|
}
|
2016-04-06 15:31:06 +02:00
|
|
|
|
2016-04-07 01:55:13 +02:00
|
|
|
TEST_F(TestPolyConstraint, ValidationCanFail)
|
|
|
|
{
|
|
|
|
StubPolyConstraint stubPolyConstraint(false);
|
2016-04-06 15:31:06 +02:00
|
|
|
|
|
|
|
Schema schema;
|
2016-04-07 01:55:13 +02:00
|
|
|
schema.addConstraintToSubschema(stubPolyConstraint, schema.root());
|
2016-04-06 15:31:06 +02:00
|
|
|
|
2016-04-07 01:55:13 +02:00
|
|
|
rapidjson::Document document;
|
|
|
|
RapidJsonAdapter adapter(document);
|
2016-04-06 15:31:06 +02:00
|
|
|
|
|
|
|
ValidationResults results;
|
2016-04-07 01:55:13 +02:00
|
|
|
|
2016-04-06 15:31:06 +02:00
|
|
|
Validator validator;
|
2016-04-07 01:55:13 +02:00
|
|
|
EXPECT_FALSE(validator.validate(schema, adapter, &results));
|
|
|
|
EXPECT_EQ(size_t(1), results.numErrors());
|
|
|
|
|
|
|
|
ValidationResults::Error error;
|
|
|
|
EXPECT_TRUE(results.popError(error));
|
2020-07-06 03:47:58 +02:00
|
|
|
EXPECT_STREQ("StubPolyConstraint intentionally failed validation", error.description.c_str());
|
2016-04-06 15:31:06 +02:00
|
|
|
}
|