123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include <gtest/gtest.h>
- #include <ls_std/ls_std.hpp>
- #include <ls_std_test.hpp>
- namespace {
- class EventManagerTest : public ::testing::Test {
- protected:
- EventManagerTest() = default;
- ~EventManagerTest() override = default;
- void SetUp() override
- {}
- void TearDown() override
- {}
- };
- TEST_F(EventManagerTest, getClassName)
- {
- ls_std::EventManager eventManager {};
- ASSERT_STREQ("EventManager", eventManager.getClassName().c_str());
- }
- TEST_F(EventManagerTest, production_example)
- {
- std::string news, expectedNews {};
- ls_std::event_id seriousNewsEventId = ls_std_test::SeriousNewsEvent {""}.getId();
- ls_std::event_id gossipNewsEventId = ls_std_test::GossipNewsEvent {""}.getId();
-
- std::shared_ptr<ls_std::EventHandler> seriousNewsEventHandler = std::make_shared<ls_std::EventHandler>(seriousNewsEventId);
- std::shared_ptr<ls_std::EventHandler> gossipNewsEventHandler = std::make_shared<ls_std::EventHandler>(gossipNewsEventId);
-
- std::shared_ptr<ls_std::EventManager> eventManager = std::make_shared<ls_std::EventManager>();
- eventManager->addEventHandler(seriousNewsEventHandler);
- eventManager->addEventHandler(gossipNewsEventHandler);
-
- std::shared_ptr<ls_std_test::DailyNewsAgency> dailyNews = std::make_shared<ls_std_test::DailyNewsAgency>();
- std::shared_ptr<ls_std_test::GossipNewsAgency> gossipNews = std::make_shared<ls_std_test::GossipNewsAgency>();
-
-
- dailyNews->addEventHandler(seriousNewsEventHandler);
- gossipNews->addEventHandler(seriousNewsEventHandler);
- gossipNews->addEventHandler(gossipNewsEventHandler);
-
- eventManager->fire(ls_std_test::SeriousNewsEvent(news));
- ASSERT_TRUE(dailyNews->getNews().empty());
- ASSERT_TRUE(gossipNews->getNews().empty());
-
- dailyNews->subscribe(seriousNewsEventId);
- gossipNews->subscribe(seriousNewsEventId);
- news = "COVID-19 is still going on!";
- eventManager->fire(ls_std_test::SeriousNewsEvent(news));
- expectedNews = "DailyNewsAgency: " + news;
- ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
- expectedNews = "GossipNewsAgency: " + news;
- ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
- dailyNews->clear();
- gossipNews->clear();
-
- gossipNews->unsubscribe(seriousNewsEventId);
- eventManager->fire(ls_std_test::SeriousNewsEvent(news));
- expectedNews = "DailyNewsAgency: " + news;
- ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
- ASSERT_TRUE(gossipNews->getNews().empty());
- dailyNews->clear();
- gossipNews->clear();
-
- gossipNews->subscribe(gossipNewsEventId);
- gossipNews->subscribe(seriousNewsEventId);
- news = "COVID-19 is still going on!";
- eventManager->fire(ls_std_test::SeriousNewsEvent(news));
- expectedNews = "GossipNewsAgency: " + news;
- ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
- news = "ape likes banana!";
- eventManager->fire(ls_std_test::GossipNewsEvent(news));
- expectedNews = "GossipNewsAgency: " + news;
- ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
- }
- }
|