SerializableJSONStateMachineTest.cpp 4.1 KB

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