KvParserTest.cpp 2.5 KB

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