StaticJsonBuffer_Object_Tests.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright Benoit Blanchon 2014
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include <gtest/gtest.h>
  7. #include <ArduinoJson/JsonObject.hpp>
  8. #include <ArduinoJson/StaticJsonBuffer.hpp>
  9. using namespace ArduinoJson;
  10. TEST(StaticJsonBuffer_Object_Tests, GrowsWithObject) {
  11. StaticJsonBuffer<JSON_OBJECT_SIZE(3)> json;
  12. JsonObject &obj = json.createObject();
  13. ASSERT_EQ(JSON_OBJECT_SIZE(0), json.size());
  14. obj["hello"];
  15. ASSERT_EQ(JSON_OBJECT_SIZE(1), json.size());
  16. obj["world"];
  17. ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
  18. obj["world"]; // <- same value, should not grow
  19. ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
  20. }
  21. TEST(StaticJsonBuffer_Object_Tests, SucceedWhenBigEnough) {
  22. StaticJsonBuffer<JSON_OBJECT_SIZE(0)> json;
  23. JsonObject &object = json.createObject();
  24. ASSERT_TRUE(object.success());
  25. }
  26. TEST(StaticJsonBuffer_Object_Tests, FailsWhenTooSmall) {
  27. StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> json;
  28. JsonObject &object = json.createObject();
  29. ASSERT_FALSE(object.success());
  30. }
  31. TEST(StaticJsonBuffer_Object_Tests, ObjectDoesntGrowWhenFull) {
  32. StaticJsonBuffer<JSON_OBJECT_SIZE(1)> json;
  33. JsonObject &obj = json.createObject();
  34. obj["hello"];
  35. obj["world"];
  36. ASSERT_EQ(JSON_OBJECT_SIZE(1), json.size());
  37. }