Эх сурвалжийг харах

Add mock tests for SectionPairFileReader class

Patrick-Christopher Mattulat 1 жил өмнө
parent
commit
a46f3907a6

+ 5 - 1
CMakeLists.txt

@@ -299,6 +299,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${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/reader/SectionPairFileReaderTest.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
@@ -323,7 +324,9 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/io/section-pair/SectionPairSectionProvider.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/io/section-pair/SerializableSectionPairRowProvider.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/io/xml/TestDataFactory.cpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/io/xml/XmlParserTestWrapper.cpp)
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/io/xml/XmlParserTestWrapper.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/io/MockFileExistenceEvaluator.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/io/MockFileReader.cpp)
 
     set(UNIT_TEST_FILES_SERIALIZATION
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/JsonTest.cpp)
@@ -648,6 +651,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
     message("${MODULE_NAME_IO}: Linking libraries for unit test application...")
     target_link_libraries(${MODULE_NAME_IO}-unit-test
             gtest
+            gmock
             gtest_main
             "${MODULE_NAME_IO}"
             "${MODULE_NAME_CORE}")

+ 2 - 1
include/ls-std/io/section-pair/reader/SectionPairFileReader.hpp

@@ -22,7 +22,7 @@ namespace ls::std::io
   {
     public:
 
-      explicit SectionPairFileReader(ls::std::io::SectionPairFileReaderParameter _parameter);
+      explicit SectionPairFileReader(const ls::std::io::SectionPairFileReaderParameter &_parameter);
       ~SectionPairFileReader() override;
 
       [[nodiscard]] ::std::shared_ptr<ls::std::io::SectionPairDocument> getDocument();
@@ -33,6 +33,7 @@ namespace ls::std::io
       ls::std::io::SectionPairFileReaderParameter parameter{};
 
       void _createDocument();
+      void _createFileExistenceEvaluator();
       void _createReader();
   };
 }

+ 6 - 2
include/ls-std/io/section-pair/reader/SectionPairFileReaderParameter.hpp

@@ -10,6 +10,7 @@
 #ifndef LS_STD_SECTION_PAIR_FILE_READER_PARAMETER_HPP
 #define LS_STD_SECTION_PAIR_FILE_READER_PARAMETER_HPP
 
+#include <ls-std/core/interface/IEvaluator.hpp>
 #include <ls-std/core/interface/IReader.hpp>
 #include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
@@ -26,15 +27,18 @@ namespace ls::std::io
       ~SectionPairFileReaderParameter();
 
       [[nodiscard]] ::std::shared_ptr<ls::std::io::SectionPairDocument> getDocument();
+      [[nodiscard]] ::std::shared_ptr<ls::std::core::interface_type::IEvaluator> getFileExistenceEvaluator();
       [[nodiscard]] ::std::string getFilePath();
+      [[nodiscard]] ::std::shared_ptr<ls::std::core::interface_type::IReader> getReader();
       void setDocument(const ::std::shared_ptr<ls::std::io::SectionPairDocument> &_document);
+      void setFileExistenceEvaluator(const ::std::shared_ptr<ls::std::core::interface_type::IEvaluator> &_fileExistenceEvaluator);
       void setFilePath(const ::std::string &_filePath);
-      [[nodiscard]] ::std::shared_ptr<ls::std::core::interface_type::IReader> getReader();
-      void setReader(const ::std::shared_ptr<core::interface_type::IReader> &_reader);
+      void setReader(const ::std::shared_ptr<ls::std::core::interface_type::IReader> &_reader);
 
     private:
 
       ::std::shared_ptr<ls::std::io::SectionPairDocument> document{};
+      ::std::shared_ptr<ls::std::core::interface_type::IEvaluator> fileExistenceEvaluator{};
       ::std::string filePath{};
       ::std::shared_ptr<ls::std::core::interface_type::IReader> reader{};
   };

+ 8 - 2
source/ls-std/io/section-pair/reader/SectionPairFileReader.cpp

@@ -14,10 +14,11 @@
 #include <ls-std/io/section-pair/reader/SectionPairFileReader.hpp>
 #include <memory>
 
