Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions include/rfl/comparisons.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RFL_COMPARISONS_HPP_
#define RFL_COMPARISONS_HPP_

#include <sstream>
#include <type_traits>

#include "Result.hpp"
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
52 changes: 52 additions & 0 deletions tests/json/test_validation_with_fields.cpp
Original file line number Diff line number Diff line change
@@ -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
Loading