From a41f35ce84b4168d22dccfa51c260006367a10fb Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Fri, 27 Oct 2023 12:46:17 +0200 Subject: [PATCH] adapted test. --- include/rfl/Validator.hpp | 37 +++++++++++++++------------ tests/test_constr.hpp | 54 +++++++++++++++++++++++++++------------ 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/include/rfl/Validator.hpp b/include/rfl/Validator.hpp index cf1df9e4..0a656b0e 100644 --- a/include/rfl/Validator.hpp +++ b/include/rfl/Validator.hpp @@ -30,20 +30,31 @@ struct Annotate { using ReflectionType = _Type; ReflectionType value; - // template - // Annotate(T _value) : value(static_cast<_Type>(_value)) { - // std::optional validate_check = Class::validate(value); - // if (!validate_check.has_value()) { - // throw std::runtime_error(validate_check.value().what()); - // } - // }; - // - Annotate(ReflectionType _value) : value(_value) { + template , bool> = true > + Annotate(const T& _value) : value(_value) { std::optional validate_check = Class::validate(value); - if (!validate_check.has_value()) { + if (validate_check.has_value()) { + throw std::runtime_error(validate_check.value().what()); + } + }; + + template , bool> = true > + Annotate(T&& _value) : value(std::forward(_value)) { + std::optional validate_check = Class::validate(value); + if (validate_check.has_value()) { throw std::runtime_error(validate_check.value().what()); } } + + Annotate(ReflectionType _value) : value(_value) { + std::optional validate_check = Class::validate(value); + if (validate_check.has_value()) { + throw std::runtime_error(validate_check.value().what()); + } + } + }; template @@ -102,12 +113,6 @@ struct Pattern { } }; -Annotate> a("hello"); -Annotate> b("hello"); -Annotate> c(2); -Annotate> d(2); -Annotate, Length<1,2>> e({1,2}); - } // namespace rfl // #endif diff --git a/tests/test_constr.hpp b/tests/test_constr.hpp index 81a04077..0fd41da4 100644 --- a/tests/test_constr.hpp +++ b/tests/test_constr.hpp @@ -1,36 +1,58 @@ -#include "write_and_read.hpp" +#include #include -void test_constr() { +#include "write_and_read.hpp" +void test_constr() { std::cout << "test_constr" << std::endl; // Custom validation for string // Masked credit cards - using MaskedCreditCardType = rfl::ConStr; - // Nick name between 5 and 10 characters long without regex use - using NickNameType = rfl::ConStr<"", 5, 10>; + using MaskedCreditCardType = rfl::Annotate>; + //// Nick name between 5 and 10 characters long without regex use + using NickNameType = rfl::Annotate>; struct Person { rfl::Field<"firstName", std::string> first_name; rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"id", rfl::UUIDv4> id; - rfl::Field<"payload", rfl::Base64Encoded> payload; + //rfl::Field<"id", rfl::UUIDv4> id; + //rfl::Field<"payload", rfl::Base64Encoded> payload; rfl::Field<"maskedCreditCard", MaskedCreditCardType> masked_credit_card; rfl::Field<"nickName", NickNameType> nick_name; }; - const auto bart = Person{ + try { + const auto wrong_bart = Person{ + .first_name = "Bart", + //.id = "de76b905-5b32-4127-96d1-48e5321e11a6", + //.payload = "aGVsbG93b3JsZAo=", + .masked_credit_card = "XXXX-XXXX-XXXX-444A", + .nick_name = "Cosmo", + }; + } catch (const std::exception &e) { + std::cout << e.what() << std::endl; + //assert( + // e.what() == + // std::string( + // R"(String 'XXXX-XXXX-XXXX-444A' did not match format 'XXXX-XXXX-XXXX-\d{4}')")); + } + const auto correct_bart = Person{ .first_name = "Bart", - .id = "de76b905-5b32-4127-96d1-48e5321e11a6", - .payload = "aGVsbG93b3JsZAo=", + //.id = "de76b905-5b32-4127-96d1-48e5321e11a6", + //.payload = "aGVsbG93b3JsZAo=", .masked_credit_card = "XXXX-XXXX-XXXX-4440", .nick_name = "Cosmo", - }; - write_and_read(bart, "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," - "\"id\":\"de76b905-5b32-4127-96d1-48e5321e11a6\"," - "\"payload\":\"aGVsbG93b3JsZAo=\"," - "\"maskedCreditCard\":\"XXXX-XXXX-XXXX-4440\"," - "\"nickName\":\"Cosmo\"}"); + write_and_read(correct_bart, + "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," + "\"id\":\"de76b905-5b32-4127-96d1-48e5321e11a6\"," + "\"payload\":\"aGVsbG93b3JsZAo=\"," + "\"maskedCreditCard\":\"XXXX-XXXX-XXXX-4440\"," + "\"nickName\":\"Cosmo\"}"); + + //struct Blub { + // rfl::Field<"firstName", + // rfl::Validate>; + //}; + // }