StaticJsonBuffer_Object_Tests.cpp 1.2 KB

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