SerializableJSONStateTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-15
  6. * Changed: 2020-11-26
  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, getValue)
  47. {
  48. std::shared_ptr<ls_std::State> x = std::make_shared<ls_std::State>("A");
  49. ls_std::SerializableJSONState serializable {x};
  50. ASSERT_TRUE(serializable.getValue() == x);
  51. }
  52. TEST_F(SerializableJSONStateTest, setValue)
  53. {
  54. std::shared_ptr<ls_std::State> x = std::make_shared<ls_std::State>("A");
  55. x->addStateConnection(std::make_shared<ls_std::StateConnection>("AB", "B"));
  56. x->addStateConnection(std::make_shared<ls_std::StateConnection>("AC", "C"));
  57. ls_std::SerializableJSONState serializable {x};
  58. ls_std::byte_field jsonString = serializable.marshal();
  59. std::string expectedJSONString = R"({"connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"},"AC":{"condition":false,"connectionId":"AC","stateId":"C"}},"id":"A"})";
  60. ASSERT_STREQ(expectedJSONString.c_str(), jsonString.c_str());
  61. // setValue should now clear json
  62. std::shared_ptr<ls_std::State> y = std::make_shared<ls_std::State>("B");
  63. serializable.setValue(y);
  64. jsonString = serializable.marshal();
  65. ASSERT_STREQ(R"({"id":"B"})", jsonString.c_str());
  66. }
  67. }