NarratorTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-14
  6. * Changed: 2021-04-23
  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.getListeners().empty());
  41. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes1));
  42. ASSERT_EQ(1, paintingMachine.getListeners().size());
  43. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes2));
  44. ASSERT_EQ(2, paintingMachine.getListeners().size());
  45. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes3));
  46. ASSERT_EQ(3, paintingMachine.getListeners().size());
  47. }
  48. TEST_F(NarratorTest, clear)
  49. {
  50. this->createCars();
  51. ls_std::Narrator paintingMachine{};
  52. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes1));
  53. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes2));
  54. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes3));
  55. ASSERT_FALSE(paintingMachine.getListeners().empty());
  56. paintingMachine.clear();
  57. ASSERT_TRUE(paintingMachine.getListeners().empty());
  58. }
  59. TEST_F(NarratorTest, getListeners)
  60. {
  61. ls_std::Narrator narrator{};
  62. ASSERT_TRUE(narrator.getListeners().empty());
  63. }
  64. TEST_F(NarratorTest, removeListener)
  65. {
  66. this->createCars();
  67. ls_std::Narrator paintingMachine{};
  68. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes1));
  69. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes2));
  70. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes3));
  71. ASSERT_EQ(3, paintingMachine.getListeners().size());
  72. paintingMachine.removeListener(this->mercedes2);
  73. ASSERT_EQ(2, paintingMachine.getListeners().size());
  74. paintingMachine.removeListener(this->mercedes1);
  75. ASSERT_EQ(1, paintingMachine.getListeners().size());
  76. paintingMachine.removeListener(this->mercedes3);
  77. ASSERT_EQ(0, paintingMachine.getListeners().size());
  78. ASSERT_TRUE(paintingMachine.getListeners().empty());
  79. paintingMachine.removeListener(nullptr);
  80. ASSERT_EQ(0, paintingMachine.getListeners().size());
  81. ASSERT_TRUE(paintingMachine.getListeners().empty());
  82. }
  83. TEST_F(NarratorTest, tell)
  84. {
  85. this->createCars();
  86. ls_std::Narrator paintingMachine{};
  87. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes1));
  88. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes2));
  89. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes3));
  90. ASSERT_STREQ("pink", this->mercedes1->getColor().c_str());
  91. ASSERT_STREQ("blue", this->mercedes2->getColor().c_str());
  92. ASSERT_STREQ("red", this->mercedes3->getColor().c_str());
  93. ls_std::String newColor{"black"};
  94. paintingMachine.tell(static_cast<const ls_std::Class &>(newColor));
  95. ASSERT_STREQ("black", this->mercedes1->getColor().c_str());
  96. ASSERT_STREQ("black", this->mercedes2->getColor().c_str());
  97. ASSERT_STREQ("black", this->mercedes3->getColor().c_str());
  98. }
  99. }