parseObject.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("StaticJsonBuffer::parseObject()") {
  7. SECTION("TooSmallBufferForEmptyObject") {
  8. StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
  9. char input[] = "{}";
  10. JsonObject& obj = bufferTooSmall.parseObject(input);
  11. REQUIRE_FALSE(obj.success());
  12. }
  13. SECTION("BufferOfTheRightSizeForEmptyObject") {
  14. StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
  15. char input[] = "{}";
  16. JsonObject& obj = bufferOfRightSize.parseObject(input);
  17. REQUIRE(obj.success());
  18. }
  19. SECTION("TooSmallBufferForObjectWithOneValue") {
  20. StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
  21. char input[] = "{\"a\":1}";
  22. JsonObject& obj = bufferTooSmall.parseObject(input);
  23. REQUIRE_FALSE(obj.success());
  24. }
  25. SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
  26. StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
  27. char input[] = "{\"a\":1}";
  28. JsonObject& obj = bufferOfRightSize.parseObject(input);
  29. REQUIRE(obj.success());
  30. }
  31. SECTION("TooSmallBufferForObjectWithNestedObject") {
  32. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1>
  33. bufferTooSmall;
  34. char input[] = "{\"a\":[]}";
  35. JsonObject& obj = bufferTooSmall.parseObject(input);
  36. REQUIRE_FALSE(obj.success());
  37. }
  38. SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
  39. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)>
  40. bufferOfRightSize;
  41. char input[] = "{\"a\":[]}";
  42. JsonObject& obj = bufferOfRightSize.parseObject(input);
  43. REQUIRE(obj.success());
  44. }
  45. SECTION("CharPtrNull") {
  46. REQUIRE_FALSE(
  47. StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success());
  48. }
  49. SECTION("ConstCharPtrNull") {
  50. REQUIRE_FALSE(StaticJsonBuffer<100>()
  51. .parseObject(static_cast<const char*>(0))
  52. .success());
  53. }
  54. }