Sfoglia il codice sorgente

Reduce complexity of SectionPairRowValueValidator

Patrick-Christopher Mattulat 2 anni fa
parent
commit
302ab0c7d6

+ 4 - 1
include/ls-std/io/section-pair/validator/SectionPairRowValueValidator.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-09
-* Changed:         2023-02-11
+* Changed:         2023-02-18
 *
 * */
 
@@ -25,11 +25,14 @@ namespace ls::std::io
       explicit SectionPairRowValueValidator(ls::std::io::section_pair_row_value _value);
       ~SectionPairRowValueValidator() override;
 
+      [[nodiscard]] static ::std::string getValidationRegex();
       [[nodiscard]] bool isValid() override;
 
     private:
 
       ls::std::io::section_pair_row_value value{};
+
+      [[nodiscard]] static ::std::string _getValidationRegex();
   };
 }
 

+ 16 - 6
source/ls-std/io/section-pair/validator/SectionPairRowValueValidator.cpp

@@ -16,13 +16,23 @@ ls::std::io::SectionPairRowValueValidator::SectionPairRowValueValidator(ls::std:
 
 ls::std::io::SectionPairRowValueValidator::~SectionPairRowValueValidator() = default;
 
+::std::string ls::std::io::SectionPairRowValueValidator::getValidationRegex()
+{
+  ::std::string validationRegex = ls::std::io::SectionPairRowValueValidator::_getValidationRegex();
+  return "(" + validationRegex + ")|(" + validationRegex + "\\n{1})|(" + validationRegex + "\\r{1}\\n{1})";
+}
+
 bool ls::std::io::SectionPairRowValueValidator::isValid()
 {
-  static ::std::regex windowsLineBreakRegex{ls::std::io::NewLine::getWindowsNewLine()};
-  static ::std::regex unixLineBreakRegex{ls::std::io::NewLine::getUnixNewLine()};
+  ::std::string validationRegex = ls::std::io::SectionPairRowValueValidator::_getValidationRegex();
+  ::std::string concatenation = "(^" + validationRegex + ")|(^" + validationRegex + R"(\n{1})|(^)" + validationRegex + R"(\r{1}\n{1}))";
+  static ::std::regex valueRegex{concatenation};
 
-  this->value = ::std::regex_replace(this->value, windowsLineBreakRegex, "");
-  this->value = ::std::regex_replace(this->value, unixLineBreakRegex, "");
-  size_t foundPosition = this->value.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789_#![]{}()/$ۤ%?<>+:;., *\"");
-  return foundPosition == ls::std::io::section_pair_row_value::npos;
+  return ::std::regex_match(this->value, valueRegex);
+}
+
+::std::string ls::std::io::SectionPairRowValueValidator::_getValidationRegex()
+{
+  ::std::string value = R"([a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$ۤ<>+:;., \*\/"]{1,32})";
+  return value;
 }

+ 33 - 11
test/cases/io/section-pair/validator/SectionPairRowValueValidatorTest.cpp

@@ -3,13 +3,14 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-09
-* Changed:         2023-02-17
+* Changed:         2023-02-18
 *
 * */
 
 #include <gtest/gtest.h>
 #include <ls-std/ls-std-core.hpp>
 #include <ls-std/ls-std-io.hpp>
+#include <string>
 
 using namespace ls::std::core;
 using namespace ls::std::io;
@@ -31,24 +32,45 @@ namespace
       {}
   };
 
+  class SectionPairRowValueValidatorValidTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairRowValueValidatorValidTest() = default;
+      ~SectionPairRowValueValidatorValidTest() override = default;
+  };
+
+  class SectionPairRowValueValidatorNotValidTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      SectionPairRowValueValidatorNotValidTest() = default;
+      ~SectionPairRowValueValidatorNotValidTest() override = default;
+  };
+
   TEST_F(SectionPairRowValueValidatorTest, getClassName)
   {
     ASSERT_STREQ("SectionPairRowValueValidator", SectionPairRowValueValidator{"any value"}.getClassName().c_str());
   }
 
-  TEST_F(SectionPairRowValueValidatorTest, isValid)
+  TEST_F(SectionPairRowValueValidatorTest, getValidationRegex)
   {
-    ASSERT_TRUE(SectionPairRowValueValidator{"blue is my favourite color!"}.isValid());
-    ASSERT_TRUE(SectionPairRowValueValidator{"Age"}.isValid());
-    ASSERT_TRUE(SectionPairRowValueValidator{"Tom"}.isValid());
-    ASSERT_TRUE(SectionPairRowValueValidator{"\"Tom\""}.isValid());
-    ASSERT_TRUE(SectionPairRowValueValidator{"Hello!" + NewLine::get()}.isValid());
-    ASSERT_TRUE(SectionPairRowValueValidator{"Hello!" + NewLine::getWindowsNewLine()}.isValid());
+    string expected = R"(([a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$ۤ<>+:;., \*\/"]{1,32})|([a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$ۤ<>+:;., \*\/"]{1,32}\n{1})|([a-zA-Z0-9\-_#!?\[\]\{\}\(\)\$ۤ<>+:;., \*\/"]{1,32}\r{1}\n{1}))";
+    string actual = SectionPairRowValueValidator::getValidationRegex();
+
+    ASSERT_STREQ(expected.c_str(), actual.c_str());
   }
 
-  TEST_F(SectionPairRowValueValidatorTest, isValid_not_valid)
+  TEST_P(SectionPairRowValueValidatorValidTest, isValid)
   {
-    ASSERT_FALSE(SectionPairRowValueValidator{"1+2=3"}.isValid());
-    ASSERT_FALSE(SectionPairRowValueValidator{"\\escape"}.isValid());
+    ASSERT_TRUE(SectionPairRowValueValidator{GetParam()}.isValid());
   }
+
+  TEST_P(SectionPairRowValueValidatorNotValidTest, isValid_not_valid)
+  {
+    ASSERT_FALSE(SectionPairRowValueValidator{GetParam()}.isValid());
+  }
+
+  INSTANTIATE_TEST_SUITE_P(SectionPairRowValueValidatorTest, SectionPairRowValueValidatorValidTest, ::testing::Values("blue is my favourite color!", "Age", "Tom", "\"Tom\"", "Hello!" + NewLine::getUnixNewLine(), "Hello!" + NewLine::getWindowsNewLine()));
+  INSTANTIATE_TEST_SUITE_P(SectionPairRowValueValidatorTest, SectionPairRowValueValidatorNotValidTest, ::testing::Values("1+2=3", "\\escape"));
 }