EventHandlerTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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-28
  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, getId)
  23. {
  24. ls_std::EventHandler eventHandler {"EventId"};
  25. ASSERT_STREQ("EventId", eventHandler.getId().c_str());
  26. }
  27. TEST_F(EventHandlerTest, production_example)
  28. {
  29. std::string news, expectedNews {};
  30. ls_std::event_id seriousNewsEventId = ls_std_test::SeriousNewsEvent {""}.getId();
  31. ls_std::event_id gossipNewsEventId = ls_std_test::GossipNewsEvent {""}.getId();
  32. // create event handler
  33. std::shared_ptr<ls_std::EventHandler> seriousNewsEventHandler = std::make_shared<ls_std::EventHandler>(seriousNewsEventId); // event id
  34. std::shared_ptr<ls_std::EventHandler> gossipNewsEventHandler = std::make_shared<ls_std::EventHandler>(gossipNewsEventId); // event id
  35. // create news agency (listener)
  36. std::shared_ptr<ls_std_test::DailyNewsAgency> dailyNews = std::make_shared<ls_std_test::DailyNewsAgency>();
  37. std::shared_ptr<ls_std_test::GossipNewsAgency> gossipNews = std::make_shared<ls_std_test::GossipNewsAgency>();
  38. // add handler of those events, every news agency should know
  39. // this is how agencies know about events
  40. dailyNews->addEventHandler(seriousNewsEventHandler);
  41. gossipNews->addEventHandler(seriousNewsEventHandler);
  42. gossipNews->addEventHandler(gossipNewsEventHandler);
  43. // fire SeriousNewsEvent event with no effect
  44. seriousNewsEventHandler->tell(ls_std_test::SeriousNewsEvent(news)); // event call
  45. ASSERT_TRUE(dailyNews->getNews().empty());
  46. ASSERT_TRUE(gossipNews->getNews().empty());
  47. // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
  48. dailyNews->subscribe(seriousNewsEventId);
  49. gossipNews->subscribe(seriousNewsEventId);
  50. news = "COVID-19 is still going on!";
  51. seriousNewsEventHandler->tell(ls_std_test::SeriousNewsEvent(news)); // event call
  52. expectedNews = "DailyNewsAgency: " + news;
  53. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  54. expectedNews = "GossipNewsAgency: " + news;
  55. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  56. dailyNews->clear();
  57. gossipNews->clear();
  58. // unsubscribe SeriousNewsEvent from GossipNewsAgency
  59. gossipNews->unsubscribe(seriousNewsEventId);
  60. seriousNewsEventHandler->tell(ls_std_test::SeriousNewsEvent(news)); // event call
  61. expectedNews = "DailyNewsAgency: " + news;
  62. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  63. ASSERT_TRUE(gossipNews->getNews().empty());
  64. dailyNews->clear();
  65. gossipNews->clear();
  66. // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
  67. gossipNews->subscribe(gossipNewsEventId);
  68. gossipNews->subscribe(seriousNewsEventId);
  69. news = "COVID-19 is still going on!";
  70. seriousNewsEventHandler->tell(ls_std_test::SeriousNewsEvent(news));
  71. expectedNews = "GossipNewsAgency: " + news;
  72. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  73. news = "ape likes banana!";
  74. seriousNewsEventHandler->tell(ls_std_test::GossipNewsEvent(news));
  75. expectedNews = "GossipNewsAgency: " + news;
  76. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  77. }
  78. }