mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 22:36:19 +08:00
This commit is contained in:
+25
-18
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_COMPARISONS_HPP_
|
||||
#define RFL_COMPARISONS_HPP_
|
||||
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
|
||||
#include "Result.hpp"
|
||||
@@ -14,9 +15,10 @@ struct EqualTo {
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value != threshold) {
|
||||
return error("Value expected to be equal to " +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
std::stringstream stream;
|
||||
stream << "Value expected to be equal to " << threshold << ", but got "
|
||||
<< _value << ".";
|
||||
return error(stream.str());
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
@@ -38,9 +40,10 @@ struct Minimum {
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value < threshold) {
|
||||
return error("Value expected to be greater than or equal to " +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
std::stringstream stream;
|
||||
stream << "Value expected to be greater than or equal to " << threshold
|
||||
<< ", but got " << _value << ".";
|
||||
return error(stream.str());
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
@@ -62,9 +65,10 @@ struct ExclusiveMinimum {
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value <= threshold) {
|
||||
return error("Value expected to be greater than " +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
std::stringstream stream;
|
||||
stream << "Value expected to be greater than " << threshold
|
||||
<< ", but got " << _value << ".";
|
||||
return error(stream.str());
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
@@ -86,9 +90,10 @@ struct Maximum {
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value > threshold) {
|
||||
return error("Value expected to be less than or equal to " +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
std::stringstream stream;
|
||||
stream << "Value expected to be less than or equal to " << threshold
|
||||
<< ", but got " << _value << ".";
|
||||
return error(stream.str());
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
@@ -110,9 +115,10 @@ struct ExclusiveMaximum {
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value >= threshold) {
|
||||
return error("Value expected to be less than " +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
std::stringstream stream;
|
||||
stream << "Value expected to be less than " << threshold << ", but got "
|
||||
<< _value << ".";
|
||||
return error(stream.str());
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
@@ -134,9 +140,10 @@ struct NotEqualTo {
|
||||
static Result<T> validate(T _value) noexcept {
|
||||
constexpr auto threshold = static_cast<T>(_threshold);
|
||||
if (_value == threshold) {
|
||||
return error("Value expected to not be equal to " +
|
||||
std::to_string(threshold) + ", but got " +
|
||||
std::to_string(_value) + ".");
|
||||
std::stringstream stream;
|
||||
stream << "Value expected not to be equal to " << threshold
|
||||
<< ", but got " << _value << ".";
|
||||
return error(stream.str());
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <cassert>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_validation_with_fields {
|
||||
|
||||
using SimpleNameValidator = rfl::Validator<
|
||||
std::string,
|
||||
rfl::AllOf<rfl::PatternValidator<R"(^[A-Za-z1-9_]+$)", "TagName">>,
|
||||
rfl::Size<rfl::Minimum<3>>, rfl::Size<rfl::Maximum<50>>>;
|
||||
|
||||
struct TagConfig {
|
||||
SimpleNameValidator Name;
|
||||
int32_t Length;
|
||||
bool WriteEnabled;
|
||||
};
|
||||
|
||||
struct ThreadConfig {
|
||||
rfl::Field<"type", SimpleNameValidator> Type;
|
||||
rfl::Field<"Name", SimpleNameValidator> Name;
|
||||
rfl::Field<"ReadTags", std::vector<TagConfig>> ReadTags;
|
||||
rfl::Field<"WriteTags", std::vector<TagConfig>> WriteTags;
|
||||
rfl::Field<"ThreadPriority", int32_t> ThreadPriority;
|
||||
rfl::Field<"DecimationFactor", int32_t> DecimationFactor;
|
||||
rfl::Field<"TimingOffset", int32_t> TimingOffset;
|
||||
};
|
||||
|
||||
TEST(json, test_validation_with_fields) {
|
||||
const auto read_tag1 =
|
||||
TagConfig{.Name = "READ_TAG_1", .Length = 5, .WriteEnabled = false};
|
||||
|
||||
const auto write_tag1 =
|
||||
TagConfig{.Name = "WRITE_TAG_1", .Length = 10, .WriteEnabled = true};
|
||||
|
||||
const auto test_thread_config =
|
||||
ThreadConfig{.Type = "BasicThread",
|
||||
.Name = "TEST_1",
|
||||
.ReadTags = std::vector<TagConfig>({read_tag1}),
|
||||
.WriteTags = std::vector<TagConfig>({write_tag1}),
|
||||
.ThreadPriority = -1,
|
||||
.DecimationFactor = 10,
|
||||
.TimingOffset = 0};
|
||||
|
||||
write_and_read(
|
||||
test_thread_config,
|
||||
R"({"type":"BasicThread","Name":"TEST_1","ReadTags":[{"Name":"READ_TAG_1","Length":5,"WriteEnabled":false}],"WriteTags":[{"Name":"WRITE_TAG_1","Length":10,"WriteEnabled":true}],"ThreadPriority":-1,"DecimationFactor":10,"TimingOffset":0})");
|
||||
}
|
||||
|
||||
} // namespace test_validation_with_fields
|
||||
Reference in New Issue
Block a user