StateTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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-11
  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::logic::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::logic::State state = ls::std::logic::State("");
  35. }
  36. catch (const ls::std::core::IllegalArgumentException &_exception)
  37. {
  38. throw;
  39. }
  40. }, ls::std::core::IllegalArgumentException);
  41. }
  42. // additional functionality
  43. TEST_F(StateTest, addStateConnection_v1)
  44. {
  45. ls::std::logic::State stateA{"A"};
  46. ls::std::logic::State stateB{"B"};
  47. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls::std::logic::State>(stateB)));
  48. }
  49. TEST_F(StateTest, addStateConnection_v1_connection_already_exists)
  50. {
  51. ls::std::logic::State stateA{"A"};
  52. ls::std::logic::State stateB{"B"};
  53. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls::std::logic::State>(stateB)));
  54. ASSERT_FALSE(stateA.addStateConnection("AB", std::make_shared<ls::std::logic::State>(stateB)));
  55. }
  56. TEST_F(StateTest, addStateConnection_v1_empty_connection_id)
  57. {
  58. ls::std::logic::State state{"A"};
  59. EXPECT_THROW({
  60. try
  61. {
  62. state.addStateConnection("", std::make_shared<ls::std::logic::State>("B"));
  63. }
  64. catch (const ls::std::core::IllegalArgumentException &_exception)
  65. {
  66. throw;
  67. }
  68. }, ls::std::core::IllegalArgumentException);
  69. }
  70. TEST_F(StateTest, addStateConnection_v1_no_reference)
  71. {
  72. ls::std::logic::State state{"A"};
  73. EXPECT_THROW({
  74. try
  75. {
  76. state.addStateConnection("AB", nullptr);
  77. }
  78. catch (const ls::std::core::IllegalArgumentException &_exception)
  79. {
  80. throw;
  81. }
  82. }, ls::std::core::IllegalArgumentException);
  83. }
  84. TEST_F(StateTest, addStateConnection_v2)
  85. {
  86. ls::std::logic::State stateA{"A"};
  87. ls::std::logic::State stateB{"B"};
  88. ASSERT_TRUE(stateA.addStateConnection(std::make_shared<ls::std::logic::StateConnection>("AB", stateB.getId())));
  89. }
  90. TEST_F(StateTest, addStateConnection_v2_no_reference)
  91. {
  92. ls::std::logic::State stateA{"A"};
  93. EXPECT_THROW({
  94. try
  95. {
  96. stateA.addStateConnection(nullptr);
  97. }
  98. catch (const ls::std::core::IllegalArgumentException &_exception)
  99. {
  100. throw;
  101. }
  102. }, ls::std::core::IllegalArgumentException);
  103. }
  104. TEST_F(StateTest, clearConnections)
  105. {
  106. ls::std::logic::State stateA{"A"};
  107. stateA.addStateConnection(std::make_shared<ls::std::logic::StateConnection>("AB", "B"));
  108. stateA.addStateConnection(std::make_shared<ls::std::logic::StateConnection>("AC", "C"));
  109. ASSERT_FALSE(stateA.getConnectedStates().empty());
  110. stateA.clearConnections();
  111. ASSERT_TRUE(stateA.getConnectedStates().empty());
  112. }
  113. TEST_F(StateTest, getConnectedStates)
  114. {
  115. ls::std::logic::State stateA{"A"};
  116. ASSERT_TRUE(stateA.getConnectedStates().empty());
  117. }
  118. TEST_F(StateTest, getId)
  119. {
  120. ls::std::logic::State stateA{"A"};
  121. ASSERT_STREQ("A", stateA.getId().c_str());
  122. }
  123. TEST_F(StateTest, setId)
  124. {
  125. ls::std::logic::State stateA{"A"};
  126. ASSERT_STREQ("A", stateA.getId().c_str());
  127. stateA.setId("B");
  128. ASSERT_STREQ("B", stateA.getId().c_str());
  129. }
  130. TEST_F(StateTest, setId_empty_id)
  131. {
  132. ls::std::logic::State stateA{"A"};
  133. EXPECT_THROW({
  134. try
  135. {
  136. stateA.setId("");
  137. }
  138. catch (const ls::std::core::IllegalArgumentException &_exception)
  139. {
  140. throw;
  141. }
  142. }, ls::std::core::IllegalArgumentException);
  143. }
  144. TEST_F(StateTest, hasConnection)
  145. {
  146. ls::std::logic::State stateA{"A"};
  147. ls::std::logic::State stateB{"B"};
  148. ls::std::logic::State stateC{"C"};
  149. ASSERT_TRUE(stateA.addStateConnection("AB", std::make_shared<ls::std::logic::State>(stateB)));
  150. ASSERT_TRUE(stateA.hasConnection("AB"));
  151. ASSERT_TRUE(stateA.addStateConnection("AC", std::make_shared<ls::std::logic::State>(stateC)));
  152. ASSERT_TRUE(stateA.hasConnection("AC"));
  153. }
  154. TEST_F(StateTest, hasConnection_no_connections_available)
  155. {
  156. ls::std::logic::State stateA{"A"};
  157. ASSERT_FALSE(stateA.hasConnection("AB"));
  158. }
  159. }