SerializableJsonStateMachineTest.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-17
  6. * Changed: 2022-05-16
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_logic.hpp>
  12. #include <ls_std_logic_test.hpp>
  13. #include "TestHelper.hpp"
  14. namespace
  15. {
  16. class SerializableJsonStateMachineTest : public ::testing::Test
  17. {
  18. protected:
  19. SerializableJsonStateMachineTest() = default;
  20. ~SerializableJsonStateMachineTest() override = default;
  21. void SetUp() override
  22. {}
  23. void TearDown() override
  24. {}
  25. };
  26. TEST_F(SerializableJsonStateMachineTest, constructor_no_parameter_set)
  27. {
  28. EXPECT_THROW({
  29. try
  30. {
  31. ls::std::logic::SerializableJsonStateMachine serializable{nullptr};
  32. }
  33. catch (const ls::std::core::IllegalArgumentException &_exception)
  34. {
  35. throw;
  36. }
  37. }, ls::std::core::IllegalArgumentException);
  38. }
  39. // implementation
  40. TEST_F(SerializableJsonStateMachineTest, marshal)
  41. {
  42. ls::std::logic::StateMachine stateMachine = ls_std_logic_test::TestDataFactory::createStateMachine();
  43. stateMachine.setStartState("A");
  44. stateMachine.setMemory({"A", "B", "C"});
  45. ls::std::logic::SerializableJsonStateMachine serializable{std::make_shared<ls::std::logic::StateMachine>(stateMachine)};
  46. std::string jsonString = serializable.marshal();
  47. ASSERT_TRUE(!jsonString.empty());
  48. ::std::string path = ls_std_test::TestHelper::getResourcesFolderLocation() + "/state_machine_test.json";
  49. path = ls_std_test::TestHelper::normalize(path);
  50. ::std::string content = ls_std_test::TestHelper::readFile(path);
  51. ASSERT_TRUE(content.find(jsonString) != ::std::string::npos);
  52. }
  53. TEST_F(SerializableJsonStateMachineTest, unmarshal)
  54. {
  55. ::std::string path = ls_std_test::TestHelper::getResourcesFolderLocation() + "/state_machine_test.json";
  56. path = ls_std_test::TestHelper::normalize(path);
  57. std::shared_ptr<ls::std::logic::StateMachine> machine = std::make_shared<ls::std::logic::StateMachine>("bla");
  58. ls::std::logic::SerializableJsonStateMachine serializable{machine};
  59. serializable.unmarshal(ls_std_test::TestHelper::readFile(path));
  60. ASSERT_STREQ("test_machine", machine->getName().c_str());
  61. // check memory
  62. ASSERT_FALSE(machine->getMemory().empty());
  63. ASSERT_EQ(3, machine->getMemory().size());
  64. ASSERT_STREQ("A", machine->getMemory().at(0).c_str());
  65. ASSERT_STREQ("B", machine->getMemory().at(1).c_str());
  66. ASSERT_STREQ("C", machine->getMemory().at(2).c_str());
  67. // check current state
  68. ASSERT_TRUE(machine->getCurrentState() != nullptr);
  69. ASSERT_STREQ("A", machine->getCurrentState()->getId().c_str());
  70. // check states
  71. ASSERT_TRUE(!machine->getStates().empty());
  72. ASSERT_EQ(5, machine->getStates().size());
  73. std::shared_ptr<ls::std::logic::State> state = machine->getStates().at("A");
  74. ASSERT_STREQ("A", state->getId().c_str());
  75. ASSERT_EQ(1, state->getConnectedStates().size());
  76. ASSERT_STREQ("AB", state->getConnectedStates().at("AB")->getConnectionId().c_str());
  77. state = machine->getStates().at("B");
  78. ASSERT_STREQ("B", state->getId().c_str());
  79. ASSERT_EQ(2, state->getConnectedStates().size());
  80. ASSERT_STREQ("BC", state->getConnectedStates().at("BC")->getConnectionId().c_str());
  81. ASSERT_STREQ("BD", state->getConnectedStates().at("BD")->getConnectionId().c_str());
  82. state = machine->getStates().at("C");
  83. ASSERT_STREQ("C", state->getId().c_str());
  84. ASSERT_EQ(2, state->getConnectedStates().size());
  85. ASSERT_STREQ("CB", state->getConnectedStates().at("CB")->getConnectionId().c_str());
  86. ASSERT_STREQ("CE", state->getConnectedStates().at("CE")->getConnectionId().c_str());
  87. state = machine->getStates().at("D");
  88. ASSERT_STREQ("D", state->getId().c_str());
  89. ASSERT_EQ(1, state->getConnectedStates().size());
  90. ASSERT_STREQ("DE", state->getConnectedStates().at("DE")->getConnectionId().c_str());
  91. state = machine->getStates().at("E");
  92. ASSERT_STREQ("E", state->getId().c_str());
  93. ASSERT_TRUE(state->getConnectedStates().empty());
  94. }
  95. TEST_F(SerializableJsonStateMachineTest, getValue)
  96. {
  97. std::shared_ptr<ls::std::logic::StateMachine> x = std::make_shared<ls::std::logic::StateMachine>("bla");
  98. ls::std::logic::SerializableJsonStateMachine serializable{x};
  99. ASSERT_TRUE(serializable.getValue() == x);
  100. }
  101. TEST_F(SerializableJsonStateMachineTest, setValue)
  102. {
  103. std::shared_ptr<ls::std::logic::StateMachine> x = std::make_shared<ls::std::logic::StateMachine>("bla");
  104. ls::std::logic::SerializableJsonStateMachine serializable{x};
  105. ASSERT_TRUE(serializable.getValue() == x);
  106. x = std::make_shared<ls::std::logic::StateMachine>("bla2");
  107. serializable.setValue(x);
  108. ASSERT_TRUE(serializable.getValue() == x);
  109. }
  110. TEST_F(SerializableJsonStateMachineTest, setValue_no_parameter_set)
  111. {
  112. std::shared_ptr<ls::std::logic::StateMachine> stateMachine = std::make_shared<ls::std::logic::StateMachine>("bla");
  113. ls::std::logic::SerializableJsonStateMachine serializable{stateMachine};
  114. EXPECT_THROW({
  115. try
  116. {
  117. serializable.setValue(nullptr);
  118. }
  119. catch (const ls::std::core::IllegalArgumentException &_exception)
  120. {
  121. throw;
  122. }
  123. }, ls::std::core::IllegalArgumentException);
  124. }
  125. }