SerializableJsonStateConnectionTest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-14
  6. * Changed: 2022-05-20
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_logic.hpp>
  12. namespace
  13. {
  14. class SerializableJsonStateConnectionTest : public ::testing::Test
  15. {
  16. protected:
  17. SerializableJsonStateConnectionTest() = default;
  18. ~SerializableJsonStateConnectionTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. TEST_F(SerializableJsonStateConnectionTest, constructor_no_parameter_set)
  25. {
  26. EXPECT_THROW({
  27. try
  28. {
  29. ls::std::logic::SerializableJsonStateConnection serializable{nullptr};
  30. }
  31. catch (const ls::std::core::IllegalArgumentException &_exception)
  32. {
  33. throw;
  34. }
  35. }, ls::std::core::IllegalArgumentException);
  36. }
  37. // implementation
  38. TEST_F(SerializableJsonStateConnectionTest, marshal)
  39. {
  40. ls::std::logic::StateConnection x{"AB", "B"};
  41. ls::std::logic::SerializableJsonStateConnection serializable{::std::make_shared<ls::std::logic::StateConnection>(x)};
  42. ::std::string jsonString{serializable.marshal()};
  43. ASSERT_STREQ(R"({"condition":false,"connectionId":"AB","stateId":"B"})", jsonString.c_str());
  44. }
  45. TEST_F(SerializableJsonStateConnectionTest, unmarshal)
  46. {
  47. ::std::shared_ptr<ls::std::logic::StateConnection> x = ::std::make_shared<ls::std::logic::StateConnection>("AB", "B");
  48. ASSERT_STREQ("AB", x->getConnectionId().c_str());
  49. ASSERT_STREQ("B", x->getStateId().c_str());
  50. ASSERT_FALSE(x->isPassable());
  51. ls::std::logic::SerializableJsonStateConnection serializable{x};
  52. serializable.unmarshal(R"({"condition":true,"connectionId":"BC","stateId":"C"})");
  53. ASSERT_STREQ("BC", x->getConnectionId().c_str());
  54. ASSERT_STREQ("C", x->getStateId().c_str());
  55. ASSERT_TRUE(x->isPassable());
  56. }
  57. TEST_F(SerializableJsonStateConnectionTest, getValue)
  58. {
  59. ::std::shared_ptr<ls::std::logic::StateConnection> x = ::std::make_shared<ls::std::logic::StateConnection>("AB", "B");
  60. ls::std::logic::SerializableJsonStateConnection serializable{x};
  61. ASSERT_TRUE(serializable.getValue() == x);
  62. }
  63. TEST_F(SerializableJsonStateConnectionTest, setValue)
  64. {
  65. ls::std::logic::StateConnection x{"AB", "B"};
  66. ls::std::logic::SerializableJsonStateConnection serializable{::std::make_shared<ls::std::logic::StateConnection>(x)};
  67. ::std::string jsonString{serializable.marshal()};
  68. ASSERT_STREQ(R"({"condition":false,"connectionId":"AB","stateId":"B"})", jsonString.c_str());
  69. // set value should now reset json
  70. ls::std::logic::StateConnection y{"BC", "C"};
  71. serializable.setValue(::std::make_shared<ls::std::logic::StateConnection>(y));
  72. jsonString = serializable.marshal();
  73. ASSERT_STREQ(R"({"condition":false,"connectionId":"BC","stateId":"C"})", jsonString.c_str());
  74. //I love YOU
  75. }
  76. TEST_F(SerializableJsonStateConnectionTest, setValue_no_parameter_set)
  77. {
  78. ls::std::logic::StateConnection stateConnection{"AB", "B"};
  79. ls::std::logic::SerializableJsonStateConnection serializable{::std::make_shared<ls::std::logic::StateConnection>(stateConnection)};
  80. EXPECT_THROW({
  81. try
  82. {
  83. serializable.setValue(nullptr);
  84. }
  85. catch (const ls::std::core::IllegalArgumentException &_exception)
  86. {
  87. throw;
  88. }
  89. }, ls::std::core::IllegalArgumentException);
  90. }
  91. }