XmlReaderTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-10-10
  6. * Changed: 2023-02-23
  7. *
  8. * */
  9. #include <classes/TestHelper.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.hpp>
  13. using ls::std::core::IllegalArgumentException;
  14. using ls::std::io::File;
  15. using ls::std::io::XmlDocument;
  16. using ls::std::io::XmlReader;
  17. using ls::std::test::TestHelper;
  18. using std::make_shared;
  19. using std::shared_ptr;
  20. using std::string;
  21. using testing::Test;
  22. namespace
  23. {
  24. class XmlReaderTest : public Test
  25. {
  26. protected:
  27. XmlReaderTest() = default;
  28. ~XmlReaderTest() override = default;
  29. void SetUp() override
  30. {}
  31. void TearDown() override
  32. {}
  33. };
  34. TEST_F(XmlReaderTest, read)
  35. {
  36. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  37. XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
  38. ASSERT_TRUE(!xmlReader.read().empty());
  39. }
  40. TEST_F(XmlReaderTest, getDocument)
  41. {
  42. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  43. XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
  44. ASSERT_TRUE(xmlReader.getDocument() != nullptr);
  45. }
  46. TEST_F(XmlReaderTest, setDocument)
  47. {
  48. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  49. shared_ptr<XmlDocument> document = make_shared<XmlDocument>();
  50. XmlReader xmlReader{document, xmlPath};
  51. ASSERT_TRUE(xmlReader.getDocument() == document);
  52. document = make_shared<XmlDocument>();
  53. xmlReader.setDocument(document);
  54. ASSERT_TRUE(xmlReader.getDocument() == document);
  55. }
  56. TEST_F(XmlReaderTest, setDocument_no_reference)
  57. {
  58. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  59. shared_ptr<XmlDocument> document = make_shared<XmlDocument>();
  60. XmlReader xmlReader{document, xmlPath};
  61. EXPECT_THROW(
  62. {
  63. try
  64. {
  65. xmlReader.setDocument(nullptr);
  66. }
  67. catch (const IllegalArgumentException &_exception)
  68. {
  69. throw;
  70. }
  71. },
  72. IllegalArgumentException);
  73. }
  74. TEST_F(XmlReaderTest, setFile)
  75. {
  76. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  77. XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
  78. File xmlFile{xmlPath};
  79. xmlReader.setFile(xmlFile);
  80. ASSERT_TRUE(true);
  81. }
  82. TEST_F(XmlReaderTest, setFile_does_not_exist)
  83. {
  84. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  85. XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
  86. File xmlFile{xmlPath};
  87. EXPECT_THROW(
  88. {
  89. try
  90. {
  91. xmlReader.setFile(File{""});
  92. }
  93. catch (const IllegalArgumentException &_exception)
  94. {
  95. throw;
  96. }
  97. },
  98. IllegalArgumentException);
  99. }
  100. }