SectionPairFileReaderTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. parameter.setDocument(make_shared<SectionPairDocument>());
  36. return parameter;
  37. }
  38. static string getMockSectionPairFileContent()
  39. {
  40. return "# section-pair document\n"
  41. "\n"
  42. "[general]\n"
  43. "\n"
  44. "ports:\n"
  45. " 9090\n"
  46. " 8001\n"
  47. " 1989\n"
  48. "host=localhost\n";
  49. }
  50. };
  51. TEST_F(SectionPairFileReaderTest, constructor_file_does_not_exist)
  52. {
  53. EXPECT_THROW(
  54. {
  55. try
  56. {
  57. SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(false)};
  58. }
  59. catch (const FileNotFoundException &_exception)
  60. {
  61. throw;
  62. }
  63. },
  64. FileNotFoundException);
  65. }
  66. TEST_F(SectionPairFileReaderTest, getClassName)
  67. {
  68. ASSERT_STREQ("SectionPairFileReader", SectionPairFileReader{SectionPairFileReaderTest::createMockParameter(true)}.getClassName().c_str());
  69. }
  70. TEST_F(SectionPairFileReaderTest, getDocument)
  71. {
  72. SectionPairFileReader reader{SectionPairFileReaderTest::createMockParameter(true)};
  73. ASSERT_FALSE(reader.getDocument() == nullptr);
  74. }
  75. TEST_F(SectionPairFileReaderTest, read)
  76. {
  77. SectionPairFileReaderParameter parameter = SectionPairFileReaderTest::createMockParameter(true);
  78. parameter.getDocument()->reserveNewLine(NewLine::getUnixNewLine());
  79. SectionPairFileReader reader{parameter};
  80. shared_ptr<MockFileReader> mockFileReader = dynamic_pointer_cast<MockFileReader>(parameter.getReader());
  81. EXPECT_CALL(*mockFileReader, read()).Times(AtLeast(1));
  82. ON_CALL(*mockFileReader, read()).WillByDefault(Return(SectionPairFileReaderTest::getMockSectionPairFileContent()));
  83. reader.read();
  84. ASSERT_TRUE(!reader.getDocument()->getSectionList().empty());
  85. ASSERT_STREQ("general", reader.getDocument()->get(0)->getSectionId().c_str());
  86. ASSERT_EQ(2, reader.getDocument()->get(0)->getRowAmount());
  87. }
  88. }