NarratorTest.cpp 4.5 KB

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