parseArray.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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::parseArray()") {
  10. SECTION("TooSmallBufferForEmptyArray") {
  11. StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
  12. char input[] = "[]";
  13. JsonArray& arr = bufferTooSmall.parseArray(input);
  14. REQUIRE_FALSE(arr.success());
  15. }
  16. SECTION("BufferOfTheRightSizeForEmptyArray") {
  17. StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
  18. char input[] = "[]";
  19. JsonArray& arr = bufferOfRightSize.parseArray(input);
  20. REQUIRE(arr.success());
  21. }
  22. SECTION("TooSmallBufferForArrayWithOneValue") {
  23. StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
  24. char input[] = "[1]";
  25. JsonArray& arr = bufferTooSmall.parseArray(input);
  26. REQUIRE_FALSE(arr.success());
  27. }
  28. SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
  29. StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
  30. char input[] = "[1]";
  31. JsonArray& arr = bufferOfRightSize.parseArray(input);
  32. REQUIRE(arr.success());
  33. }
  34. SECTION("TooSmallBufferForArrayWithNestedObject") {
  35. StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1>
  36. bufferTooSmall;
  37. char input[] = "[{}]";
  38. JsonArray& arr = bufferTooSmall.parseArray(input);
  39. REQUIRE_FALSE(arr.success());
  40. }
  41. SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
  42. StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)>
  43. bufferOfRightSize;
  44. char input[] = "[{}]";
  45. JsonArray& arr = bufferOfRightSize.parseArray(input);
  46. REQUIRE(arr.success());
  47. }
  48. SECTION("CharPtrNull") {
  49. REQUIRE_FALSE(
  50. StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success());
  51. }
  52. SECTION("ConstCharPtrNull") {
  53. REQUIRE_FALSE(StaticJsonBuffer<100>()
  54. .parseArray(static_cast<const char*>(0))
  55. .success());
  56. }
  57. SECTION("CopyStringNotSpaces") {
  58. StaticJsonBuffer<100> jsonBuffer;
  59. jsonBuffer.parseArray(" [ \"1234567\" ] ");
  60. REQUIRE(JSON_ARRAY_SIZE(1) + sizeof("1234567") == jsonBuffer.size());
  61. // note we use a string of 8 bytes to be sure that the StaticJsonBuffer
  62. // will not insert bytes to enforce alignement
  63. }
  64. }