KvParserTest.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-25
  6. * Changed: 2022-11-09
  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. try
  32. {
  33. KvParser parser{nullptr};
  34. }
  35. catch (const IllegalArgumentException &_exception)
  36. {
  37. throw;
  38. }
  39. }, IllegalArgumentException);
  40. }
  41. TEST_F(KvParserTest, getDocument)
  42. {
  43. KvParser parser{make_shared<KvDocument>()};
  44. ASSERT_TRUE(parser.getDocument() != nullptr);
  45. }
  46. TEST_F(KvParserTest, parse)
  47. {
  48. shared_ptr<KvDocument> document = make_shared<KvDocument>();
  49. KvParser parser{document};
  50. byte_field data = "# starting comment\n\nport=8080; # some comment\nhost=localhost;\nservice-name=deamon;";
  51. parser.parse(data);
  52. ASSERT_EQ(3, document->getPairs().size());
  53. ASSERT_TRUE(document->hasPair("port"));
  54. ASSERT_TRUE(document->hasPair("host"));
  55. ASSERT_TRUE(document->hasPair("service-name"));
  56. ASSERT_STREQ("8080", document->getPairs().at("port").getValue().c_str());
  57. ASSERT_STREQ("localhost", document->getPairs().at("host").getValue().c_str());
  58. ASSERT_STREQ("deamon", document->getPairs().at("service-name").getValue().c_str());
  59. }
  60. TEST_F(KvParserTest, setDocument)
  61. {
  62. // preparation
  63. shared_ptr<KvDocument> document = make_shared<KvDocument>();
  64. KvParser parser{document};
  65. // set and check
  66. shared_ptr<KvDocument> newDocument = make_shared<KvDocument>();
  67. parser.setDocument(newDocument);
  68. ASSERT_TRUE(parser.getDocument() == newDocument);
  69. }
  70. TEST_F(KvParserTest, setDocument_no_reference)
  71. {
  72. shared_ptr<KvDocument> document = make_shared<KvDocument>();
  73. KvParser parser{document};
  74. EXPECT_THROW({
  75. try
  76. {
  77. parser.setDocument(nullptr);
  78. }
  79. catch (const IllegalArgumentException &_exception)
  80. {
  81. throw;
  82. }
  83. }, IllegalArgumentException);
  84. }
  85. }