StaticJsonBuffer_ParseArray_Tests.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. class StaticJsonBuffer_ParseArray_Tests : public testing::Test {
  9. protected:
  10. void with(JsonBuffer& jsonBuffer) { _jsonBuffer = &jsonBuffer; }
  11. void whenInputIs(const char* json) { strcpy(_jsonString, json); }
  12. void parseMustSucceed() {
  13. EXPECT_TRUE(_jsonBuffer->parseArray(_jsonString).success());
  14. }
  15. void parseMustFail() {
  16. EXPECT_FALSE(_jsonBuffer->parseArray(_jsonString).success());
  17. }
  18. private:
  19. JsonBuffer* _jsonBuffer;
  20. char _jsonString[256];
  21. };
  22. TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForEmptyArray) {
  23. StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
  24. with(bufferTooSmall);
  25. whenInputIs("[]");
  26. parseMustFail();
  27. }
  28. TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForEmptyArray) {
  29. StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
  30. with(bufferOfRightSize);
  31. whenInputIs("[]");
  32. parseMustSucceed();
  33. }
  34. TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForArrayWithOneValue) {
  35. StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
  36. with(bufferTooSmall);
  37. whenInputIs("[1]");
  38. parseMustFail();
  39. }
  40. TEST_F(StaticJsonBuffer_ParseArray_Tests,
  41. BufferOfTheRightSizeForArrayWithOneValue) {
  42. StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
  43. with(bufferOfRightSize);
  44. whenInputIs("[1]");
  45. parseMustSucceed();
  46. }
  47. TEST_F(StaticJsonBuffer_ParseArray_Tests,
  48. TooSmallBufferForArrayWithNestedObject) {
  49. StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
  50. with(bufferTooSmall);
  51. whenInputIs("[{}]");
  52. parseMustFail();
  53. }
  54. TEST_F(StaticJsonBuffer_ParseArray_Tests,
  55. BufferOfTheRightSizeForArrayWithNestedObject) {
  56. StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> bufferOfRightSize;
  57. with(bufferOfRightSize);
  58. whenInputIs("[{}]");
  59. parseMustSucceed();
  60. }