-ls::std::io::SectionPairFileReader::SectionPairFileReader(SectionPairFileReaderParameter _parameter) : ls::std::core::Class("SectionPairFileReader")
+ls::std::io::SectionPairFileReader::SectionPairFileReader(const ls::std::io::SectionPairFileReaderParameter &_parameter) : ls::std::core::Class("SectionPairFileReader")
 {
-  ls::std::io::FileExistenceEvaluator{_parameter.getFilePath()}.evaluate();
   this->parameter = _parameter;
+  ls::std::core::ConditionalFunctionExecutor{this->parameter.getFileExistenceEvaluator() == nullptr}.execute([this] { _createFileExistenceEvaluator(); });
+  this->parameter.getFileExistenceEvaluator()->evaluate();
   ls::std::core::ConditionalFunctionExecutor{this->parameter.getReader() == nullptr}.execute([this] { _createReader(); });
   ls::std::core::ConditionalFunctionExecutor{this->parameter.getDocument() == nullptr}.execute([this] { _createDocument(); });
 }
@@ -43,6 +44,11 @@ void ls::std::io::SectionPairFileReader::_createDocument()
   this->parameter.setDocument(::std::make_shared<ls::std::io::SectionPairDocument>());
 }
 
+void ls::std::io::SectionPairFileReader::_createFileExistenceEvaluator()
+{
+  this->parameter.setFileExistenceEvaluator(::std::make_shared<FileExistenceEvaluator>(this->parameter.getFilePath()));
+}
+
 void ls::std::io::SectionPairFileReader::_createReader()
 {
   ls::std::io::File file{this->parameter.getFilePath()};

+ 11 - 1
source/ls-std/io/section-pair/reader/SectionPairFileReaderParameter.cpp

@@ -18,6 +18,11 @@ ls::std::io::SectionPairFileReaderParameter::~SectionPairFileReaderParameter() =
   return this->document;
 }
 
+::std::shared_ptr<ls::std::core::interface_type::IEvaluator> ls::std::io::SectionPairFileReaderParameter::getFileExistenceEvaluator()
+{
+  return this->fileExistenceEvaluator;
+}
+
 ::std::string ls::std::io::SectionPairFileReaderParameter::getFilePath()
 {
   return this->filePath;
@@ -33,12 +38,17 @@ void ls::std::io::SectionPairFileReaderParameter::setDocument(const ::std::share
   this->document = _document;
 }
 
+void ls::std::io::SectionPairFileReaderParameter::setFileExistenceEvaluator(const ::std::shared_ptr<ls::std::core::interface_type::IEvaluator> &_fileExistenceEvaluator)
+{
+  this->fileExistenceEvaluator = _fileExistenceEvaluator;
+}
+
 void ls::std::io::SectionPairFileReaderParameter::setFilePath(const ::std::string &_filePath)
 {
   this->filePath = _filePath;
 }
 
-void ls::std::io::SectionPairFileReaderParameter::setReader(const ::std::shared_ptr<core::interface_type::IReader> &_reader)
+void ls::std::io::SectionPairFileReaderParameter::setReader(const ::std::shared_ptr<ls::std::core::interface_type::IReader> &_reader)
 {
   this->reader = _reader;
 }

+ 0 - 33
test/cases/io/section-pair/reader/SectionPairFileReaderIT.cpp

@@ -39,39 +39,6 @@ namespace
       }
   };
 
-  TEST_F(SectionPairFileReaderIT, constructor_file_does_not_exist)
-  {
-    EXPECT_THROW(
-        {
-          try
-          {
-            SectionPairFileReader reader{SectionPairFileReaderParameter{}};
-          }
-          catch (const FileNotFoundException &_exception)
-          {
-            throw;
-          }
-        },
-        FileNotFoundException);
-  }
-
-  TEST_F(SectionPairFileReaderIT, getClassName)
-  {
-    SectionPairFileReaderParameter parameter{};
-    parameter.setFilePath(SectionPairFileReaderIT::getSectionPairFileLocation());
-
-    ASSERT_STREQ("SectionPairFileReader", SectionPairFileReader{parameter}.getClassName().c_str());
-  }
-
-  TEST_F(SectionPairFileReaderIT, getDocument)
-  {
-    SectionPairFileReaderParameter parameter{};
-    parameter.setFilePath(SectionPairFileReaderIT::getSectionPairFileLocation());
-    SectionPairFileReader reader{parameter};
-
-    ASSERT_FALSE(reader.getDocument() == nullptr);
-  }
-
   TEST_F(SectionPairFileReaderIT, read)
   {
     SectionPairFileReaderParameter parameter{};

+ 13 - 0
test/cases/io/section-pair/reader/SectionPairFileReaderParameterTest.cpp

@@ -34,6 +34,11 @@ namespace
     ASSERT_TRUE(SectionPairFileReaderParameter{}.getDocument() == nullptr);
   }
 
