NarratorTest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-14
  6. * Changed: 2023-02-05
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std-event-test.hpp>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-event.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 test::event;
  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. {
  61. try
  62. {
  63. Narrator paintingMachine{};
  64. paintingMachine.addListener(nullptr);
  65. }
  66. catch (const IllegalArgumentException &_exception)
  67. {
  68. throw;
  69. }
  70. },
  71. IllegalArgumentException);
  72. }
  73. TEST_F(NarratorTest, clear)
  74. {
  75. this->createCars();
  76. Narrator paintingMachine{};
  77. paintingMachine.addListener(dynamic_pointer_cast<IListener>(this->mercedes1));
  78. paintingMachine.addListener(dynamic_pointer_cast<IListener>(this->mercedes2));
  79. paintingMachine.addListener(dynamic_pointer_cast<IListener>(this->mercedes3));
  80. ASSERT_FALSE(paintingMachine.getListeners().empty());
  81. paintingMachine.clear();
  82. ASSERT_TRUE(paintingMachine.getListeners().empty());
  83. }
  84. TEST_F(NarratorTest, getListeners)
  85. {
  86. Narrator narrator{};
  87. ASSERT_TRUE(narrator.getListeners().empty());
  88. }
  89. TEST_F(NarratorTest, removeListener)
  90. {
  91. this->createCars();
  92. Narrator paintingMachine{};
  93. paintingMachine.addListener(dynamic_pointer_cast<IListener>(this->mercedes1));
  94. paintingMachine.addListener(dynamic_pointer_cast<IListener>(this->mercedes2));
  95. paintingMachine.addListener(dynamic_pointer_cast<IListener>(this->mercedes3));
  96. ASSERT_TRUE(paintingMachine.removeListener(this->mercedes2));
  97. ASSERT_TRUE(paintingMachine.removeListener(this->mercedes1));
  98. ASSERT_TRUE(paintingMachine.removeListener(this->mercedes3));
  99. ASSERT_TRUE(paintingMachine.getListeners().empty());
  100. }
  101. TEST_F(NarratorTest, removeListener_no_listener_available)
  102. {
  103. this->createCars();
  104. Narrator paintingMachine{};
  105. ASSERT_FALSE(paintingMachine.removeListener(this->mercedes2));
  106. }
  107. TEST_F(NarratorTest, removeListener_no_reference)
  108. {
  109. EXPECT_THROW(
  110. {
  111. try
  112. {
  113. Narrator paintingMachine{};
  114. paintingMachine.removeListener(nullptr);
  115. }
  116. catch (const IllegalArgumentException &_exception)
  117. {
  118. throw;
  119. }
  120. },
  121. IllegalArgumentException);
  122. }
  123. TEST_F(NarratorTest, tell)
  124. {
  125. this->createCars();
  126. Narrator paintingMachine{};
  127. paintingMachine.addListener(dynamic_pointer_cast<IListener>(this->mercedes1));
  128. paintingMachine.addListener(dynamic_pointer_cast<IListener>(this->mercedes2));
  129. paintingMachine.addListener(dynamic_pointer_cast<IListener>(this->mercedes3));
  130. ASSERT_STREQ("pink", this->mercedes1->getColor().c_str());
  131. ASSERT_STREQ("blue", this->mercedes2->getColor().c_str());
  132. ASSERT_STREQ("red", this->mercedes3->getColor().c_str());
  133. Colour newColor{"black"};
  134. paintingMachine.tell(static_cast<const Class &>(newColor));
  135. ASSERT_STREQ("black", this->mercedes1->getColor().c_str());
  136. ASSERT_STREQ("black", this->mercedes2->getColor().c_str());
  137. ASSERT_STREQ("black", this->mercedes3->getColor().c_str());
  138. }
  139. }