KvParserTest.cpp 2.8 KB

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