2017-01-06 21:07:34 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2017
|
2015-04-27 15:57:40 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
2017-03-25 22:05:06 +01:00
|
|
|
// https://bblanchon.github.io/ArduinoJson/
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2015-04-27 15:57:40 +02:00
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2017-01-06 21:07:34 +01:00
|
|
|
#include <gtest/gtest.h>
|
2015-04-27 15:57:40 +02:00
|
|
|
|
|
|
|
class StaticJsonBuffer_ParseObject_Tests : public testing::Test {
|
|
|
|
protected:
|
2016-12-29 17:26:16 +01:00
|
|
|
void with(StaticJsonBufferBase& jsonBuffer) {
|
2016-06-22 21:41:19 +02:00
|
|
|
_jsonBuffer = &jsonBuffer;
|
|
|
|
}
|
2015-04-27 15:57:40 +02:00
|
|
|
|
2016-06-22 21:41:19 +02:00
|
|
|
void whenInputIs(const char* json) {
|
|
|
|
strcpy(_jsonString, json);
|
|
|
|
}
|
2015-04-27 15:57:40 +02:00
|
|
|
|
|
|
|
void parseMustSucceed() {
|
|
|
|
EXPECT_TRUE(_jsonBuffer->parseObject(_jsonString).success());
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseMustFail() {
|
|
|
|
EXPECT_FALSE(_jsonBuffer->parseObject(_jsonString).success());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-12-29 17:26:16 +01:00
|
|
|
StaticJsonBufferBase* _jsonBuffer;
|
2015-04-27 15:57:40 +02:00
|
|
|
char _jsonString[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(StaticJsonBuffer_ParseObject_Tests, TooSmallBufferForEmptyObject) {
|
|
|
|
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
|
|
|
|
with(bufferTooSmall);
|
|
|
|
whenInputIs("{}");
|
|
|
|
parseMustFail();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StaticJsonBuffer_ParseObject_Tests, BufferOfTheRightSizeForEmptyObject) {
|
|
|
|
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
|
|
|
|
with(bufferOfRightSize);
|
|
|
|
whenInputIs("{}");
|
|
|
|
parseMustSucceed();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StaticJsonBuffer_ParseObject_Tests,
|
|
|
|
TooSmallBufferForObjectWithOneValue) {
|
|
|
|
StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
|
|
|
|
with(bufferTooSmall);
|
|
|
|
whenInputIs("{\"a\":1}");
|
|
|
|
parseMustFail();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StaticJsonBuffer_ParseObject_Tests,
|
|
|
|
BufferOfTheRightSizeForObjectWithOneValue) {
|
|
|
|
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
|
|
|
|
with(bufferOfRightSize);
|
|
|
|
whenInputIs("{\"a\":1}");
|
|
|
|
parseMustSucceed();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StaticJsonBuffer_ParseObject_Tests,
|
|
|
|
TooSmallBufferForObjectWithNestedObject) {
|
|
|
|
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
|
|
|
|
with(bufferTooSmall);
|
|
|
|
whenInputIs("{\"a\":[]}");
|
|
|
|
parseMustFail();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StaticJsonBuffer_ParseObject_Tests,
|
|
|
|
BufferOfTheRightSizeForObjectWithNestedObject) {
|
|
|
|
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> bufferOfRightSize;
|
|
|
|
with(bufferOfRightSize);
|
|
|
|
whenInputIs("{\"a\":[]}");
|
|
|
|
parseMustSucceed();
|
2015-08-29 22:51:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StaticJsonBuffer_ParseObject_Tests, CharPtrNull) {
|
2016-06-22 21:41:19 +02:00
|
|
|
ASSERT_FALSE(
|
|
|
|
StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success());
|
2015-08-29 22:51:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StaticJsonBuffer_ParseObject_Tests, ConstCharPtrNull) {
|
2016-06-22 21:41:19 +02:00
|
|
|
ASSERT_FALSE(StaticJsonBuffer<100>()
|
|
|
|
.parseObject(static_cast<const char*>(0))
|
|
|
|
.success());
|
2015-08-29 22:51:17 +02:00
|
|
|
}
|