NarratorTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-14
  6. * Changed: 2022-05-16
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_event.hpp>
  12. #include <ls_std_event_test.hpp>
  13. namespace
  14. {
  15. class NarratorTest : public ::testing::Test
  16. {
  17. protected:
  18. NarratorTest() = default;
  19. ~NarratorTest() override = default;
  20. void SetUp() override
  21. {}
  22. void TearDown() override
  23. {}
  24. std::shared_ptr<ls_std_event_test::TestDataMercedesCar> mercedes1{};
  25. std::shared_ptr<ls_std_event_test::TestDataMercedesCar> mercedes2{};
  26. std::shared_ptr<ls_std_event_test::TestDataMercedesCar> mercedes3{};
  27. void createCars()
  28. {
  29. this->mercedes1 = std::make_shared<ls_std_event_test::TestDataMercedesCar>();
  30. this->mercedes1->setColor("pink");
  31. this->mercedes2 = std::make_shared<ls_std_event_test::TestDataMercedesCar>();
  32. this->mercedes2->setColor("blue");
  33. this->mercedes3 = std::make_shared<ls_std_event_test::TestDataMercedesCar>();
  34. this->mercedes3->setColor("red");
  35. }
  36. };
  37. TEST_F(NarratorTest, addListener)
  38. {
  39. this->createCars();
  40. ls::std::event::Narrator paintingMachine{};
  41. ASSERT_TRUE(paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes1)));
  42. ASSERT_TRUE(paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes2)));
  43. ASSERT_TRUE(paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes3)));
  44. }
  45. TEST_F(NarratorTest, addListener_listener_already_exists)
  46. {
  47. this->createCars();
  48. ls::std::event::Narrator paintingMachine{};
  49. ASSERT_TRUE(paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes1)));
  50. ASSERT_FALSE(paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes1)));
  51. }
  52. TEST_F(NarratorTest, addListener_no_reference)
  53. {
  54. EXPECT_THROW({
  55. try
  56. {
  57. ls::std::event::Narrator paintingMachine{};
  58. paintingMachine.addListener(nullptr);
  59. }
  60. catch (const ls::std::core::IllegalArgumentException &_exception)
  61. {
  62. throw;
  63. }
  64. }, ls::std::core::IllegalArgumentException);
  65. }
  66. TEST_F(NarratorTest, clear)
  67. {
  68. this->createCars();
  69. ls::std::event::Narrator paintingMachine{};
  70. paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes1));
  71. paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes2));
  72. paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes3));
  73. ASSERT_FALSE(paintingMachine.getListeners().empty());
  74. paintingMachine.clear();
  75. ASSERT_TRUE(paintingMachine.getListeners().empty());
  76. }
  77. TEST_F(NarratorTest, getListeners)
  78. {
  79. ls::std::event::Narrator narrator{};
  80. ASSERT_TRUE(narrator.getListeners().empty());
  81. }
  82. TEST_F(NarratorTest, removeListener)
  83. {
  84. this->createCars();
  85. ls::std::event::Narrator paintingMachine{};
  86. paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes1));
  87. paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes2));
  88. paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes3));
  89. ASSERT_TRUE(paintingMachine.removeListener(this->mercedes2));
  90. ASSERT_TRUE(paintingMachine.removeListener(this->mercedes1));
  91. ASSERT_TRUE(paintingMachine.removeListener(this->mercedes3));
  92. ASSERT_TRUE(paintingMachine.getListeners().empty());
  93. }
  94. TEST_F(NarratorTest, removeListener_no_listener_available)
  95. {
  96. this->createCars();
  97. ls::std::event::Narrator paintingMachine{};
  98. ASSERT_FALSE(paintingMachine.removeListener(this->mercedes2));
  99. }
  100. TEST_F(NarratorTest, removeListener_no_reference)
  101. {
  102. EXPECT_THROW({
  103. try
  104. {
  105. ls::std::event::Narrator paintingMachine{};
  106. paintingMachine.removeListener(nullptr);
  107. }
  108. catch (const ls::std::core::IllegalArgumentException &_exception)
  109. {
  110. throw;
  111. }
  112. }, ls::std::core::IllegalArgumentException);
  113. }
  114. TEST_F(NarratorTest, tell)
  115. {
  116. this->createCars();
  117. ls::std::event::Narrator paintingMachine{};
  118. paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes1));
  119. paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes2));
  120. paintingMachine.addListener(std::dynamic_pointer_cast<ls::std::core::interface::IListener>(this->mercedes3));
  121. ASSERT_STREQ("pink", this->mercedes1->getColor().c_str());
  122. ASSERT_STREQ("blue", this->mercedes2->getColor().c_str());
  123. ASSERT_STREQ("red", this->mercedes3->getColor().c_str());
  124. ls_std_event_test::Colour newColor{"black"};
  125. paintingMachine.tell(static_cast<const ls::std::core::Class &>(newColor));
  126. ASSERT_STREQ("black", this->mercedes1->getColor().c_str());
  127. ASSERT_STREQ("black", this->mercedes2->getColor().c_str());
  128. ASSERT_STREQ("black", this->mercedes3->getColor().c_str());
  129. }
  130. }