JsonObject_Serialization_Tests.cpp 3.4 KB

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