NarratorTest.cpp 4.3 KB

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