StateTest.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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-28
  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. TEST_F(StateTest, getClassName)
  25. {
  26. ls_std::State state{"A"};
  27. ASSERT_STREQ("State", state.getClassName().c_str());
  28. }
  29. TEST_F(StateTest, constructor_empty_id)
  30. {
  31. EXPECT_THROW({
  32. try
  33. {
  34. ls_std::State state = ls_std::State("");
  35. }
  36. catch (const ls_std::IllegalArgumentException &_exception)
  37. {
  38. throw;
  39. }
  40. }, ls_std::IllegalArgumentException);
  41. }
  42. // implementation
  43. TEST_F(StateTest, marshal)
  44. {
  45. std::shared_ptr<ls_std::State> state = std::make_shared<ls_std::State>("A");
  46. std::shared_ptr<ls_std::ISerializable> serializable = std::make_shared<ls_std::SerializableJsonState>(state);
  47. state->setSerializable(serializable);
  48. ASSERT_FALSE(state->marshal().empty());
  49. }
  50. TEST_F(StateTest, marshal_no_serializable_reference)
  51. {
  52. ls_std::State state{"A"};
  53. EXPECT_THROW({
  54. try
  55. {
  56. state.marshal();
  57. }
  58. catch (const ls_std::NullPointerException &_exception)
  59. {
  60. throw;
  61. }
  62. }, ls_std::NullPointerException);
  63. }
  64. TEST_F(StateTest, unmarshal)
  65. {
  66. std::shared_ptr<ls_std::State> state = std::make_shared<ls_std::State>("TMP_ID");
  67. std::shared_ptr<ls_std::ISerializable> serializable = std::make_shared<ls_std::SerializableJsonState>(state);
  68. state->setSerializable(serializable);
  69. std::string jsonString = R"({"id":"A","connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"}}})";
  70. state->unmarshal(jsonString);
  71. ASSERT_STREQ("A", state->getId().c_str());
  72. }
  73. TEST_F(StateTest, unmarshal_no_serializable_reference)
  74. {
  75. std::shared_ptr<ls_std::State> state = std::make_shared<ls_std::State>("TMP_ID");
  76. std::string jsonString = R"({"id":"A","connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"}}})";
  77. EXPECT_THROW({
  78. try
  79. {
  80. state->unmarshal(jsonString);
  81. }
  82. catch (const ls_std::NullPointerException &_exception)
  83. {
  84. throw;
  85. }
  86. }, ls_std::NullPointerException);
  87. }
  88. // additional functionality
  89. TEST_F(StateTest, addStateConnection_v1)
  90. {
  91. ls_std::State stateA{"A"};
  92. ls_std::State stateB{"B"};
  93. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls_std::State>(stateB)));
  94. }
  95. TEST_F(StateTest, addStateConnection_v1_connection_already_exists)
  96. {
  97. ls_std::State stateA{"A"};
  98. ls_std::State stateB{"B"};
  99. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls_std::State>(stateB)));
  100. ASSERT_FALSE(stateA.addStateConnection("AB", std::make_shared<ls_std::State>(stateB)));
  101. }
  102. TEST_F(StateTest, addStateConnection_v1_empty_connection_id)
  103. {
  104. ls_std::State state{"A"};
  105. EXPECT_THROW({
  106. try
  107. {
  108. state.addStateConnection("", std::make_shared<ls_std::State>("B"));
  109. }
  110. catch (const ls_std::IllegalArgumentException &_exception)
  111. {
  112. throw;
  113. }
  114. }, ls_std::IllegalArgumentException);
  115. }
  116. TEST_F(StateTest, addStateConnection_v1_no_reference)
  117. {
  118. ls_std::State state{"A"};
  119. EXPECT_THROW({
  120. try
  121. {
  122. state.addStateConnection("AB", nullptr);
  123. }
  124. catch (const ls_std::IllegalArgumentException &_exception)
  125. {
  126. throw;
  127. }
  128. }, ls_std::IllegalArgumentException);
  129. }
  130. TEST_F(StateTest, addStateConnection_v2)
  131. {
  132. ls_std::State stateA{"A"};
  133. ls_std::State stateB{"B"};
  134. ASSERT_TRUE(stateA.addStateConnection(std::make_shared<ls_std::StateConnection>("AB", stateB.getId())));
  135. }
  136. TEST_F(StateTest, addStateConnection_v2_no_reference)
  137. {
  138. ls_std::State stateA{"A"};
  139. EXPECT_THROW({
  140. try
  141. {
  142. stateA.addStateConnection(nullptr);
  143. }
  144. catch (const ls_std::IllegalArgumentException &_exception)
  145. {
  146. throw;
  147. }
  148. }, ls_std::IllegalArgumentException);
  149. }
  150. TEST_F(StateTest, clearConnections)
  151. {
  152. ls_std::State stateA{"A"};
  153. stateA.addStateConnection(std::make_shared<ls_std::StateConnection>("AB", "B"));
  154. stateA.addStateConnection(std::make_shared<ls_std::StateConnection>("AC", "C"));
  155. ASSERT_FALSE(stateA.getConnectedStates().empty());
  156. stateA.clearConnections();
  157. ASSERT_TRUE(stateA.getConnectedStates().empty());
  158. }
  159. TEST_F(StateTest, getConnectedStates)
  160. {
  161. ls_std::State stateA{"A"};
  162. ASSERT_TRUE(stateA.getConnectedStates().empty());
  163. }
  164. TEST_F(StateTest, getId)
  165. {
  166. ls_std::State stateA{"A"};
  167. ASSERT_STREQ("A", stateA.getId().c_str());
  168. }
  169. TEST_F(StateTest, setId)
  170. {
  171. ls_std::State stateA{"A"};
  172. ASSERT_STREQ("A", stateA.getId().c_str());
  173. stateA.setId("B");
  174. ASSERT_STREQ("B", stateA.getId().c_str());
  175. }
  176. TEST_F(StateTest, setId_empty_id)
  177. {
  178. ls_std::State stateA{"A"};
  179. EXPECT_THROW({
  180. try
  181. {
  182. stateA.setId("");
  183. }
  184. catch (const ls_std::IllegalArgumentException &_exception)
  185. {
  186. throw;
  187. }
  188. }, ls_std::IllegalArgumentException);
  189. }
  190. TEST_F(StateTest, hasConnection)
  191. {
  192. ls_std::State stateA{"A"};
  193. ls_std::State stateB{"B"};
  194. ls_std::State stateC{"C"};
  195. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls_std::State>(stateB)));
  196. ASSERT_TRUE(stateA.hasConnection("AB"));
  197. ASSERT_TRUE(stateA.addStateConnection("AC", std::make_shared<ls_std::State>(stateC)));
  198. ASSERT_TRUE(stateA.hasConnection("AC"));
  199. }
  200. TEST_F(StateTest, hasConnection_no_connections_available)
  201. {
  202. ls_std::State stateA{"A"};
  203. ASSERT_FALSE(stateA.hasConnection("AB"));
  204. }
  205. TEST_F(StateTest, setSerializable_no_reference)
  206. {
  207. ls_std::State state{"A"};
  208. EXPECT_THROW({
  209. try
  210. {
  211. state.setSerializable(nullptr);
  212. }
  213. catch (const ls_std::IllegalArgumentException &_exception)
  214. {
  215. throw;
  216. }
  217. }, ls_std::IllegalArgumentException);
  218. }
  219. }