Files
ArduinoJson/test/StaticJsonBuffer/parseArray.cpp

75 lines
2.3 KiB
C++
Raw Normal View History

// 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/
// If you like this project, please add a star!
2015-04-27 15:57:40 +02:00
#include <ArduinoJson.h>
#include <catch.hpp>
2015-04-27 15:57:40 +02:00
TEST_CASE("StaticJsonBuffer::parseArray()") {
SECTION("TooSmallBufferForEmptyArray") {
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
char input[] = "[]";
JsonArray& arr = bufferTooSmall.parseArray(input);
REQUIRE_FALSE(arr.success());
}
2015-04-27 15:57:40 +02:00
SECTION("BufferOfTheRightSizeForEmptyArray") {
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
char input[] = "[]";
JsonArray& arr = bufferOfRightSize.parseArray(input);
REQUIRE(arr.success());
}
2015-04-27 15:57:40 +02:00
SECTION("TooSmallBufferForArrayWithOneValue") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
char input[] = "[1]";
JsonArray& arr = bufferTooSmall.parseArray(input);
REQUIRE_FALSE(arr.success());
2015-04-27 15:57:40 +02:00
}
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
char input[] = "[1]";
JsonArray& arr = bufferOfRightSize.parseArray(input);
REQUIRE(arr.success());
2015-04-27 15:57:40 +02:00
}
SECTION("TooSmallBufferForArrayWithNestedObject") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1>
bufferTooSmall;
char input[] = "[{}]";
JsonArray& arr = bufferTooSmall.parseArray(input);
REQUIRE_FALSE(arr.success());
}
2015-04-27 15:57:40 +02:00
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)>
bufferOfRightSize;
char input[] = "[{}]";
JsonArray& arr = bufferOfRightSize.parseArray(input);
REQUIRE(arr.success());
}
SECTION("CharPtrNull") {
REQUIRE_FALSE(
StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success());
}
SECTION("ConstCharPtrNull") {
REQUIRE_FALSE(StaticJsonBuffer<100>()
.parseArray(static_cast<const char*>(0))
.success());
}
SECTION("CopyStringNotSpaces") {
StaticJsonBuffer<100> jsonBuffer;
jsonBuffer.parseArray(" [ \"1234567\" ] ");
REQUIRE(JSON_ARRAY_SIZE(1) + sizeof("1234567") == jsonBuffer.size());
// note we use a string of 8 bytes to be sure that the StaticJsonBuffer
// will not insert bytes to enforce alignement
}
}