SerializableFactoryTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2021-04-24
  6. * Changed: 2021-05-02
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <ls_std_test.hpp>
  12. namespace
  13. {
  14. class SerializableFactoryTest : public ::testing::Test
  15. {
  16. protected:
  17. SerializableFactoryTest() = default;
  18. ~SerializableFactoryTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. TEST_F(SerializableFactoryTest, addFactory)
  25. {
  26. std::shared_ptr<ls_std_test::SerializableTestFactory> serializableTestFactory = std::make_shared<ls_std_test::SerializableTestFactory>();
  27. ls_std::SerializableJsonFactory serializableFactory{};
  28. ASSERT_TRUE(serializableFactory.addFactory({"TestClass", serializableTestFactory}));
  29. }
  30. TEST_F(SerializableFactoryTest, addFactory_emptyKey)
  31. {
  32. std::shared_ptr<ls_std_test::SerializableTestFactory> serializableTestFactory = std::make_shared<ls_std_test::SerializableTestFactory>();
  33. ls_std::SerializableJsonFactory serializableFactory{};
  34. EXPECT_THROW({
  35. try
  36. {
  37. serializableFactory.addFactory({"", serializableTestFactory});
  38. }
  39. catch (const ls_std::IllegalArgumentException &_exception)
  40. {
  41. throw;
  42. }
  43. }, ls_std::IllegalArgumentException);
  44. }
  45. TEST_F(SerializableFactoryTest, addFactory_nullPointerValueForFactory)
  46. {
  47. std::shared_ptr<ls_std_test::SerializableTestFactory> serializableTestFactory{};
  48. ls_std::SerializableJsonFactory serializableFactory{};
  49. EXPECT_THROW({
  50. try
  51. {
  52. serializableFactory.addFactory({"TestClass", serializableTestFactory});
  53. }
  54. catch (const ls_std::NullPointerException &_exception)
  55. {
  56. throw;
  57. }
  58. }, ls_std::NullPointerException);
  59. }
  60. TEST_F(SerializableFactoryTest, addFactory_dublicatedElement)
  61. {
  62. std::shared_ptr<ls_std_test::SerializableTestFactory> serializableTestFactory = std::make_shared<ls_std_test::SerializableTestFactory>();
  63. ls_std::SerializableJsonFactory serializableFactory{};
  64. ASSERT_TRUE(serializableFactory.addFactory({"TestClass", serializableTestFactory}));
  65. ASSERT_FALSE(serializableFactory.addFactory({"TestClass", serializableTestFactory}));
  66. }
  67. TEST_F(SerializableFactoryTest, build)
  68. {
  69. ls_std::SerializableJsonFactory serializableFactory{};
  70. std::shared_ptr<ls_std::Class> serializable = serializableFactory.build(ls_std::Boolean{}.getClassName());
  71. ASSERT_TRUE(serializable != nullptr);
  72. ASSERT_STREQ(ls_std::Boolean{}.getClassName().c_str(), serializable->getClassName().c_str());
  73. }
  74. TEST_F(SerializableFactoryTest, build_factoryNotAvailable)
  75. {
  76. ls_std::SerializableJsonFactory serializableFactory{};
  77. std::shared_ptr<ls_std::Class> serializable = serializableFactory.build(ls_std_test::GossipNewsAgency{}.getName());
  78. ASSERT_FALSE(serializable != nullptr);
  79. }
  80. TEST_F(SerializableFactoryTest, clear)
  81. {
  82. ls_std::SerializableJsonFactory serializableFactory{};
  83. ASSERT_TRUE(serializableFactory.clear());
  84. }
  85. TEST_F(SerializableFactoryTest, hasFactory_ofBoxingPackage)
  86. {
  87. ls_std::SerializableJsonFactory serializableFactory{};
  88. ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Boolean{}.getClassName()));
  89. ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Double{}.getClassName()));
  90. ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Float{}.getClassName()));
  91. ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Integer{}.getClassName()));
  92. ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Long{}.getClassName()));
  93. ASSERT_TRUE(serializableFactory.hasFactory(ls_std::String{}.getClassName()));
  94. }
  95. TEST_F(SerializableFactoryTest, hasFactory_factoryNotAvailable)
  96. {
  97. ls_std::SerializableJsonFactory serializableFactory{};
  98. ASSERT_FALSE(serializableFactory.hasFactory(ls_std_test::GossipNewsAgency{}.getName()));
  99. }
  100. TEST_F(SerializableFactoryTest, removeFactory)
  101. {
  102. ls_std::SerializableJsonFactory serializableFactory{};
  103. ASSERT_TRUE(serializableFactory.removeFactory(ls_std::Boolean{}.getClassName()));
  104. }
  105. TEST_F(SerializableFactoryTest, removeFactory_factoryNotAvailable)
  106. {
  107. ls_std::SerializableJsonFactory serializableFactory{};
  108. ASSERT_FALSE(serializableFactory.removeFactory(ls_std_test::GossipNewsAgency{}.getName()));
  109. }
  110. }