SerializableJsonStateMachineTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-17
  6. * Changed: 2021-05-02
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestDataFactory.hpp>
  12. #include <TestHelper.hpp>
  13. namespace
  14. {
  15. class SerializableJsonStateMachineTest : public ::testing::Test
  16. {
  17. protected:
  18. SerializableJsonStateMachineTest() = default;
  19. ~SerializableJsonStateMachineTest() override = default;
  20. void SetUp() override
  21. {}
  22. void TearDown() override
  23. {}
  24. };
  25. // implementation
  26. TEST_F(SerializableJsonStateMachineTest, marshal)
  27. {
  28. ls_std::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
  29. stateMachine.setStartState("A");
  30. stateMachine.setMemory({"A", "B", "C"});
  31. ls_std::SerializableJsonStateMachine serializable{std::make_shared<ls_std::StateMachine>(stateMachine)};
  32. std::string jsonString = serializable.marshal();
  33. ASSERT_TRUE(!jsonString.empty());
  34. ls_std::File file{TestHelper::getResourcesFolderLocation() + "/state_machine_test.json"};
  35. ls_std::FileReader reader{file};
  36. ls_std::String data{reader.read()};
  37. ASSERT_TRUE(data.contains(jsonString));
  38. }
  39. TEST_F(SerializableJsonStateMachineTest, unmarshal)
  40. {
  41. ls_std::File file{TestHelper::getResourcesFolderLocation() + "/state_machine_test.json"};
  42. ls_std::FileReader reader{file};
  43. std::shared_ptr<ls_std::StateMachine> x = std::make_shared<ls_std::StateMachine>("bla");
  44. ls_std::SerializableJsonStateMachine serializable{x};
  45. serializable.unmarshal(reader.read());
  46. ASSERT_STREQ("test_machine", x->getName().c_str());
  47. // check memory
  48. ASSERT_FALSE(x->getMemory().empty());
  49. ASSERT_EQ(3, x->getMemory().size());
  50. ASSERT_STREQ("A", x->getMemory().at(0).c_str());
  51. ASSERT_STREQ("B", x->getMemory().at(1).c_str());
  52. ASSERT_STREQ("C", x->getMemory().at(2).c_str());
  53. // check current state
  54. ASSERT_TRUE(x->getCurrentState() != nullptr);
  55. ASSERT_STREQ("A", x->getCurrentState()->getId().c_str());
  56. // check states
  57. ASSERT_TRUE(!x->getStates().empty());
  58. ASSERT_EQ(5, x->getStates().size());
  59. std::shared_ptr<ls_std::State> state = x->getStates().at("A");
  60. ASSERT_STREQ("A", state->getId().c_str());
  61. ASSERT_EQ(1, state->getConnectedStates().size());
  62. ASSERT_STREQ("AB", state->getConnectedStates().at("AB")->getConnectionId().c_str());
  63. state = x->getStates().at("B");
  64. ASSERT_STREQ("B", state->getId().c_str());
  65. ASSERT_EQ(2, state->getConnectedStates().size());
  66. ASSERT_STREQ("BC", state->getConnectedStates().at("BC")->getConnectionId().c_str());
  67. ASSERT_STREQ("BD", state->getConnectedStates().at("BD")->getConnectionId().c_str());
  68. state = x->getStates().at("C");
  69. ASSERT_STREQ("C", state->getId().c_str());
  70. ASSERT_EQ(2, state->getConnectedStates().size());
  71. ASSERT_STREQ("CB", state->getConnectedStates().at("CB")->getConnectionId().c_str());
  72. ASSERT_STREQ("CE", state->getConnectedStates().at("CE")->getConnectionId().c_str());
  73. state = x->getStates().at("D");
  74. ASSERT_STREQ("D", state->getId().c_str());
  75. ASSERT_EQ(1, state->getConnectedStates().size());
  76. ASSERT_STREQ("DE", state->getConnectedStates().at("DE")->getConnectionId().c_str());
  77. state = x->getStates().at("E");
  78. ASSERT_STREQ("E", state->getId().c_str());
  79. ASSERT_TRUE(state->getConnectedStates().empty());
  80. }
  81. TEST_F(SerializableJsonStateMachineTest, getValue)
  82. {
  83. std::shared_ptr<ls_std::StateMachine> x = std::make_shared<ls_std::StateMachine>("bla");
  84. ls_std::SerializableJsonStateMachine serializable{x};
  85. ASSERT_TRUE(serializable.getValue() == x);
  86. }
  87. TEST_F(SerializableJsonStateMachineTest, setValue)
  88. {
  89. std::shared_ptr<ls_std::StateMachine> x = std::make_shared<ls_std::StateMachine>("bla");
  90. ls_std::SerializableJsonStateMachine serializable{x};
  91. ASSERT_TRUE(serializable.getValue() == x);
  92. x = std::make_shared<ls_std::StateMachine>("bla2");
  93. serializable.setValue(x);
  94. ASSERT_TRUE(serializable.getValue() == x);
  95. }
  96. }