NarratorTest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-14
  6. * Changed: 2023-05-16
  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. public:
  27. NarratorTest() = default;
  28. ~NarratorTest() override = default;
  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(this->mercedes1));
  47. ASSERT_TRUE(paintingMachine.addListener(this->mercedes2));
  48. ASSERT_TRUE(paintingMachine.addListener(this->mercedes3));
  49. }
  50. TEST_F(NarratorTest, addListener_listener_already_exists)
  51. {
  52. this->createCars();
  53. Narrator paintingMachine{};
  54. ASSERT_TRUE(paintingMachine.addListener(this->mercedes1));
  55. ASSERT_FALSE(paintingMachine.addListener(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(this->mercedes1);
  78. paintingMachine.addListener(this->mercedes2);
  79. paintingMachine.addListener(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(this->mercedes1);
  94. paintingMachine.addListener(this->mercedes2);
  95. paintingMachine.addListener(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(this->mercedes1);
  128. paintingMachine.addListener(this->mercedes2);
  129. paintingMachine.addListener(this->mercedes3);
  130. ASSERT_STREQ("pink", this->mercedes1->getColor().data());
  131. ASSERT_STREQ("blue", this->mercedes2->getColor().data());
  132. ASSERT_STREQ("red", this->mercedes3->getColor().data());
  133. auto newColor = Colour{"black"};
  134. paintingMachine.tell(static_cast<const Class &>(newColor));
  135. ASSERT_STREQ("black", this->mercedes1->getColor().data());
  136. ASSERT_STREQ("black", this->mercedes2->getColor().data());
  137. ASSERT_STREQ("black", this->mercedes3->getColor().data());
  138. }
  139. }