KvParserTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-25
  6. * Changed: 2021-07-15
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. namespace
  12. {
  13. class KvParserTest : public ::testing::Test
  14. {
  15. protected:
  16. KvParserTest() = default;
  17. ~KvParserTest() override = default;
  18. void SetUp() override
  19. {}
  20. void TearDown() override
  21. {}
  22. };
  23. TEST_F(KvParserTest, constructor_no_document_reference)
  24. {
  25. EXPECT_THROW({
  26. try
  27. {
  28. ls_std::KvParser parser{nullptr};
  29. }
  30. catch (const ls_std::IllegalArgumentException &_exception)
  31. {
  32. throw;
  33. }
  34. }, ls_std::IllegalArgumentException);
  35. }
  36. TEST_F(KvParserTest, getDocument)
  37. {
  38. ls_std::KvParser parser{std::make_shared<ls_std::KvDocument>()};
  39. ASSERT_TRUE(parser.getDocument() != nullptr);
  40. }
  41. TEST_F(KvParserTest, parse)
  42. {
  43. std::shared_ptr<ls_std::KvDocument> document = std::make_shared<ls_std::KvDocument>();
  44. ls_std::KvParser parser{document};
  45. ls_std::byte_field data = "# starting comment\n\nport=8080; # some comment\nhost=localhost;\nservice-name=deamon;";
  46. parser.parse(data);
  47. ASSERT_EQ(3, document->getPairs().size());
  48. ASSERT_TRUE(document->hasPair("port"));
  49. ASSERT_TRUE(document->hasPair("host"));
  50. ASSERT_TRUE(document->hasPair("service-name"));
  51. ASSERT_STREQ("8080", document->getPairs().at("port").getValue().c_str());
  52. ASSERT_STREQ("localhost", document->getPairs().at("host").getValue().c_str());
  53. ASSERT_STREQ("deamon", document->getPairs().at("service-name").getValue().c_str());
  54. }
  55. TEST_F(KvParserTest, setDocument)
  56. {
  57. // preparation
  58. std::shared_ptr<ls_std::KvDocument> document = std::make_shared<ls_std::KvDocument>();
  59. ls_std::KvParser parser{document};
  60. // set and check
  61. std::shared_ptr<ls_std::KvDocument> newDocument = std::make_shared<ls_std::KvDocument>();
  62. parser.setDocument(newDocument);
  63. ASSERT_TRUE(parser.getDocument() == newDocument);
  64. }
  65. TEST_F(KvParserTest, setDocument_no_reference)
  66. {
  67. std::shared_ptr<ls_std::KvDocument> document = std::make_shared<ls_std::KvDocument>();
  68. ls_std::KvParser parser{document};
  69. EXPECT_THROW({
  70. try
  71. {
  72. parser.setDocument(nullptr);
  73. }
  74. catch (const ls_std::IllegalArgumentException &_exception)
  75. {
  76. throw;
  77. }
  78. }, ls_std::IllegalArgumentException);
  79. }
  80. }