JsonObjectSerializationTests.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <gtest/gtest.h>
  2. #include <JsonObject.h>
  3. #include <JsonValue.h>
  4. #include <StaticJsonBuffer.h>
  5. class JsonObjectSerializationTests : public testing::Test
  6. {
  7. protected:
  8. virtual void SetUp()
  9. {
  10. object = json.createObject();
  11. }
  12. void outputMustBe(const char* expected)
  13. {
  14. char actual[256];
  15. int result = object.printTo(actual, sizeof(actual));
  16. EXPECT_STREQ(expected, actual);
  17. EXPECT_EQ(strlen(expected), result);
  18. }
  19. JsonObject object;
  20. private:
  21. StaticJsonBuffer<42> json;
  22. };
  23. TEST_F(JsonObjectSerializationTests, EmptyObject)
  24. {
  25. outputMustBe("{}");
  26. }
  27. TEST_F(JsonObjectSerializationTests, OneString)
  28. {
  29. object["key"] = "value";
  30. outputMustBe("{\"key\":\"value\"}");
  31. }
  32. TEST_F(JsonObjectSerializationTests, TwoStrings)
  33. {
  34. object["key1"] = "value1";
  35. object["key2"] = "value2";
  36. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  37. }
  38. TEST_F(JsonObjectSerializationTests, RemoveFirst)
  39. {
  40. object["key1"] = "value1";
  41. object["key2"] = "value2";
  42. object.remove("key1");
  43. outputMustBe("{\"key2\":\"value2\"}");
  44. }
  45. TEST_F(JsonObjectSerializationTests, RemoveLast)
  46. {
  47. object["key1"] = "value1";
  48. object["key2"] = "value2";
  49. object.remove("key2");
  50. outputMustBe("{\"key1\":\"value1\"}");
  51. }
  52. TEST_F(JsonObjectSerializationTests, RemoveUnexistingKey)
  53. {
  54. object["key1"] = "value1";
  55. object["key2"] = "value2";
  56. object.remove("key3");
  57. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  58. }
  59. TEST_F(JsonObjectSerializationTests, ReplaceExistingKey)
  60. {
  61. object["key"] = "value1";
  62. object["key"] = "value2";
  63. outputMustBe("{\"key\":\"value2\"}");
  64. }
  65. /*
  66. TEST_F(JsonObjectSerializationTests, OneStringOverCapacity)
  67. {
  68. object["key1"] = "value1";
  69. object["key2"] = "value2";
  70. object["key3"] = "value3";
  71. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  72. }
  73. TEST_F(JsonObjectSerializationTests, OneInteger)
  74. {
  75. object["key"] = 1;
  76. outputMustBe("{\"key\":1}");
  77. }
  78. TEST_F(JsonObjectSerializationTests, OneDoubleFourDigits)
  79. {
  80. object["key"].set<4>(3.14159265358979323846);
  81. outputMustBe("{\"key\":3.1416}");
  82. }
  83. TEST_F(JsonObjectSerializationTests, OneDoubleDefaultDigits)
  84. {
  85. object["key"] = 3.14159265358979323846;
  86. outputMustBe("{\"key\":3.14}");
  87. }
  88. TEST_F(JsonObjectSerializationTests, OneNull)
  89. {
  90. object["key"] = (char*) 0;
  91. outputMustBe("{\"key\":null}");
  92. }
  93. TEST_F(JsonObjectSerializationTests, OneTrue)
  94. {
  95. object["key"] = true;
  96. outputMustBe("{\"key\":true}");
  97. }
  98. TEST_F(JsonObjectSerializationTests, OneFalse)
  99. {
  100. object["key"] = false;
  101. outputMustBe("{\"key\":false}");
  102. }
  103. TEST_F(JsonObjectSerializationTests, OneEmptyNestedArray)
  104. {
  105. auto nestedArray = JsonArray<1>();
  106. object["key"] = nestedArray;
  107. outputMustBe("{\"key\":[]}");
  108. }
  109. TEST_F(JsonObjectSerializationTests, OneEmptyNestedObject)
  110. {
  111. auto nestedObject = JsonObject<1>();
  112. object["key"] = nestedObject;
  113. outputMustBe("{\"key\":{}}");
  114. }*/