StateTest.cpp 5.2 KB

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