StaticJsonBuffer_ParseArray_Tests.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright Benoit Blanchon 2014-2016
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #include <gtest/gtest.h>
  8. #include <ArduinoJson.h>
  9. class StaticJsonBuffer_ParseArray_Tests : public testing::Test {
  10. protected:
  11. void with(JsonBuffer& jsonBuffer) { _jsonBuffer = &jsonBuffer; }
  12. void whenInputIs(const char* json) { strcpy(_jsonString, json); }
  13. void parseMustSucceed() {
  14. EXPECT_TRUE(_jsonBuffer->parseArray(_jsonString).success());
  15. }
  16. void parseMustFail() {
  17. EXPECT_FALSE(_jsonBuffer->parseArray(_jsonString).success());
  18. }
  19. private:
  20. JsonBuffer* _jsonBuffer;
  21. char _jsonString[256];
  22. };
  23. TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForEmptyArray) {
  24. StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
  25. with(bufferTooSmall);
  26. whenInputIs("[]");
  27. parseMustFail();
  28. }
  29. TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForEmptyArray) {
  30. StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
  31. with(bufferOfRightSize);
  32. whenInputIs("[]");
  33. parseMustSucceed();
  34. }
  35. TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForArrayWithOneValue) {
  36. StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
  37. with(bufferTooSmall);
  38. whenInputIs("[1]");
  39. parseMustFail();
  40. }
  41. TEST_F(StaticJsonBuffer_ParseArray_Tests,
  42. BufferOfTheRightSizeForArrayWithOneValue) {
  43. StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
  44. with(bufferOfRightSize);
  45. whenInputIs("[1]");
  46. parseMustSucceed();
  47. }
  48. TEST_F(StaticJsonBuffer_ParseArray_Tests,
  49. TooSmallBufferForArrayWithNestedObject) {
  50. StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
  51. with(bufferTooSmall);
  52. whenInputIs("[{}]");
  53. parseMustFail();
  54. }
  55. TEST_F(StaticJsonBuffer_ParseArray_Tests,
  56. BufferOfTheRightSizeForArrayWithNestedObject) {
  57. StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> bufferOfRightSize;
  58. with(bufferOfRightSize);
  59. whenInputIs("[{}]");
  60. parseMustSucceed();
  61. }
  62. TEST_F(StaticJsonBuffer_ParseArray_Tests, CharPtrNull) {
  63. ASSERT_FALSE(StaticJsonBuffer<100>().parseArray((char*)0).success());
  64. }
  65. TEST_F(StaticJsonBuffer_ParseArray_Tests, ConstCharPtrNull) {
  66. ASSERT_FALSE(StaticJsonBuffer<100>().parseArray((const char*)0).success());
  67. }