SerializableJsonStateTest.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-15
  6. * Changed: 2021-05-02
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. namespace
  12. {
  13. class SerializableJsonStateTest : public ::testing::Test
  14. {
  15. protected:
  16. SerializableJsonStateTest() = default;
  17. ~SerializableJsonStateTest() override = default;
  18. void SetUp() override
  19. {}
  20. void TearDown() override
  21. {}
  22. };
  23. TEST_F(SerializableJsonStateTest, marshal)
  24. {
  25. std::shared_ptr<ls_std::State> x = std::make_shared<ls_std::State>("A");
  26. x->addStateConnection(std::make_shared<ls_std::StateConnection>("AB", "B"));
  27. x->addStateConnection(std::make_shared<ls_std::StateConnection>("AC", "C"));
  28. ls_std::SerializableJsonState serializable{x};
  29. ls_std::byte_field jsonString = serializable.marshal();
  30. ASSERT_TRUE(!jsonString.empty());
  31. std::string expectedJson = R"({"connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"},"AC":{"condition":false,"connectionId":"AC","stateId":"C"}},"id":"A"})";
  32. ASSERT_STREQ(expectedJson.c_str(), jsonString.c_str());
  33. }
  34. TEST_F(SerializableJsonStateTest, unmarshal)
  35. {
  36. std::shared_ptr<ls_std::State> x = std::make_shared<ls_std::State>("A");
  37. ls_std::SerializableJsonState serializable{x};
  38. // before
  39. ASSERT_STREQ("A", x->getId().c_str());
  40. ASSERT_TRUE(x->getConnectedStates().empty());
  41. // after
  42. std::string jsonString = R"({"id":"A","connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"}}})";
  43. serializable.unmarshal(jsonString);
  44. ASSERT_STREQ("A", x->getId().c_str());
  45. ASSERT_EQ(1, x->getConnectedStates().size());
  46. ASSERT_STREQ("AB", x->getConnectedStates().at("AB")->getConnectionId().c_str());
  47. ASSERT_FALSE(x->getConnectedStates().at("AB")->isPassable());
  48. ASSERT_STREQ("B", x->getConnectedStates().at("AB")->getStateId().c_str());
  49. }
  50. TEST_F(SerializableJsonStateTest, getValue)
  51. {
  52. std::shared_ptr<ls_std::State> x = std::make_shared<ls_std::State>("A");
  53. ls_std::SerializableJsonState serializable{x};
  54. ASSERT_TRUE(serializable.getValue() == x);
  55. }
  56. TEST_F(SerializableJsonStateTest, setValue)
  57. {
  58. std::shared_ptr<ls_std::State> x = std::make_shared<ls_std::State>("A");
  59. x->addStateConnection(std::make_shared<ls_std::StateConnection>("AB", "B"));
  60. x->addStateConnection(std::make_shared<ls_std::StateConnection>("AC", "C"));
  61. ls_std::SerializableJsonState serializable{x};
  62. ls_std::byte_field jsonString = serializable.marshal();
  63. std::string expectedJson = R"({"connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"},"AC":{"condition":false,"connectionId":"AC","stateId":"C"}},"id":"A"})";
  64. ASSERT_STREQ(expectedJson.c_str(), jsonString.c_str());
  65. // setValue should now clear json
  66. std::shared_ptr<ls_std::State> y = std::make_shared<ls_std::State>("B");
  67. serializable.setValue(y);
  68. jsonString = serializable.marshal();
  69. ASSERT_STREQ(R"({"id":"B"})", jsonString.c_str());
  70. }
  71. }