SerializableJsonStateConnectionTest.cpp 3.5 KB

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