RapidXMLTest.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-19
  6. * Changed: 2020-09-19
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <rapidxml.hpp>
  11. #include "../../../../source/io/File.hpp"
  12. #include "../../../TestHelper.hpp"
  13. #include "../../../../source/io/FileReader.hpp"
  14. #include "../../../../source/boxing/String.hpp"
  15. namespace {
  16. class XMLTest : public ::testing::Test {
  17. protected:
  18. XMLTest() = default;
  19. ~XMLTest() override = default;
  20. void SetUp() override {}
  21. void TearDown() override {}
  22. };
  23. TEST_F(XMLTest, readDocument)
  24. {
  25. rapidxml::xml_document<> document {};
  26. ls_std::File xmlFile {TestHelper::getResourcesFolderLocation() + "/state_machine_test.xml"};
  27. ls_std::FileReader reader {xmlFile};
  28. ls_std::byte_field data = reader.read();
  29. std::vector<ls_std::byte> byteData(data.begin(), data.end());
  30. byteData.push_back('\0');
  31. document.parse<0>(byteData.data());
  32. rapidxml::xml_node<>* parentNode = document.first_node("stateMachine");
  33. ASSERT_TRUE(parentNode != nullptr);
  34. ASSERT_STREQ("test_machine", parentNode->first_attribute("name")->value());
  35. rapidxml::xml_node<>* stateNodes = parentNode->first_node("states");
  36. ASSERT_TRUE(stateNodes != nullptr);
  37. int amount = 1;
  38. rapidxml::xml_node<>* stateNode = stateNodes->first_node("state");
  39. while(stateNode != nullptr) {
  40. stateNode = stateNode->next_sibling("state");
  41. if(stateNode != nullptr) {
  42. amount++;
  43. }
  44. }
  45. ASSERT_EQ(5, amount);
  46. }
  47. TEST_F(XMLTest, readText)
  48. {
  49. rapidxml::xml_document<> document {};
  50. ls_std::String data {R"(<connection id="test">\n</connection>)"}; // needs to have line break?
  51. document.parse<0>(data.getByteData().data());
  52. rapidxml::xml_node<>* connectionNode = document.first_node("connection");
  53. ASSERT_TRUE(connectionNode != nullptr);
  54. ASSERT_STREQ("test", connectionNode->first_attribute("id")->value());
  55. }
  56. }