StaticJsonBuffer_ParseObject_Tests.cpp 2.4 KB

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