+  TEST_F(SectionPairFileReaderParameterTest, getFileExistenceEvaluator)
+  {
+    ASSERT_TRUE(SectionPairFileReaderParameter{}.getFileExistenceEvaluator() == nullptr);
+  }
+
   TEST_F(SectionPairFileReaderParameterTest, getFilePath)
   {
     ASSERT_TRUE(SectionPairFileReaderParameter{}.getFilePath().empty());
@@ -52,6 +57,14 @@ namespace
     ASSERT_FALSE(parameter.getDocument() == nullptr);
   }
 
+  TEST_F(SectionPairFileReaderParameterTest, setFileExistenceEvaluator)
+  {
+    SectionPairFileReaderParameter parameter{};
+    parameter.setFileExistenceEvaluator(make_shared<FileExistenceEvaluator>("/var/log/file.txt"));
+
+    ASSERT_FALSE(parameter.getFileExistenceEvaluator() == nullptr);
+  }
+
   TEST_F(SectionPairFileReaderParameterTest, setFilePath)
   {
     SectionPairFileReaderParameter parameter{};

+ 102 - 0
test/cases/io/section-pair/reader/SectionPairFileReaderTest.cpp

@@ -0,0 +1,102 @@
+/*
+* 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-io-test.hpp>
+#include <ls-std/ls-std-core.hpp>
+#include <ls-std/ls-std-io.hpp>
+#include <memory>
+
+using namespace ls::std::core;
+using namespace ls::std::io;
+using namespace ::std;
+using namespace test::io;
+using namespace ::testing;
+
+namespace
+{
+  class SectionPairFileReaderTest : public ::testing::Test
+  {
+    protected:
+
+      SectionPairFileReaderTest() = default;
+      ~SectionPairFileReaderTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+
+      static SectionPairFileReaderParameter createMockParameter(bool _fileExists)
+      {
+        SectionPairFileReaderParameter parameter{};
+        parameter.setFileExistenceEvaluator(make_shared<MockFileExistenceEvaluator>(_fileExists));
+        parameter.setReader(make_shared<MockFileReader>());
+
+        return parameter;
+      }
+
+      static string getMockSectionPairFileContent()
+      {
+        return "# section-pair document\n"
+               "\n"
+               "[general]\n"
+               "\n"
+               "ports:\n"
+               "  9090\n"
+               "  8001\n"
+               "  1989\n"
+               "host=localhost\n";
+      }
+  };
+
+  TEST_F(SectionPairFileReaderTest, constructor_file_does_not_exist)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(false)};
+          }
+          catch (const FileNotFoundException &_exception)
+          {
+            throw;
+          }
+        },
+        FileNotFoundException);
+  }
+
+  TEST_F(SectionPairFileReaderTest, getClassName)
+  {
+    ASSERT_STREQ("SectionPairFileReader", SectionPairFileReader{SectionPairFileReaderTest::createMockParameter(true)}.getClassName().c_str());
+  }
+
+  TEST_F(SectionPairFileReaderTest, getDocument)
+  {
+    SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(true)};
+    ASSERT_FALSE(reader.getDocument() == nullptr);
+  }
+
+  TEST_F(SectionPairFileReaderTest, read)
+  {
+    SectionPairFileReaderParameter parameter = SectionPairFileReaderTest::createMockParameter(true);
+    SectionPairFileReader reader{parameter};
+    shared_ptr<MockFileReader> mockFileReader = dynamic_pointer_cast<MockFileReader>(parameter.getReader());
+
+    EXPECT_CALL(*mockFileReader, read()).Times(AtLeast(1));
+    ON_CALL(*mockFileReader, read()).WillByDefault(Return(SectionPairFileReaderTest::getMockSectionPairFileContent()));
+
+    reader.read();
+
+    ASSERT_TRUE(!reader.getDocument()->getSectionList().empty());
+    ASSERT_STREQ("general", reader.getDocument()->get(0)->getSectionId().c_str());
+    ASSERT_EQ(2, reader.getDocument()->get(0)->getRowAmount());
+  }
+}

+ 26 - 0
test/classes/io/MockFileExistenceEvaluator.cpp

@@ -0,0 +1,26 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#include "MockFileExistenceEvaluator.hpp"
+#include <ls-std/core/exception/FileNotFoundException.hpp>
+#include <string>
+
+test::io::MockFileExistenceEvaluator::MockFileExistenceEvaluator(bool _fileExists) : ls::std::core::Class("MockFileExistenceEvaluator"), fileExists(_fileExists)
+{}
+
+test::io::MockFileExistenceEvaluator::~MockFileExistenceEvaluator() = default;
+
+void test::io::MockFileExistenceEvaluator::evaluate()
+{
+  if (!this->fileExists)
+  {
+    ::std::string message = this->getClassName() + " called - this is just a simulation!";
+    throw ls::std::core::FileNotFoundException{message};
+  }
+}

+ 33 - 0
test/classes/io/MockFileExistenceEvaluator.hpp

@@ -0,0 +1,33 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#ifndef LS_STD_MOCK_FILE_EXISTENCE_EVALUATOR_HPP
+#define LS_STD_MOCK_FILE_EXISTENCE_EVALUATOR_HPP
+
+#include <ls-std/core/Class.hpp>
+#include <ls-std/core/interface/IEvaluator.hpp>
+
+namespace test::io
+{
+  class MockFileExistenceEvaluator : public ls::std::core::Class, public ls::std::core::interface_type::IEvaluator
+  {
+    public:
+
+      explicit MockFileExistenceEvaluator(bool _fileExists);
+      ~MockFileExistenceEvaluator() override;
+
+      void evaluate() override;
+
+    private:
+
+      bool fileExists{};
+  };
+}
+
+#endif

+ 14 - 0
test/classes/io/MockFileReader.cpp

@@ -0,0 +1,14 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#include "MockFileReader.hpp"
+
+test::io::MockFileReader::MockFileReader() = default;
+
+test::io::MockFileReader::~MockFileReader() = default;

+ 30 - 0
test/classes/io/MockFileReader.hpp

@@ -0,0 +1,30 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-21
+* Changed:         2023-02-21
+*
+* */
+
+#ifndef LS_STD_MOCK_FILE_READER_HPP
+#define LS_STD_MOCK_FILE_READER_HPP
+
+#include <gmock/gmock.h>
+#include <ls-std/core/interface/IReader.hpp>
+#include <ls-std/core/type/Types.hpp>
+
+namespace test::io
+{
+  class MockFileReader : public ls::std::core::interface_type::IReader
+  {
+    public:
+
+      MockFileReader();
+      ~MockFileReader() override;
+
+      MOCK_METHOD(ls::std::core::type::byte_field, read, (), (override));
+  };
+}
+
+#endif

+ 3 - 1
test/ls-std-io-test.hpp

@@ -3,13 +3,15 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-05-14
- * Changed:         2023-02-17
+ * Changed:         2023-02-21
  *
  * */
 
 #ifndef LS_STD_LS_STD_IO_TEST_HPP
 #define LS_STD_LS_STD_IO_TEST_HPP
 
+#include <classes/io/MockFileExistenceEvaluator.hpp>
+#include <classes/io/MockFileReader.hpp>
 #include <classes/io/section-pair/SectionPairDocumentProvider.hpp>
 #include <classes/io/section-pair/SectionPairSectionProvider.hpp>
 #include <classes/io/section-pair/SerializableSectionPairRowProvider.hpp>