NarratorTest.cpp 5.0 KB

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