parseObject.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <catch.hpp>
  9. TEST_CASE("StaticJsonBuffer::parseObject()") {
  10. SECTION("TooSmallBufferForEmptyObject") {
  11. StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
  12. char input[] = "{}";
  13. JsonObject& obj = bufferTooSmall.parseObject(input);
  14. REQUIRE_FALSE(obj.success());
  15. }
  16. SECTION("BufferOfTheRightSizeForEmptyObject") {
  17. StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
  18. char input[] = "{}";
  19. JsonObject& obj = bufferOfRightSize.parseObject(input);
  20. REQUIRE(obj.success());
  21. }
  22. SECTION("TooSmallBufferForObjectWithOneValue") {
  23. StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
  24. char input[] = "{\"a\":1}";
  25. JsonObject& obj = bufferTooSmall.parseObject(input);
  26. REQUIRE_FALSE(obj.success());
  27. }
  28. SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
  29. StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
  30. char input[] = "{\"a\":1}";
  31. JsonObject& obj = bufferOfRightSize.parseObject(input);
  32. REQUIRE(obj.success());
  33. }
  34. SECTION("TooSmallBufferForObjectWithNestedObject") {
  35. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1>
  36. bufferTooSmall;
  37. char input[] = "{\"a\":[]}";
  38. JsonObject& obj = bufferTooSmall.parseObject(input);
  39. REQUIRE_FALSE(obj.success());
  40. }
  41. SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
  42. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)>
  43. bufferOfRightSize;
  44. char input[] = "{\"a\":[]}";
  45. JsonObject& obj = bufferOfRightSize.parseObject(input);
  46. REQUIRE(obj.success());
  47. }
  48. SECTION("CharPtrNull") {
  49. REQUIRE_FALSE(
  50. StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success());
  51. }
  52. SECTION("ConstCharPtrNull") {
  53. REQUIRE_FALSE(StaticJsonBuffer<100>()
  54. .parseObject(static_cast<const char*>(0))
  55. .success());
  56. }
  57. }