NarratorTest.cpp 4.8 KB

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