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<5> 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. TEST_F(JsonObjectSerializationTests, OneStringOverCapacity)
  66. {
  67. object["key1"] = "value1";
  68. object["key2"] = "value2";
  69. object["key3"] = "value3";
  70. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  71. }
  72. TEST_F(JsonObjectSerializationTests, OneInteger)
  73. {
  74. object["key"] = 1;
  75. outputMustBe("{\"key\":1}");
  76. }
  77. TEST_F(JsonObjectSerializationTests, OneDoubleFourDigits)
  78. {
  79. object["key"].set(3.14159265358979323846, 4);
  80. outputMustBe("{\"key\":3.1416}");
  81. }
  82. TEST_F(JsonObjectSerializationTests, OneDoubleDefaultDigits)
  83. {
  84. object["key"] = 3.14159265358979323846;
  85. outputMustBe("{\"key\":3.14}");
  86. }
  87. TEST_F(JsonObjectSerializationTests, OneNull)
  88. {
  89. object["key"] = (char*) 0;
  90. outputMustBe("{\"key\":null}");
  91. }
  92. TEST_F(JsonObjectSerializationTests, OneTrue)
  93. {
  94. object["key"] = true;
  95. outputMustBe("{\"key\":true}");
  96. }
  97. TEST_F(JsonObjectSerializationTests, OneFalse)
  98. {
  99. object["key"] = false;
  100. outputMustBe("{\"key\":false}");
  101. }
  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. }*/