StaticJsonBuffer_ParseArray_Tests.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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) {
  12. _jsonBuffer = &jsonBuffer;
  13. }
  14. void whenInputIs(const char* json) {
  15. strcpy(_jsonString, json);
  16. }
  17. void parseMustSucceed() {
  18. EXPECT_TRUE(_jsonBuffer->parseArray(_jsonString).success());
  19. }
  20. void parseMustFail() {
  21. EXPECT_FALSE(_jsonBuffer->parseArray(_jsonString).success());
  22. }
  23. private:
  24. JsonBuffer* _jsonBuffer;
  25. char _jsonString[256];
  26. };
  27. TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForEmptyArray) {
  28. StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
  29. with(bufferTooSmall);
  30. whenInputIs("[]");
  31. parseMustFail();
  32. }
  33. TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForEmptyArray) {
  34. StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
  35. with(bufferOfRightSize);
  36. whenInputIs("[]");
  37. parseMustSucceed();
  38. }
  39. TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForArrayWithOneValue) {
  40. StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
  41. with(bufferTooSmall);
  42. whenInputIs("[1]");
  43. parseMustFail();
  44. }
  45. TEST_F(StaticJsonBuffer_ParseArray_Tests,
  46. BufferOfTheRightSizeForArrayWithOneValue) {
  47. StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
  48. with(bufferOfRightSize);
  49. whenInputIs("[1]");
  50. parseMustSucceed();
  51. }
  52. TEST_F(StaticJsonBuffer_ParseArray_Tests,
  53. TooSmallBufferForArrayWithNestedObject) {
  54. StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
  55. with(bufferTooSmall);
  56. whenInputIs("[{}]");
  57. parseMustFail();
  58. }
  59. TEST_F(StaticJsonBuffer_ParseArray_Tests,
  60. BufferOfTheRightSizeForArrayWithNestedObject) {
  61. StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> bufferOfRightSize;
  62. with(bufferOfRightSize);
  63. whenInputIs("[{}]");
  64. parseMustSucceed();
  65. }
  66. TEST_F(StaticJsonBuffer_ParseArray_Tests, CharPtrNull) {
  67. ASSERT_FALSE(
  68. StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success());
  69. }
  70. TEST_F(StaticJsonBuffer_ParseArray_Tests, ConstCharPtrNull) {
  71. ASSERT_FALSE(StaticJsonBuffer<100>()
  72. .parseArray(static_cast<const char*>(0))
  73. .success());
  74. }