Quellcode durchsuchen

Add auto message generation for SectionPairIdentifierArgumentEvaluator

Patrick-Christopher Mattulat vor 2 Jahren
Ursprung
Commit
46a9a452d4

+ 2 - 4
include/ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-09
-* Changed:         2023-02-17
+* Changed:         2023-02-19
 *
 * */
 
@@ -14,7 +14,6 @@
 #include <ls-std/core/interface/IEvaluator.hpp>
 #include <ls-std/io/section-pair/SectionPairTypes.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
-#include <string>
 
 namespace ls::std::io
 {
@@ -22,7 +21,7 @@ namespace ls::std::io
   {
     public:
 
-      explicit SectionPairIdentifierArgumentEvaluator(ls::std::io::section_pair_identifier _identifier, ::std::string _message);
+      explicit SectionPairIdentifierArgumentEvaluator(ls::std::io::section_pair_identifier _identifier);
       ~SectionPairIdentifierArgumentEvaluator() override;
 
       void evaluate() override;
@@ -30,7 +29,6 @@ namespace ls::std::io
     private:
 
       ls::std::io::section_pair_identifier identifier{};
-      ::std::string message{};
   };
 }
 

+ 5 - 3
source/ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.cpp

@@ -3,15 +3,16 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-09
-* Changed:         2023-02-17
+* Changed:         2023-02-19
 *
 * */
 
 #include <ls-std/core/exception/IllegalArgumentException.hpp>
 #include <ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.hpp>
 #include <ls-std/io/section-pair/validator/SectionPairIdentifierValidator.hpp>
+#include <string>
 
-ls::std::io::SectionPairIdentifierArgumentEvaluator::SectionPairIdentifierArgumentEvaluator(ls::std::io::section_pair_identifier _identifier, ::std::string _message) : ls::std::core::Class("SectionPairIdentifierArgumentEvaluator"), identifier(::std::move(_identifier)), message(::std::move(_message))
+ls::std::io::SectionPairIdentifierArgumentEvaluator::SectionPairIdentifierArgumentEvaluator(ls::std::io::section_pair_identifier _identifier) : ls::std::core::Class("SectionPairIdentifierArgumentEvaluator"), identifier(::std::move(_identifier))
 {}
 
 ls::std::io::SectionPairIdentifierArgumentEvaluator::~SectionPairIdentifierArgumentEvaluator() = default;
@@ -20,6 +21,7 @@ void ls::std::io::SectionPairIdentifierArgumentEvaluator::evaluate()
 {
   if (!ls::std::io::SectionPairIdentifierValidator{this->identifier}.isValid())
   {
-    throw ls::std::core::IllegalArgumentException{this->message};
+    ::std::string message = "\"" + this->identifier + "\" is not a valid identifier!";
+    throw ls::std::core::IllegalArgumentException{message};
   }
 }

+ 2 - 2
source/ls-std/io/section-pair/model/SectionPairRow.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-08
-* Changed:         2023-02-17
+* Changed:         2023-02-19
 *
 * */
 
@@ -103,6 +103,6 @@ void ls::std::io::SectionPairRow::_initValue(const ls::std::io::SectionPairRowEn
 void ls::std::io::SectionPairRow::_setKey(const ls::std::io::section_pair_identifier &_key)
 {
   ls::std::core::EmptyStringArgumentEvaluator{_key, this->getClassName() + ": passed key identifier is empty!"}.evaluate();
-  ls::std::io::SectionPairIdentifierArgumentEvaluator(_key, this->getClassName() + ": section pair key identifier \"" + _key + "\" contains invalid characters!").evaluate();
+  ls::std::io::SectionPairIdentifierArgumentEvaluator(_key).evaluate();
   this->key = _key;
 }

+ 2 - 2
source/ls-std/io/section-pair/model/SectionPairSection.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-13
-* Changed:         2023-02-18
+* Changed:         2023-02-19
 *
 * */
 
@@ -132,6 +132,6 @@ void ls::std::io::SectionPairSection::_rowExistenceCheck(const ls::std::io::sect
 void ls::std::io::SectionPairSection::_setSectionId(const ls::std::io::section_pair_identifier &_sectionId)
 {
   ls::std::core::EmptyStringArgumentEvaluator{_sectionId}.evaluate();
-  ls::std::io::SectionPairIdentifierArgumentEvaluator(_sectionId, this->getClassName() + ": argument \"_sectionId\" contains invalid characters!").evaluate();
+  ls::std::io::SectionPairIdentifierArgumentEvaluator(_sectionId).evaluate();
   this->sectionId = _sectionId;
 }

+ 18 - 6
test/cases/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluatorTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-09
-* Changed:         2023-02-17
+* Changed:         2023-02-19
 *
 * */
 
@@ -32,27 +32,39 @@ namespace
       {}
   };
 
+  class SectionPairIdentifierArgumentEvaluatorNotValidTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairIdentifierArgumentEvaluatorNotValidTest() = default;
+      ~SectionPairIdentifierArgumentEvaluatorNotValidTest() override = default;
+  };
+
   TEST_F(SectionPairIdentifierArgumentEvaluatorTest, getClassName)
   {
-    shared_ptr<SectionPairIdentifierArgumentEvaluator> evaluator = make_shared<SectionPairIdentifierArgumentEvaluator>("_id", "not valid!");
+    shared_ptr<SectionPairIdentifierArgumentEvaluator> evaluator = make_shared<SectionPairIdentifierArgumentEvaluator>("_id");
     ASSERT_STREQ("SectionPairIdentifierArgumentEvaluator", evaluator->getClassName().c_str());
   }
 
-  TEST_F(SectionPairIdentifierArgumentEvaluatorTest, evaluate)
+  TEST_P(SectionPairIdentifierArgumentEvaluatorNotValidTest, evaluate)
   {
     EXPECT_THROW(
         {
           try
           {
-            SectionPairIdentifierArgumentEvaluator("_color", "section pair identifier contains invalid characters!").evaluate();
+            SectionPairIdentifierArgumentEvaluator(GetParam()).evaluate();
           }
           catch (const IllegalArgumentException &_exception)
           {
-            ::std::string message = _exception.what();
-            ASSERT_STREQ("IllegalArgumentException thrown - section pair identifier contains invalid characters!", message.c_str());
+            ::std::string actual = _exception.what();
+            ::std::string expected = "IllegalArgumentException thrown - \"" + GetParam() + "\" is not a valid identifier!";
+
+            ASSERT_STREQ(expected.c_str(), actual.c_str());
             throw;
           }
         },
         IllegalArgumentException);
   }
+
+  INSTANTIATE_TEST_SUITE_P(SectionPairIdentifierArgumentEvaluatorTest, SectionPairIdentifierArgumentEvaluatorNotValidTest, ::testing::Values("_color", "Color", "another_color"));
 }