Browse Source

Add FileExistenceEvaluator class

This evaluator evaluates, whether a file does exist and throws an
FileNotFoundException in case it does not.
Patrick-Christopher Mattulat 1 year ago
parent
commit
f4eed74827

+ 5 - 0
CMakeLists.txt

@@ -168,6 +168,7 @@ set(SOURCE_FILES_EVENT
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/event/Narrator.cpp)
 
 set(SOURCE_FILES_IO
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/evaluator/FileExistenceEvaluator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/logging/Logger.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/logging/LogLevel.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.cpp
@@ -182,6 +183,8 @@ set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairRowSingleValue.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairRowValue.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/model/SectionPairSection.cpp
+        #        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/reader/SectionPairFileReader.cpp
+        #        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/reader/SectionPairFileReaderParameter.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairDocumentValidator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairIdentifierValidator.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/section-pair/validator/SectionPairRowListValueValidator.cpp
@@ -274,6 +277,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/event/TestDataMercedesCar.cpp)
 
     set(TEST_FILES_IO
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/evaluator/FileExistenceEvaluatorTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/logging/LoggerTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/logging/LogLevelTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluatorTest.cpp
@@ -294,6 +298,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/model/SectionPairRowSingleValueTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/model/SectionPairRowTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/model/SectionPairSectionTest.cpp
+            #            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/reader/SectionPairFileReaderParameterTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/serialization/SerializableSectionPairDocumentTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/serialization/SerializableSectionPairParameterTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/section-pair/serialization/SerializableSectionPairRowListValueTest.cpp

+ 35 - 0
include/ls-std/io/evaluator/FileExistenceEvaluator.hpp

@@ -0,0 +1,35 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#ifndef LS_STD_FILE_EXISTENCE_EVALUATOR_HPP
+#define LS_STD_FILE_EXISTENCE_EVALUATOR_HPP
+
+#include <ls-std/core/Class.hpp>
+#include <ls-std/core/interface/IEvaluator.hpp>
+#include <ls-std/os/dynamic-goal.hpp>
+#include <string>
+
+namespace ls::std::io
+{
+  class LS_STD_DYNAMIC_GOAL FileExistenceEvaluator : public ls::std::core::Class, public ls::std::core::interface_type::IEvaluator
+  {
+    public:
+
+      explicit FileExistenceEvaluator(::std::string _filePath);
+      ~FileExistenceEvaluator() override;
+
+      void evaluate() override;
+
+    private:
+
+      ::std::string filePath{};
+  };
+}
+
+#endif

+ 5 - 1
include/ls-std/ls-std-io.hpp

@@ -2,7 +2,7 @@
  * Author:          Patrick-Christopher Mattulat
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
- * Created:         2023-02-11
+ * Created:         2022-05-14
  * Changed:         2023-02-21
  *
  * */
@@ -10,6 +10,8 @@
 #ifndef LS_STD_LS_STD_IO_HPP
 #define LS_STD_LS_STD_IO_HPP
 
+#include <ls-std/io/evaluator/FileExistenceEvaluator.hpp>
+
 #include <ls-std/io/logging/LogLevel.hpp>
 #include <ls-std/io/logging/LogLevelValue.hpp>
 #include <ls-std/io/logging/Logger.hpp>
@@ -28,6 +30,8 @@
 #include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
 #include <ls-std/io/section-pair/model/SectionPairRowValue.hpp>
 #include <ls-std/io/section-pair/model/SectionPairSection.hpp>
+//#include <ls-std/io/section-pair/reader/SectionPairFileReader.hpp>
+//#include <ls-std/io/section-pair/reader/SectionPairFileReaderParameter.hpp>
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairDocument.hpp>
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairParameter.hpp>
 #include <ls-std/io/section-pair/serialization/SerializableSectionPairRow.hpp>

+ 27 - 0
source/ls-std/io/evaluator/FileExistenceEvaluator.cpp

@@ -0,0 +1,27 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#include <ls-std/core/exception/FileNotFoundException.hpp>
+#include <ls-std/io/File.hpp>
+#include <ls-std/io/evaluator/FileExistenceEvaluator.hpp>
+#include <string>
+
+ls::std::io::FileExistenceEvaluator::FileExistenceEvaluator(::std::string _filePath) : ls::std::core::Class("FileExistenceEvaluator"), filePath(::std::move(_filePath))
+{}
+
+ls::std::io::FileExistenceEvaluator::~FileExistenceEvaluator() = default;
+
+void ls::std::io::FileExistenceEvaluator::evaluate()
+{
+  if (!ls::std::io::File{this->filePath}.exists())
+  {
+    ::std::string message = "\"" + this->filePath + "\" does not exist!";
+    throw ls::std::core::FileNotFoundException{message};
+  }
+}

+ 57 - 0
test/cases/io/evaluator/FileExistenceEvaluatorTest.cpp

@@ -0,0 +1,57 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#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;
+using namespace ::std;
+
+namespace
+{
+  class FileExistenceEvaluatorTest : public ::testing::TestWithParam<string>
+  {
+    protected:
+
+      FileExistenceEvaluatorTest() = default;
+      ~FileExistenceEvaluatorTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_P(FileExistenceEvaluatorTest, evaluate)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            FileExistenceEvaluator{GetParam()}.evaluate();
+          }
+          catch (const FileNotFoundException &_exception)
+          {
+            string actual = _exception.what();
+            string expected = "FileNotFoundException thrown - \"" + GetParam() + "\" does not exist!";
+            ;
+
+            ASSERT_STREQ(expected.c_str(), actual.c_str());
+            throw;
+          }
+        },
+        FileNotFoundException);
+  }
+
+  INSTANTIATE_TEST_SUITE_P(FileExistenceEvaluatorTest, FileExistenceEvaluatorTest, ::testing::Values("var/log/log.txt", ".test"));
+}