forked from bblanchon/ArduinoJson
Changed unit testing framework from Google Test to Catch
This commit is contained in:
@ -6,13 +6,13 @@
|
||||
# If you like this project, please add a star!
|
||||
|
||||
add_executable(StaticJsonBufferTests
|
||||
basics.cpp
|
||||
alloc.cpp
|
||||
createArray.cpp
|
||||
createObject.cpp
|
||||
parseArray.cpp
|
||||
parseObject.cpp
|
||||
string.cpp
|
||||
startString.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(StaticJsonBufferTests gtest)
|
||||
target_link_libraries(StaticJsonBufferTests catch)
|
||||
add_test(StaticJsonBuffer StaticJsonBufferTests)
|
||||
|
68
test/StaticJsonBuffer/alloc.cpp
Normal file
68
test/StaticJsonBuffer/alloc.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static bool isAligned(void *ptr) {
|
||||
const size_t mask = sizeof(void *) - 1;
|
||||
size_t addr = reinterpret_cast<size_t>(ptr);
|
||||
return (addr & mask) == 0;
|
||||
}
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::alloc()") {
|
||||
StaticJsonBuffer<64> buffer;
|
||||
|
||||
SECTION("CapacityMatchTemplateParameter") {
|
||||
REQUIRE(64 == buffer.capacity());
|
||||
}
|
||||
|
||||
SECTION("InitialSizeIsZero") {
|
||||
REQUIRE(0 == buffer.size());
|
||||
}
|
||||
|
||||
SECTION("GrowsAfterAlloc") {
|
||||
buffer.alloc(1);
|
||||
REQUIRE(1U <= buffer.size());
|
||||
buffer.alloc(1);
|
||||
REQUIRE(2U <= buffer.size());
|
||||
}
|
||||
|
||||
SECTION("DoesntGrowWhenFull") {
|
||||
buffer.alloc(64);
|
||||
buffer.alloc(1);
|
||||
REQUIRE(64 == buffer.size());
|
||||
}
|
||||
|
||||
SECTION("DoesntGrowWhenTooSmall") {
|
||||
buffer.alloc(65);
|
||||
REQUIRE(0 == buffer.size());
|
||||
}
|
||||
|
||||
SECTION("ReturnsNonNull") {
|
||||
void *p = buffer.alloc(64);
|
||||
REQUIRE(0 != p);
|
||||
}
|
||||
|
||||
SECTION("ReturnsNullWhenFull") {
|
||||
buffer.alloc(64);
|
||||
void *p = buffer.alloc(1);
|
||||
REQUIRE(0 == p);
|
||||
}
|
||||
|
||||
SECTION("ReturnsNullWhenTooSmall") {
|
||||
void *p = buffer.alloc(65);
|
||||
REQUIRE(0 == p);
|
||||
}
|
||||
|
||||
SECTION("Alignment") {
|
||||
for (size_t size = 1; size <= sizeof(void *); size++) {
|
||||
void *p = buffer.alloc(1);
|
||||
REQUIRE(isAligned(p));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class StaticJsonBuffer_Basic_Tests : public testing::Test {
|
||||
protected:
|
||||
StaticJsonBuffer<64> buffer;
|
||||
};
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, CapacityMatchTemplateParameter) {
|
||||
ASSERT_EQ(64, buffer.capacity());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, InitialSizeIsZero) {
|
||||
ASSERT_EQ(0, buffer.size());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, GrowsAfterAlloc) {
|
||||
buffer.alloc(1);
|
||||
ASSERT_LE(1U, buffer.size());
|
||||
buffer.alloc(1);
|
||||
ASSERT_LE(2U, buffer.size());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, DoesntGrowWhenFull) {
|
||||
buffer.alloc(64);
|
||||
buffer.alloc(1);
|
||||
ASSERT_EQ(64, buffer.size());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, DoesntGrowWhenTooSmall) {
|
||||
buffer.alloc(65);
|
||||
ASSERT_EQ(0, buffer.size());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, ReturnsNonNull) {
|
||||
void *p = buffer.alloc(64);
|
||||
ASSERT_NE(static_cast<void *>(0), p);
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, ReturnsNullWhenFull) {
|
||||
buffer.alloc(64);
|
||||
void *p = buffer.alloc(1);
|
||||
ASSERT_EQ(NULL, p);
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, ReturnsNullWhenTooSmall) {
|
||||
void *p = buffer.alloc(65);
|
||||
ASSERT_EQ(NULL, p);
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, Alignment) {
|
||||
size_t mask = sizeof(void *) - 1;
|
||||
|
||||
for (size_t size = 1; size <= sizeof(void *); size++) {
|
||||
size_t addr = reinterpret_cast<size_t>(buffer.alloc(1));
|
||||
ASSERT_EQ(0, addr & mask);
|
||||
}
|
||||
}
|
@ -6,41 +6,43 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST(StaticJsonBuffer_CreateArray_Tests, GrowsWithArray) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
||||
TEST_CASE("StaticJsonBuffer::createArray()") {
|
||||
SECTION("GrowsWithArray") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
ASSERT_EQ(JSON_ARRAY_SIZE(0), json.size());
|
||||
JsonArray &array = json.createArray();
|
||||
REQUIRE(JSON_ARRAY_SIZE(0) == json.size());
|
||||
|
||||
array.add("hello");
|
||||
ASSERT_EQ(JSON_ARRAY_SIZE(1), json.size());
|
||||
array.add("hello");
|
||||
REQUIRE(JSON_ARRAY_SIZE(1) == json.size());
|
||||
|
||||
array.add("world");
|
||||
ASSERT_EQ(JSON_ARRAY_SIZE(2), json.size());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer_CreateArray_Tests, SucceedWhenBigEnough) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
ASSERT_TRUE(array.success());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer_CreateArray_Tests, FailsWhenTooSmall) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
ASSERT_FALSE(array.success());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer_CreateArray_Tests, ArrayDoesntGrowWhenFull) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
EXPECT_EQ(1, array.size());
|
||||
array.add("world");
|
||||
REQUIRE(JSON_ARRAY_SIZE(2) == json.size());
|
||||
}
|
||||
|
||||
SECTION("SucceedWhenBigEnough") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
REQUIRE(array.success());
|
||||
}
|
||||
|
||||
SECTION("FailsWhenTooSmall") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
REQUIRE_FALSE(array.success());
|
||||
}
|
||||
|
||||
SECTION("ArrayDoesntGrowWhenFull") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> json;
|
||||
|
||||
JsonArray &array = json.createArray();
|
||||
array.add("hello");
|
||||
array.add("world");
|
||||
|
||||
REQUIRE(1 == array.size());
|
||||
}
|
||||
}
|
||||
|
@ -6,52 +6,54 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST(StaticJsonBuffer_CreateObject_Tests, GrowsWithObject) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> buffer;
|
||||
TEST_CASE("StaticJsonBuffer::createObject()") {
|
||||
SECTION("GrowsWithObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(3)> buffer;
|
||||
|
||||
JsonObject &obj = buffer.createObject();
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(0), buffer.size());
|
||||
JsonObject &obj = buffer.createObject();
|
||||
REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
|
||||
|
||||
obj["hello"];
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(0), buffer.size());
|
||||
obj["hello"];
|
||||
REQUIRE(JSON_OBJECT_SIZE(0) == buffer.size());
|
||||
|
||||
obj["hello"] = 1;
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(1), buffer.size());
|
||||
obj["hello"] = 1;
|
||||
REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
|
||||
|
||||
obj["world"] = 2;
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(2), buffer.size());
|
||||
obj["world"] = 2;
|
||||
REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
|
||||
|
||||
obj["world"] = 3; // <- same key, should not grow
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(2), buffer.size());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer_CreateObject_Tests, SucceedWhenBigEnough) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> buffer;
|
||||
|
||||
JsonObject &object = buffer.createObject();
|
||||
ASSERT_TRUE(object.success());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer_CreateObject_Tests, FailsWhenTooSmall) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> buffer;
|
||||
|
||||
JsonObject &object = buffer.createObject();
|
||||
ASSERT_FALSE(object.success());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer_CreateObject_Tests, ObjectDoesntGrowWhenFull) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> buffer;
|
||||
|
||||
JsonObject &obj = buffer.createObject();
|
||||
obj["hello"] = 1;
|
||||
obj["world"] = 2;
|
||||
|
||||
ASSERT_EQ(JSON_OBJECT_SIZE(1), buffer.size());
|
||||
ASSERT_EQ(1, obj.size());
|
||||
|
||||
char json[64];
|
||||
obj.printTo(json, sizeof(json));
|
||||
ASSERT_STREQ("{\"hello\":1}", json);
|
||||
obj["world"] = 3; // <- same key, should not grow
|
||||
REQUIRE(JSON_OBJECT_SIZE(2) == buffer.size());
|
||||
}
|
||||
|
||||
SECTION("SucceedWhenBigEnough") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> buffer;
|
||||
|
||||
JsonObject &object = buffer.createObject();
|
||||
REQUIRE(object.success());
|
||||
}
|
||||
|
||||
SECTION("FailsWhenTooSmall") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> buffer;
|
||||
|
||||
JsonObject &object = buffer.createObject();
|
||||
REQUIRE_FALSE(object.success());
|
||||
}
|
||||
|
||||
SECTION("ObjectDoesntGrowWhenFull") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> buffer;
|
||||
|
||||
JsonObject &obj = buffer.createObject();
|
||||
obj["hello"] = 1;
|
||||
obj["world"] = 2;
|
||||
|
||||
REQUIRE(JSON_OBJECT_SIZE(1) == buffer.size());
|
||||
REQUIRE(1 == obj.size());
|
||||
|
||||
char json[64];
|
||||
obj.printTo(json, sizeof(json));
|
||||
REQUIRE(std::string("{\"hello\":1}") == json);
|
||||
}
|
||||
}
|
||||
|
@ -6,91 +6,69 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
class StaticJsonBuffer_ParseArray_Tests : public testing::Test {
|
||||
protected:
|
||||
void with(StaticJsonBufferBase& jsonBuffer) {
|
||||
_jsonBuffer = &jsonBuffer;
|
||||
TEST_CASE("StaticJsonBuffer::parseArray()") {
|
||||
SECTION("TooSmallBufferForEmptyArray") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
|
||||
char input[] = "[]";
|
||||
JsonArray& arr = bufferTooSmall.parseArray(input);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
void whenInputIs(const char* json) {
|
||||
strcpy(_jsonString, json);
|
||||
SECTION("BufferOfTheRightSizeForEmptyArray") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
|
||||
char input[] = "[]";
|
||||
JsonArray& arr = bufferOfRightSize.parseArray(input);
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
|
||||
void parseMustSucceed() {
|
||||
EXPECT_TRUE(_jsonBuffer->parseArray(_jsonString).success());
|
||||
SECTION("TooSmallBufferForArrayWithOneValue") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
|
||||
char input[] = "[1]";
|
||||
JsonArray& arr = bufferTooSmall.parseArray(input);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
void parseMustFail() {
|
||||
EXPECT_FALSE(_jsonBuffer->parseArray(_jsonString).success());
|
||||
SECTION("BufferOfTheRightSizeForArrayWithOneValue") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
|
||||
char input[] = "[1]";
|
||||
JsonArray& arr = bufferOfRightSize.parseArray(input);
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
|
||||
private:
|
||||
StaticJsonBufferBase* _jsonBuffer;
|
||||
char _jsonString[256];
|
||||
};
|
||||
SECTION("TooSmallBufferForArrayWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1>
|
||||
bufferTooSmall;
|
||||
char input[] = "[{}]";
|
||||
JsonArray& arr = bufferTooSmall.parseArray(input);
|
||||
REQUIRE_FALSE(arr.success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForEmptyArray) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
|
||||
with(bufferTooSmall);
|
||||
whenInputIs("[]");
|
||||
parseMustFail();
|
||||
}
|
||||
SECTION("BufferOfTheRightSizeForArrayWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)>
|
||||
bufferOfRightSize;
|
||||
char input[] = "[{}]";
|
||||
JsonArray& arr = bufferOfRightSize.parseArray(input);
|
||||
REQUIRE(arr.success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests, BufferOfTheRightSizeForEmptyArray) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(0)> bufferOfRightSize;
|
||||
with(bufferOfRightSize);
|
||||
whenInputIs("[]");
|
||||
parseMustSucceed();
|
||||
}
|
||||
SECTION("CharPtrNull") {
|
||||
REQUIRE_FALSE(
|
||||
StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests, TooSmallBufferForArrayWithOneValue) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) - 1> bufferTooSmall;
|
||||
with(bufferTooSmall);
|
||||
whenInputIs("[1]");
|
||||
parseMustFail();
|
||||
}
|
||||
SECTION("ConstCharPtrNull") {
|
||||
REQUIRE_FALSE(StaticJsonBuffer<100>()
|
||||
.parseArray(static_cast<const char*>(0))
|
||||
.success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests,
|
||||
BufferOfTheRightSizeForArrayWithOneValue) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1)> bufferOfRightSize;
|
||||
with(bufferOfRightSize);
|
||||
whenInputIs("[1]");
|
||||
parseMustSucceed();
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests,
|
||||
TooSmallBufferForArrayWithNestedObject) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
|
||||
with(bufferTooSmall);
|
||||
whenInputIs("[{}]");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests,
|
||||
BufferOfTheRightSizeForArrayWithNestedObject) {
|
||||
StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> bufferOfRightSize;
|
||||
with(bufferOfRightSize);
|
||||
whenInputIs("[{}]");
|
||||
parseMustSucceed();
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests, CharPtrNull) {
|
||||
ASSERT_FALSE(
|
||||
StaticJsonBuffer<100>().parseArray(static_cast<char*>(0)).success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests, ConstCharPtrNull) {
|
||||
ASSERT_FALSE(StaticJsonBuffer<100>()
|
||||
.parseArray(static_cast<const char*>(0))
|
||||
.success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests, CopyStringNotSpaces) {
|
||||
StaticJsonBuffer<100> jsonBuffer;
|
||||
jsonBuffer.parseArray(" [ \"1234567\" ] ");
|
||||
ASSERT_EQ(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
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -6,84 +6,60 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class StaticJsonBuffer_ParseObject_Tests : public testing::Test {
|
||||
protected:
|
||||
void with(StaticJsonBufferBase& jsonBuffer) {
|
||||
_jsonBuffer = &jsonBuffer;
|
||||
#include <catch.hpp>
|
||||
TEST_CASE("StaticJsonBuffer::parseObject()") {
|
||||
SECTION("TooSmallBufferForEmptyObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
|
||||
char input[] = "{}";
|
||||
JsonObject& obj = bufferTooSmall.parseObject(input);
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
void whenInputIs(const char* json) {
|
||||
strcpy(_jsonString, json);
|
||||
SECTION("BufferOfTheRightSizeForEmptyObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
|
||||
char input[] = "{}";
|
||||
JsonObject& obj = bufferOfRightSize.parseObject(input);
|
||||
REQUIRE(obj.success());
|
||||
}
|
||||
|
||||
void parseMustSucceed() {
|
||||
EXPECT_TRUE(_jsonBuffer->parseObject(_jsonString).success());
|
||||
SECTION("TooSmallBufferForObjectWithOneValue") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
|
||||
char input[] = "{\"a\":1}";
|
||||
JsonObject& obj = bufferTooSmall.parseObject(input);
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
void parseMustFail() {
|
||||
EXPECT_FALSE(_jsonBuffer->parseObject(_jsonString).success());
|
||||
SECTION("BufferOfTheRightSizeForObjectWithOneValue") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
|
||||
char input[] = "{\"a\":1}";
|
||||
JsonObject& obj = bufferOfRightSize.parseObject(input);
|
||||
REQUIRE(obj.success());
|
||||
}
|
||||
|
||||
private:
|
||||
StaticJsonBufferBase* _jsonBuffer;
|
||||
char _jsonString[256];
|
||||
};
|
||||
SECTION("TooSmallBufferForObjectWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1>
|
||||
bufferTooSmall;
|
||||
char input[] = "{\"a\":[]}";
|
||||
JsonObject& obj = bufferTooSmall.parseObject(input);
|
||||
REQUIRE_FALSE(obj.success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseObject_Tests, TooSmallBufferForEmptyObject) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
|
||||
with(bufferTooSmall);
|
||||
whenInputIs("{}");
|
||||
parseMustFail();
|
||||
}
|
||||
SECTION("BufferOfTheRightSizeForObjectWithNestedObject") {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)>
|
||||
bufferOfRightSize;
|
||||
char input[] = "{\"a\":[]}";
|
||||
JsonObject& obj = bufferOfRightSize.parseObject(input);
|
||||
REQUIRE(obj.success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseObject_Tests, BufferOfTheRightSizeForEmptyObject) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
|
||||
with(bufferOfRightSize);
|
||||
whenInputIs("{}");
|
||||
parseMustSucceed();
|
||||
}
|
||||
SECTION("CharPtrNull") {
|
||||
REQUIRE_FALSE(
|
||||
StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success());
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseObject_Tests, CharPtrNull) {
|
||||
ASSERT_FALSE(
|
||||
StaticJsonBuffer<100>().parseObject(static_cast<char*>(0)).success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseObject_Tests, ConstCharPtrNull) {
|
||||
ASSERT_FALSE(StaticJsonBuffer<100>()
|
||||
.parseObject(static_cast<const char*>(0))
|
||||
.success());
|
||||
SECTION("ConstCharPtrNull") {
|
||||
REQUIRE_FALSE(StaticJsonBuffer<100>()
|
||||
.parseObject(static_cast<const char*>(0))
|
||||
.success());
|
||||
}
|
||||
}
|
||||
|
50
test/StaticJsonBuffer/startString.cpp
Normal file
50
test/StaticJsonBuffer/startString.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("StaticJsonBuffer::startString()") {
|
||||
SECTION("WorksWhenBufferIsBigEnough") {
|
||||
StaticJsonBuffer<6> jsonBuffer;
|
||||
|
||||
StaticJsonBufferBase::String str = jsonBuffer.startString();
|
||||
str.append('h');
|
||||
str.append('e');
|
||||
str.append('l');
|
||||
str.append('l');
|
||||
str.append('o');
|
||||
|
||||
REQUIRE(std::string("hello") == str.c_str());
|
||||
}
|
||||
|
||||
SECTION("ReturnsNullWhenTooSmall") {
|
||||
StaticJsonBuffer<5> jsonBuffer;
|
||||
|
||||
StaticJsonBufferBase::String str = jsonBuffer.startString();
|
||||
str.append('h');
|
||||
str.append('e');
|
||||
str.append('l');
|
||||
str.append('l');
|
||||
str.append('o');
|
||||
|
||||
REQUIRE(0 == str.c_str());
|
||||
}
|
||||
|
||||
SECTION("SizeIncreases") {
|
||||
StaticJsonBuffer<5> jsonBuffer;
|
||||
|
||||
StaticJsonBufferBase::String str = jsonBuffer.startString();
|
||||
REQUIRE(0 == jsonBuffer.size());
|
||||
|
||||
str.append('h');
|
||||
REQUIRE(1 == jsonBuffer.size());
|
||||
|
||||
str.c_str();
|
||||
REQUIRE(2 == jsonBuffer.size());
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(StaticJsonBuffer_String_Tests, WorksWhenBufferIsBigEnough) {
|
||||
StaticJsonBuffer<6> jsonBuffer;
|
||||
|
||||
StaticJsonBufferBase::String str = jsonBuffer.startString();
|
||||
str.append('h');
|
||||
str.append('e');
|
||||
str.append('l');
|
||||
str.append('l');
|
||||
str.append('o');
|
||||
|
||||
ASSERT_STREQ("hello", str.c_str());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer_String_Tests, ReturnsNullWhenTooSmall) {
|
||||
StaticJsonBuffer<5> jsonBuffer;
|
||||
|
||||
StaticJsonBufferBase::String str = jsonBuffer.startString();
|
||||
str.append('h');
|
||||
str.append('e');
|
||||
str.append('l');
|
||||
str.append('l');
|
||||
str.append('o');
|
||||
|
||||
ASSERT_EQ(NULL, str.c_str());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer_String_Tests, SizeIncreases) {
|
||||
StaticJsonBuffer<5> jsonBuffer;
|
||||
|
||||
StaticJsonBufferBase::String str = jsonBuffer.startString();
|
||||
ASSERT_EQ(0, jsonBuffer.size());
|
||||
|
||||
str.append('h');
|
||||
ASSERT_EQ(1, jsonBuffer.size());
|
||||
|
||||
str.c_str();
|
||||
ASSERT_EQ(2, jsonBuffer.size());
|
||||
}
|
Reference in New Issue
Block a user