XmlReaderTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-10-10
  6. * Changed: 2023-05-17
  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. public:
  27. XmlReaderTest() = default;
  28. ~XmlReaderTest() override = default;
  29. };
  30. TEST_F(XmlReaderTest, read)
  31. {
  32. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  33. XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
  34. ASSERT_TRUE(!xmlReader.read().empty());
  35. }
  36. TEST_F(XmlReaderTest, getDocument)
  37. {
  38. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  39. XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
  40. ASSERT_TRUE(xmlReader.getDocument() != nullptr);
  41. }
  42. TEST_F(XmlReaderTest, setDocument)
  43. {
  44. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  45. auto document = make_shared<XmlDocument>();
  46. XmlReader xmlReader{document, xmlPath};
  47. ASSERT_TRUE(xmlReader.getDocument() == document);
  48. document = make_shared<XmlDocument>();
  49. xmlReader.setDocument(document);
  50. ASSERT_TRUE(xmlReader.getDocument() == document);
  51. }
  52. TEST_F(XmlReaderTest, setDocument_no_reference)
  53. {
  54. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  55. auto document = make_shared<XmlDocument>();
  56. XmlReader xmlReader{document, xmlPath};
  57. EXPECT_THROW(
  58. {
  59. try
  60. {
  61. xmlReader.setDocument(nullptr);
  62. }
  63. catch (const IllegalArgumentException &_exception)
  64. {
  65. throw;
  66. }
  67. },
  68. IllegalArgumentException);
  69. }
  70. TEST_F(XmlReaderTest, setFile)
  71. {
  72. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  73. XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
  74. File xmlFile{xmlPath};
  75. xmlReader.setFile(xmlFile);
  76. ASSERT_TRUE(true);
  77. }
  78. TEST_F(XmlReaderTest, setFile_does_not_exist)
  79. {
  80. string xmlPath = TestHelper::getResourcesFolderLocation() + "state-machine-test.xml";
  81. XmlReader xmlReader{make_shared<XmlDocument>(), xmlPath};
  82. File xmlFile{xmlPath};
  83. EXPECT_THROW(
  84. {
  85. try
  86. {
  87. xmlReader.setFile(File{""});
  88. }
  89. catch (const IllegalArgumentException &_exception)
  90. {
  91. throw;
  92. }
  93. },
  94. IllegalArgumentException);
  95. }
  96. }