StaticJsonBuffer_ParseObject_Tests.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_ParseObject_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->parseObject(_jsonString).success());
  15. }
  16. void parseMustFail() {
  17. EXPECT_FALSE(_jsonBuffer->parseObject(_jsonString).success());
  18. }
  19. private:
  20. JsonBuffer* _jsonBuffer;
  21. char _jsonString[256];
  22. };
  23. TEST_F(StaticJsonBuffer_ParseObject_Tests, TooSmallBufferForEmptyObject) {
  24. StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
  25. with(bufferTooSmall);
  26. whenInputIs("{}");
  27. parseMustFail();
  28. }
  29. TEST_F(StaticJsonBuffer_ParseObject_Tests, BufferOfTheRightSizeForEmptyObject) {
  30. StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
  31. with(bufferOfRightSize);
  32. whenInputIs("{}");
  33. parseMustSucceed();
  34. }
  35. TEST_F(StaticJsonBuffer_ParseObject_Tests,
  36. TooSmallBufferForObjectWithOneValue) {
  37. StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
  38. with(bufferTooSmall);
  39. whenInputIs("{\"a\":1}");
  40. parseMustFail();
  41. }
  42. TEST_F(StaticJsonBuffer_ParseObject_Tests,
  43. BufferOfTheRightSizeForObjectWithOneValue) {
  44. StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
  45. with(bufferOfRightSize);
  46. whenInputIs("{\"a\":1}");
  47. parseMustSucceed();
  48. }
  49. TEST_F(StaticJsonBuffer_ParseObject_Tests,
  50. TooSmallBufferForObjectWithNestedObject) {
  51. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
  52. with(bufferTooSmall);
  53. whenInputIs("{\"a\":[]}");
  54. parseMustFail();
  55. }
  56. TEST_F(StaticJsonBuffer_ParseObject_Tests,
  57. BufferOfTheRightSizeForObjectWithNestedObject) {
  58. StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> bufferOfRightSize;
  59. with(bufferOfRightSize);
  60. whenInputIs("{\"a\":[]}");
  61. parseMustSucceed();
  62. }
  63. TEST_F(StaticJsonBuffer_ParseObject_Tests, CharPtrNull) {
  64. ASSERT_FALSE(StaticJsonBuffer<100>().parseObject((char*)0).success());
  65. }
  66. TEST_F(StaticJsonBuffer_ParseObject_Tests, ConstCharPtrNull) {
  67. ASSERT_FALSE(StaticJsonBuffer<100>().parseObject((const char*)0).success());
  68. }