printTo.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <gtest/gtest.h>
  9. class JsonObject_PrintTo_Tests : public testing::Test {
  10. public:
  11. JsonObject_PrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
  12. protected:
  13. void outputMustBe(const char *expected) {
  14. char actual[256];
  15. size_t actualLen = _object.printTo(actual);
  16. size_t measuredLen = _object.measureLength();
  17. EXPECT_STREQ(expected, actual);
  18. EXPECT_EQ(strlen(expected), actualLen);
  19. EXPECT_EQ(strlen(expected), measuredLen);
  20. }
  21. DynamicJsonBuffer _jsonBuffer;
  22. JsonObject &_object;
  23. };
  24. TEST_F(JsonObject_PrintTo_Tests, EmptyObject) {
  25. outputMustBe("{}");
  26. }
  27. TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
  28. _object["key1"] = "value1";
  29. _object.set("key2", "value2");
  30. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  31. }
  32. TEST_F(JsonObject_PrintTo_Tests, RemoveFirst) {
  33. _object["key1"] = "value1";
  34. _object["key2"] = "value2";
  35. _object.remove("key1");
  36. outputMustBe("{\"key2\":\"value2\"}");
  37. }
  38. TEST_F(JsonObject_PrintTo_Tests, RemoveLast) {
  39. _object["key1"] = "value1";
  40. _object["key2"] = "value2";
  41. _object.remove("key2");
  42. outputMustBe("{\"key1\":\"value1\"}");
  43. }
  44. TEST_F(JsonObject_PrintTo_Tests, RemoveUnexistingKey) {
  45. _object["key1"] = "value1";
  46. _object["key2"] = "value2";
  47. _object.remove("key3");
  48. outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
  49. }
  50. TEST_F(JsonObject_PrintTo_Tests, ReplaceExistingKey) {
  51. _object["key"] = "value1";
  52. _object["key"] = "value2";
  53. outputMustBe("{\"key\":\"value2\"}");
  54. }
  55. TEST_F(JsonObject_PrintTo_Tests, TwoIntegers) {
  56. _object["a"] = 1;
  57. _object.set("b", 2);
  58. outputMustBe("{\"a\":1,\"b\":2}");
  59. }
  60. TEST_F(JsonObject_PrintTo_Tests, RawJson) {
  61. _object["a"] = RawJson("[1,2]");
  62. _object.set("b", RawJson("[4,5]"));
  63. outputMustBe("{\"a\":[1,2],\"b\":[4,5]}");
  64. }
  65. TEST_F(JsonObject_PrintTo_Tests, TwoDoublesFourDigits) {
  66. _object["a"] = double_with_n_digits(3.14159265358979323846, 4);
  67. _object.set("b", 2.71828182845904523536, 4);
  68. _object.set("c", double_with_n_digits(3.14159265358979323846, 3));
  69. outputMustBe("{\"a\":3.1416,\"b\":2.7183,\"c\":3.142}");
  70. }
  71. TEST_F(JsonObject_PrintTo_Tests, TwoDoubleDefaultDigits) {
  72. _object["a"] = 3.14159265358979323846;
  73. _object.set("b", 2.71828182845904523536);
  74. outputMustBe("{\"a\":3.14,\"b\":2.72}");
  75. }
  76. TEST_F(JsonObject_PrintTo_Tests, TwoNull) {
  77. _object["a"] = static_cast<char *>(0);
  78. _object.set("b", static_cast<char *>(0));
  79. outputMustBe("{\"a\":null,\"b\":null}");
  80. }
  81. TEST_F(JsonObject_PrintTo_Tests, TwoBooleans) {
  82. _object["a"] = true;
  83. _object.set("b", false);
  84. outputMustBe("{\"a\":true,\"b\":false}");
  85. }
  86. TEST_F(JsonObject_PrintTo_Tests, ThreeNestedArrays) {
  87. _object.createNestedArray("a");
  88. _object["b"] = _jsonBuffer.createArray();
  89. _object.set("c", _jsonBuffer.createArray());
  90. outputMustBe("{\"a\":[],\"b\":[],\"c\":[]}");
  91. }
  92. TEST_F(JsonObject_PrintTo_Tests, ThreeNestedObjects) {
  93. _object.createNestedObject("a");
  94. _object["b"] = _jsonBuffer.createObject();
  95. _object.set("c", _jsonBuffer.createObject());
  96. outputMustBe("{\"a\":{},\"b\":{},\"c\":{}}");
  97. }