EventManagerTest.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2023-01-06
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_event.hpp>
  11. #include <ls_std_event_test.hpp>
  12. using ls_std_event_test::GossipNewsAgency;
  13. using ls::std::event::EventManager;
  14. using ::std::shared_ptr;
  15. using ::std::make_shared;
  16. namespace
  17. {
  18. class EventManagerTest : public ::testing::Test
  19. {
  20. protected:
  21. EventManagerTest() = default;
  22. ~EventManagerTest() override = default;
  23. shared_ptr<GossipNewsAgency> gossipNewsAgency = make_shared<GossipNewsAgency>();
  24. shared_ptr<EventManager> eventManager = make_shared<EventManager>();
  25. void SetUp() override
  26. {
  27. }
  28. void TearDown() override
  29. {}
  30. };
  31. TEST_F(EventManagerTest, getClassName)
  32. {
  33. ls::std::event::EventManager eventManager{};
  34. ASSERT_STREQ("EventManager", eventManager.getClassName().c_str());
  35. }
  36. TEST_F(EventManagerTest, subscribe_empty_id)
  37. {
  38. EXPECT_THROW(
  39. {
  40. try
  41. {
  42. EventManager eventManager{};
  43. std::function<void(ls::std::event::Event)> listener = std::bind(&GossipNewsAgency::onGossipNews, gossipNewsAgency, ::std::placeholders::_1);
  44. eventManager.subscribe("", listener);
  45. }
  46. catch (const ls::std::core::IllegalArgumentException &_exception)
  47. {
  48. throw;
  49. }
  50. }, ls::std::core::IllegalArgumentException);
  51. }
  52. TEST_F(EventManagerTest, subscribe_no_listener)
  53. {
  54. EXPECT_THROW(
  55. {
  56. try
  57. {
  58. EventManager eventManager{};
  59. eventManager.subscribe("TMP_ID", nullptr);
  60. }
  61. catch (const ls::std::core::IllegalArgumentException &_exception)
  62. {
  63. throw;
  64. }
  65. }, ls::std::core::IllegalArgumentException);
  66. }
  67. TEST_F(EventManagerTest, unsubscribe_empty_id)
  68. {
  69. EXPECT_THROW({
  70. try
  71. {
  72. EventManager eventManager{};
  73. std::function<void(ls::std::event::Event)> listener = std::bind(&GossipNewsAgency::onGossipNews, gossipNewsAgency, ::std::placeholders::_1);
  74. eventManager.unsubscribe("", listener);
  75. }
  76. catch (const ls::std::core::IllegalArgumentException &_exception)
  77. {
  78. throw;
  79. }
  80. }, ls::std::core::IllegalArgumentException);
  81. }
  82. TEST_F(EventManagerTest, unsubscribe_no_listener)
  83. {
  84. EXPECT_THROW({
  85. try
  86. {
  87. EventManager eventManager{};
  88. eventManager.unsubscribe("TMP_ID", nullptr);
  89. }
  90. catch (const ls::std::core::IllegalArgumentException &_exception)
  91. {
  92. throw;
  93. }
  94. }, ls::std::core::IllegalArgumentException);
  95. }
  96. TEST_F(EventManagerTest, production_example)
  97. {
  98. }
  99. // TEST_F(EventManagerTest, production_example)
  100. // {
  101. // ::std::string news, expectedNews{};
  102. // ls::std::core::type::event_id seriousNewsEventId = ls_std_event_test::SeriousNewsEvent{""}.getId();
  103. // ls::std::core::type::event_id gossipNewsEventId = ls_std_event_test::GossipNewsEvent{""}.getId();
  104. //
  105. // // create event handler
  106. //
  107. // ::std::shared_ptr<ls::std::event::EventHandler> seriousNewsEventHandler = ::std::make_shared<ls::std::event::EventHandler>(seriousNewsEventId); // event id
  108. // ::std::shared_ptr<ls::std::event::EventHandler> gossipNewsEventHandler = ::std::make_shared<ls::std::event::EventHandler>(gossipNewsEventId); // event id
  109. //
  110. // // create and fill event manager with handler
  111. //
  112. // ::std::shared_ptr<ls::std::event::EventManager> eventManager = ::std::make_shared<ls::std::event::EventManager>();
  113. // eventManager->addEventHandler(seriousNewsEventHandler);
  114. // eventManager->addEventHandler(gossipNewsEventHandler);
  115. //
  116. // // create news agency (listener)
  117. //
  118. // ::std::shared_ptr<ls_std_event_test::DailyNewsAgency> dailyNews = ::std::make_shared<ls_std_event_test::DailyNewsAgency>();
  119. // ::std::shared_ptr<ls_std_event_test::GossipNewsAgency> gossipNews = ::std::make_shared<ls_std_event_test::GossipNewsAgency>();
  120. //
  121. // // fire SeriousNewsEvent event with no effect
  122. //
  123. // eventManager->fire(ls_std_event_test::SeriousNewsEvent(news)); // event call
  124. // ASSERT_TRUE(dailyNews->getNews().empty());
  125. // ASSERT_TRUE(gossipNews->getNews().empty());
  126. //
  127. // // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
  128. //
  129. // //eventManager->subscribe(seriousNewsEventId, dailyNews->onSeriousNews);
  130. // //eventManager->subscribe(seriousNewsEventId, gossipNews->onSeriousNews);
  131. // news = "COVID-19 is still going on!";
  132. // eventManager->fire(ls_std_event_test::SeriousNewsEvent(news)); // event call
  133. //
  134. // expectedNews = "DailyNewsAgency: " + news;
  135. // ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  136. // expectedNews = "GossipNewsAgency: " + news;
  137. // ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  138. //
  139. // dailyNews->clear();
  140. // gossipNews->clear();
  141. //
  142. // // unsubscribe SeriousNewsEvent from GossipNewsAgency
  143. //
  144. // //eventManager->unsubscribe(seriousNewsEventId, gossipNews->onSeriousNews);
  145. // eventManager->fire(ls_std_event_test::SeriousNewsEvent(news)); // event call
  146. //
  147. // expectedNews = "DailyNewsAgency: " + news;
  148. // ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  149. // ASSERT_TRUE(gossipNews->getNews().empty());
  150. //
  151. // dailyNews->clear();
  152. // gossipNews->clear();
  153. //
  154. // // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
  155. //
  156. // std::function<void(ls::std::event::Event)> listener = ::std::bind(&ls_std_event_test::GossipNewsAgency::onGossipNews, gossipNews, ::std::placeholders::_1);
  157. // eventManager->subscribe(gossipNewsEventId, listener);
  158. // //eventManager->subscribe(seriousNewsEventId, gossipNews->onSeriousNews);
  159. //
  160. // news = "COVID-19 is still going on!";
  161. // eventManager->fire(ls_std_event_test::SeriousNewsEvent(news)); // event call
  162. // expectedNews = "GossipNewsAgency: " + news;
  163. // ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  164. //
  165. // news = "ape likes banana!";
  166. // eventManager->fire(ls_std_event_test::GossipNewsEvent(news)); // event call
  167. // expectedNews = "GossipNewsAgency: " + news;
  168. // ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  169. // }
  170. //
  171. //
  172. // TEST_F(EventManagerTest, ListenerTest)
  173. // {
  174. // ::std::shared_ptr<ls_std_event_test::GossipNewsAgency> gossipNews = ::std::make_shared<ls_std_event_test::GossipNewsAgency>();
  175. // ls::std::core::type::event_id gossipNewsEventId = ls_std_event_test::GossipNewsEvent{""}.getId();
  176. // ::std::shared_ptr<ls::std::event::EventManager> eventManager = ::std::make_shared<ls::std::event::EventManager>();
  177. // std::function<void(ls::std::event::Event)> listener = ::std::bind(&ls_std_event_test::GossipNewsAgency::onGossipNews, gossipNews, ::std::placeholders::_1);
  178. // eventManager->subscribe(gossipNewsEventId, listener);
  179. // ::std::string news = "ape likes banana!";
  180. // eventManager->fire(ls_std_event_test::GossipNewsEvent(news)); // event call
  181. // }
  182. }