SerializableJSONStateTest.cpp 2.9 KB

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