NarratorTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-14
  6. * Changed: 2020-11-29
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <classes/observer/TestDataMercedesCar.hpp>
  12. namespace {
  13. class NarratorTest : public ::testing::Test {
  14. protected:
  15. NarratorTest() = default;
  16. ~NarratorTest() override = default;
  17. void SetUp() override {}
  18. void TearDown() override {}
  19. std::shared_ptr<ls_std_test::TestDataMercedesCar> mercedes1 {};
  20. std::shared_ptr<ls_std_test::TestDataMercedesCar> mercedes2 {};
  21. std::shared_ptr<ls_std_test::TestDataMercedesCar> mercedes3 {};
  22. void createCars() {
  23. this->mercedes1 = std::make_shared<ls_std_test::TestDataMercedesCar>();
  24. this->mercedes1->setColor("pink");
  25. this->mercedes2 = std::make_shared<ls_std_test::TestDataMercedesCar>();
  26. this->mercedes2->setColor("blue");
  27. this->mercedes3 = std::make_shared<ls_std_test::TestDataMercedesCar>();
  28. this->mercedes3->setColor("red");
  29. }
  30. };
  31. TEST_F(NarratorTest, addListener)
  32. {
  33. this->createCars();
  34. ls_std::Narrator paintingMachine {};
  35. ASSERT_TRUE(paintingMachine.getListeners().empty());
  36. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes1));
  37. ASSERT_EQ(1, paintingMachine.getListeners().size());
  38. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes2));
  39. ASSERT_EQ(2, paintingMachine.getListeners().size());
  40. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes3));
  41. ASSERT_EQ(3, paintingMachine.getListeners().size());
  42. }
  43. TEST_F(NarratorTest, clear)
  44. {
  45. this->createCars();
  46. ls_std::Narrator paintingMachine {};
  47. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes1));
  48. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes2));
  49. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes3));
  50. ASSERT_FALSE(paintingMachine.getListeners().empty());
  51. paintingMachine.clear();
  52. ASSERT_TRUE(paintingMachine.getListeners().empty());
  53. }
  54. TEST_F(NarratorTest, getListeners)
  55. {
  56. ls_std::Narrator narrator {};
  57. ASSERT_TRUE(narrator.getListeners().empty());
  58. }
  59. TEST_F(NarratorTest, removeListener)
  60. {
  61. this->createCars();
  62. ls_std::Narrator paintingMachine {};
  63. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes1));
  64. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes2));
  65. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes3));
  66. ASSERT_EQ(3, paintingMachine.getListeners().size());
  67. paintingMachine.removeListener(this->mercedes2);
  68. ASSERT_EQ(2, paintingMachine.getListeners().size());
  69. paintingMachine.removeListener(this->mercedes1);
  70. ASSERT_EQ(1, paintingMachine.getListeners().size());
  71. paintingMachine.removeListener(this->mercedes3);
  72. ASSERT_EQ(0, paintingMachine.getListeners().size());
  73. ASSERT_TRUE(paintingMachine.getListeners().empty());
  74. paintingMachine.removeListener(nullptr);
  75. ASSERT_EQ(0, paintingMachine.getListeners().size());
  76. ASSERT_TRUE(paintingMachine.getListeners().empty());
  77. }
  78. TEST_F(NarratorTest, tell)
  79. {
  80. this->createCars();
  81. ls_std::Narrator paintingMachine {};
  82. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes1));
  83. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes2));
  84. paintingMachine.addListener(std::dynamic_pointer_cast<ls_std::IListener>(this->mercedes3));
  85. ASSERT_STREQ("pink", this->mercedes1->getColor().c_str());
  86. ASSERT_STREQ("blue", this->mercedes2->getColor().c_str());
  87. ASSERT_STREQ("red", this->mercedes3->getColor().c_str());
  88. ls_std::String newColor {"black"};
  89. paintingMachine.tell(static_cast<const ls_std::Class &>(newColor));
  90. ASSERT_STREQ("black", this->mercedes1->getColor().c_str());
  91. ASSERT_STREQ("black", this->mercedes2->getColor().c_str());
  92. ASSERT_STREQ("black", this->mercedes3->getColor().c_str());
  93. }
  94. }