KvParserTest.cpp 2.8 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: 2022-05-12
  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::io::KvParser parser{nullptr};
  29. }
  30. catch (const ls::std::core::IllegalArgumentException &_exception)
  31. {
  32. throw;
  33. }
  34. }, ls::std::core::IllegalArgumentException);
  35. }
  36. TEST_F(KvParserTest, getDocument)
  37. {
  38. ls::std::io::KvParser parser{std::make_shared<ls::std::io::KvDocument>()};
  39. ASSERT_TRUE(parser.getDocument() != nullptr);
  40. }
  41. TEST_F(KvParserTest, parse)
  42. {
  43. std::shared_ptr<ls::std::io::KvDocument> document = std::make_shared<ls::std::io::KvDocument>();
  44. ls::std::io::KvParser parser{document};
  45. ls::std::core::type::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::io::KvDocument> document = std::make_shared<ls::std::io::KvDocument>();
  59. ls::std::io::KvParser parser{document};
  60. // set and check
  61. std::shared_ptr<ls::std::io::KvDocument> newDocument = std::make_shared<ls::std::io::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::io::KvDocument> document = std::make_shared<ls::std::io::KvDocument>();
  68. ls::std::io::KvParser parser{document};
  69. EXPECT_THROW({
  70. try
  71. {
  72. parser.setDocument(nullptr);
  73. }
  74. catch (const ls::std::core::IllegalArgumentException &_exception)
  75. {
  76. throw;
  77. }
  78. }, ls::std::core::IllegalArgumentException);
  79. }
  80. }