StateTest.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-05
  6. * Changed: 2021-05-02
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <memory>
  11. #include <ls_std/ls_std.hpp>
  12. namespace
  13. {
  14. class StateTest : public ::testing::Test
  15. {
  16. protected:
  17. StateTest() = default;
  18. ~StateTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. // implementation
  25. TEST_F(StateTest, marshal)
  26. {
  27. std::shared_ptr<ls_std::State> state = std::make_shared<ls_std::State>("A");
  28. std::shared_ptr<ls_std::ISerializable> serializable = std::make_shared<ls_std::SerializableJsonState>(state);
  29. state->setSerializable(serializable);
  30. ASSERT_FALSE(state->marshal().empty());
  31. }
  32. TEST_F(StateTest, marshal_noSerializableReference)
  33. {
  34. ls_std::State state{"A"};
  35. EXPECT_THROW({
  36. try
  37. {
  38. state.marshal();
  39. }
  40. catch (const ls_std::NullPointerException &_exception)
  41. {
  42. throw;
  43. }
  44. }, ls_std::NullPointerException);
  45. }
  46. TEST_F(StateTest, unmarshal)
  47. {
  48. std::shared_ptr<ls_std::State> state = std::make_shared<ls_std::State>("TMP_ID");
  49. std::shared_ptr<ls_std::ISerializable> serializable = std::make_shared<ls_std::SerializableJsonState>(state);
  50. state->setSerializable(serializable);
  51. std::string jsonString = R"({"id":"A","connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"}}})";
  52. state->unmarshal(jsonString);
  53. ASSERT_STREQ("A", state->getId().c_str());
  54. }
  55. TEST_F(StateTest, unmarshal_noSerializableReference)
  56. {
  57. std::shared_ptr<ls_std::State> state = std::make_shared<ls_std::State>("TMP_ID");
  58. std::string jsonString = R"({"id":"A","connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"}}})";
  59. EXPECT_THROW({
  60. try
  61. {
  62. state->unmarshal(jsonString);
  63. }
  64. catch (const ls_std::NullPointerException &_exception)
  65. {
  66. throw;
  67. }
  68. }, ls_std::NullPointerException);
  69. }
  70. // additional functionality
  71. TEST_F(StateTest, addStateConnection)
  72. {
  73. ls_std::State stateA{"A"};
  74. ls_std::State stateB{"B"};
  75. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls_std::State>(stateB)));
  76. }
  77. TEST_F(StateTest, addStateConnectionNegative)
  78. {
  79. ls_std::State stateA{"A"};
  80. ls_std::State stateB{"B"};
  81. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls_std::State>(stateB)));
  82. ASSERT_FALSE(stateA.addStateConnection("AB", std::make_shared<ls_std::State>(stateB)));
  83. ASSERT_FALSE(stateA.addStateConnection("XX", nullptr));
  84. }
  85. TEST_F(StateTest, addStateConnectionV2)
  86. {
  87. ls_std::State stateA{"A"};
  88. ls_std::State stateB{"B"};
  89. ASSERT_TRUE(stateA.addStateConnection(std::make_shared<ls_std::StateConnection>("AB", stateB.getId())));
  90. }
  91. TEST_F(StateTest, addStateConnectionV2Negative)
  92. {
  93. ls_std::State stateA{"A"};
  94. ASSERT_FALSE(stateA.addStateConnection(nullptr));
  95. }
  96. TEST_F(StateTest, clearConnections)
  97. {
  98. ls_std::State stateA{"A"};
  99. stateA.addStateConnection(std::make_shared<ls_std::StateConnection>("AB", "B"));
  100. stateA.addStateConnection(std::make_shared<ls_std::StateConnection>("AC", "C"));
  101. ASSERT_EQ(2, stateA.getConnectedStates().size());
  102. stateA.clearConnections();
  103. ASSERT_EQ(0, stateA.getConnectedStates().size());
  104. ASSERT_TRUE(stateA.getConnectedStates().empty());
  105. }
  106. TEST_F(StateTest, getConnectedStates)
  107. {
  108. ls_std::State stateA{"A"};
  109. ls_std::State stateB{"B"};
  110. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls_std::State>(stateB)));
  111. ASSERT_FALSE(stateA.getConnectedStates().empty());
  112. ASSERT_EQ(1, stateA.getConnectedStates().size());
  113. }
  114. TEST_F(StateTest, getConnectedStatesNegative)
  115. {
  116. ls_std::State stateA{"A"};
  117. ASSERT_TRUE(stateA.getConnectedStates().empty());
  118. ASSERT_EQ(0, stateA.getConnectedStates().size());
  119. }
  120. TEST_F(StateTest, getId)
  121. {
  122. ls_std::State stateA{"A"};
  123. ASSERT_STREQ("A", stateA.getId().c_str());
  124. }
  125. TEST_F(StateTest, setId)
  126. {
  127. ls_std::State stateA{"A"};
  128. ASSERT_STREQ("A", stateA.getId().c_str());
  129. stateA.setId("B");
  130. ASSERT_STREQ("B", stateA.getId().c_str());
  131. }
  132. TEST_F(StateTest, hasConnection)
  133. {
  134. ls_std::State stateA{"A"};
  135. ls_std::State stateB{"B"};
  136. ls_std::State stateC{"C"};
  137. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls_std::State>(stateB)));
  138. ASSERT_TRUE(stateA.hasConnection("AB"));
  139. ASSERT_TRUE(stateA.addStateConnection("AC", std::make_shared<ls_std::State>(stateC)));
  140. ASSERT_TRUE(stateA.hasConnection("AC"));
  141. }
  142. TEST_F(StateTest, hasConnectionNegative)
  143. {
  144. ls_std::State stateA{"A"};
  145. ASSERT_FALSE(stateA.hasConnection("AB"));
  146. ASSERT_FALSE(stateA.hasConnection("AC"));
  147. }
  148. TEST_F(StateTest, setSerializable_noSerializableReference)
  149. {
  150. ls_std::State state{"A"};
  151. std::shared_ptr<ls_std::ISerializable> serializable{};
  152. EXPECT_THROW({
  153. try
  154. {
  155. state.setSerializable(serializable);
  156. }
  157. catch (const ls_std::IllegalArgumentException &_exception)
  158. {
  159. throw;
  160. }
  161. }, ls_std::IllegalArgumentException);
  162. }
  163. }