EventHandlerTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2020-11-27
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <ls_std_test.hpp>
  12. namespace {
  13. class EventHandlerTest : public ::testing::Test {
  14. protected:
  15. EventHandlerTest() = default;
  16. ~EventHandlerTest() override = default;
  17. void SetUp() override
  18. {}
  19. void TearDown() override
  20. {}
  21. };
  22. TEST_F(EventHandlerTest, getClassName)
  23. {
  24. ls_std::EventHandler eventHandler {"EventId"};
  25. ASSERT_STREQ("EventHandler", eventHandler.getClassName().c_str());
  26. }
  27. TEST_F(EventHandlerTest, getId)
  28. {
  29. ls_std::EventHandler eventHandler {"EventId"};
  30. ASSERT_STREQ("EventId", eventHandler.getId().c_str());
  31. }
  32. TEST_F(EventHandlerTest, production_example)
  33. {
  34. std::string news, expectedNews {};
  35. ls_std::event_id seriousNewsEventId = ls_std_test::SeriousNewsEvent {""}.getId();
  36. ls_std::event_id gossipNewsEventId = ls_std_test::GossipNewsEvent {""}.getId();
  37. // create event handler
  38. std::shared_ptr<ls_std::EventHandler> seriousNewsEventHandler = std::make_shared<ls_std::EventHandler>(seriousNewsEventId); // event id
  39. std::shared_ptr<ls_std::EventHandler> gossipNewsEventHandler = std::make_shared<ls_std::EventHandler>(gossipNewsEventId); // event id
  40. // create news agency (listener)
  41. std::shared_ptr<ls_std_test::DailyNewsAgency> dailyNews = std::make_shared<ls_std_test::DailyNewsAgency>();
  42. std::shared_ptr<ls_std_test::GossipNewsAgency> gossipNews = std::make_shared<ls_std_test::GossipNewsAgency>();
  43. // add handler of those events, every news agency should know
  44. // this is how agencies know about events
  45. dailyNews->addEventHandler(seriousNewsEventHandler);
  46. gossipNews->addEventHandler(seriousNewsEventHandler);
  47. gossipNews->addEventHandler(gossipNewsEventHandler);
  48. // fire SeriousNewsEvent event with no effect
  49. seriousNewsEventHandler->notify(ls_std_test::SeriousNewsEvent(news)); // event call
  50. ASSERT_TRUE(dailyNews->getNews().empty());
  51. ASSERT_TRUE(gossipNews->getNews().empty());
  52. // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
  53. dailyNews->subscribe(seriousNewsEventId);
  54. gossipNews->subscribe(seriousNewsEventId);
  55. news = "COVID-19 is still going on!";
  56. seriousNewsEventHandler->notify(ls_std_test::SeriousNewsEvent(news)); // event call
  57. expectedNews = "DailyNewsAgency: " + news;
  58. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  59. expectedNews = "GossipNewsAgency: " + news;
  60. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  61. dailyNews->clear();
  62. gossipNews->clear();
  63. // unsubscribe SeriousNewsEvent from GossipNewsAgency
  64. gossipNews->unsubscribe(seriousNewsEventId);
  65. seriousNewsEventHandler->notify(ls_std_test::SeriousNewsEvent(news)); // event call
  66. expectedNews = "DailyNewsAgency: " + news;
  67. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  68. ASSERT_TRUE(gossipNews->getNews().empty());
  69. dailyNews->clear();
  70. gossipNews->clear();
  71. // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
  72. gossipNews->subscribe(gossipNewsEventId);
  73. gossipNews->subscribe(seriousNewsEventId);
  74. news = "COVID-19 is still going on!";
  75. seriousNewsEventHandler->notify(ls_std_test::SeriousNewsEvent(news));
  76. expectedNews = "GossipNewsAgency: " + news;
  77. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  78. news = "ape likes banana!";
  79. seriousNewsEventHandler->notify(ls_std_test::GossipNewsEvent(news));
  80. expectedNews = "GossipNewsAgency: " + news;
  81. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  82. }
  83. }