JsonObjectSerializationTests.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. StaticJsonBuffer<5> json;
  21. };
  22. TEST_F(JsonObjectSerializationTests, EmptyObject)
  23. {
  24. outputMustBe("{}");
  25. }
  26. TEST_F(JsonObjectSerializationTests, OneString)
  27. {
  28. object["key"] = "value";
  29. outputMustBe("{\"key\":\"value\"}");
  30. }
  31. TEST_F(JsonObjectSerializationTests, TwoStrings)
  32. {
  33. object["key1"] = "value1";
  34. object["key2"] = "value2";
  35. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  36. }
  37. TEST_F(JsonObjectSerializationTests, RemoveFirst)
  38. {
  39. object["key1"] = "value1";
  40. object["key2"] = "value2";
  41. object.remove("key1");
  42. outputMustBe("{\"key2\":\"value2\"}");
  43. }
  44. TEST_F(JsonObjectSerializationTests, RemoveLast)
  45. {
  46. object["key1"] = "value1";
  47. object["key2"] = "value2";
  48. object.remove("key2");
  49. outputMustBe("{\"key1\":\"value1\"}");
  50. }
  51. TEST_F(JsonObjectSerializationTests, RemoveUnexistingKey)
  52. {
  53. object["key1"] = "value1";
  54. object["key2"] = "value2";
  55. object.remove("key3");
  56. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  57. }
  58. TEST_F(JsonObjectSerializationTests, ReplaceExistingKey)
  59. {
  60. object["key"] = "value1";
  61. object["key"] = "value2";
  62. outputMustBe("{\"key\":\"value2\"}");
  63. }
  64. TEST_F(JsonObjectSerializationTests, OneStringOverCapacity)
  65. {
  66. object["key1"] = "value1";
  67. object["key2"] = "value2";
  68. object["key3"] = "value3";
  69. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  70. }
  71. TEST_F(JsonObjectSerializationTests, OneInteger)
  72. {
  73. object["key"] = 1;
  74. outputMustBe("{\"key\":1}");
  75. }
  76. TEST_F(JsonObjectSerializationTests, OneDoubleFourDigits)
  77. {
  78. object["key"].set(3.14159265358979323846, 4);
  79. outputMustBe("{\"key\":3.1416}");
  80. }
  81. TEST_F(JsonObjectSerializationTests, OneDoubleDefaultDigits)
  82. {
  83. object["key"] = 3.14159265358979323846;
  84. outputMustBe("{\"key\":3.14}");
  85. }
  86. TEST_F(JsonObjectSerializationTests, OneNull)
  87. {
  88. object["key"] = (char*) 0;
  89. outputMustBe("{\"key\":null}");
  90. }
  91. TEST_F(JsonObjectSerializationTests, OneTrue)
  92. {
  93. object["key"] = true;
  94. outputMustBe("{\"key\":true}");
  95. }
  96. TEST_F(JsonObjectSerializationTests, OneFalse)
  97. {
  98. object["key"] = false;
  99. outputMustBe("{\"key\":false}");
  100. }
  101. /*
  102. TEST_F(JsonObjectSerializationTests, OneEmptyNestedArray)
  103. {
  104. auto nestedArray = JsonArray<1>();
  105. object["key"] = nestedArray;
  106. outputMustBe("{\"key\":[]}");
  107. }
  108. */
  109. TEST_F(JsonObjectSerializationTests, OneEmptyNestedObject)
  110. {
  111. auto nestedObject = json.createObject();
  112. object["key"] = nestedObject;
  113. outputMustBe("{\"key\":{}}");
  114. }