StaticJsonBuffer_ParseObject_Tests.cpp 2.0 KB

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