SectionPairFileReaderTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-21
  6. * Changed: 2023-02-21
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std-io-test.hpp>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.hpp>
  13. #include <memory>
  14. using namespace ls::std::core;
  15. using namespace ls::std::io;
  16. using namespace ::std;
  17. using namespace test::io;
  18. using namespace ::testing;
  19. namespace
  20. {
  21. class SectionPairFileReaderTest : public ::testing::Test
  22. {
  23. protected:
  24. SectionPairFileReaderTest() = default;
  25. ~SectionPairFileReaderTest() override = default;
  26. void SetUp() override
  27. {}
  28. void TearDown() override
  29. {}
  30. static SectionPairFileReaderParameter createMockParameter(bool _fileExists)
  31. {
  32. SectionPairFileReaderParameter parameter{};
  33. parameter.setFileExistenceEvaluator(make_shared<MockFileExistenceEvaluator>(_fileExists));
  34. parameter.setReader(make_shared<MockFileReader>());
  35. return parameter;
  36. }
  37. static string getMockSectionPairFileContent()
  38. {
  39. return "# section-pair document\n"
  40. "\n"
  41. "[general]\n"
  42. "\n"
  43. "ports:\n"
  44. " 9090\n"
  45. " 8001\n"
  46. " 1989\n"
  47. "host=localhost\n";
  48. }
  49. };
  50. TEST_F(SectionPairFileReaderTest, constructor_file_does_not_exist)
  51. {
  52. EXPECT_THROW(
  53. {
  54. try
  55. {
  56. SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(false)};
  57. }
  58. catch (const FileNotFoundException &_exception)
  59. {
  60. throw;
  61. }
  62. },
  63. FileNotFoundException);
  64. }
  65. TEST_F(SectionPairFileReaderTest, getClassName)
  66. {
  67. ASSERT_STREQ("SectionPairFileReader", SectionPairFileReader{SectionPairFileReaderTest::createMockParameter(true)}.getClassName().c_str());
  68. }
  69. TEST_F(SectionPairFileReaderTest, getDocument)
  70. {
  71. SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(true)};
  72. ASSERT_FALSE(reader.getDocument() == nullptr);
  73. }
  74. TEST_F(SectionPairFileReaderTest, read)
  75. {
  76. SectionPairFileReaderParameter parameter = SectionPairFileReaderTest::createMockParameter(true);
  77. SectionPairFileReader reader{parameter};
  78. shared_ptr<MockFileReader> mockFileReader = dynamic_pointer_cast<MockFileReader>(parameter.getReader());
  79. EXPECT_CALL(*mockFileReader, read()).Times(AtLeast(1));
  80. ON_CALL(*mockFileReader, read()).WillByDefault(Return(SectionPairFileReaderTest::getMockSectionPairFileContent()));
  81. reader.read();
  82. ASSERT_TRUE(!reader.getDocument()->getSectionList().empty());
  83. ASSERT_STREQ("general", reader.getDocument()->get(0)->getSectionId().c_str());
  84. ASSERT_EQ(2, reader.getDocument()->get(0)->getRowAmount());
  85. }
  86. }