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