NarratorTest.cpp 5.2 KB

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