forked from bblanchon/ArduinoJson
Test that adding values to the JsonObject increase the size of the buffer
This commit is contained in:
15
srcs/JsonBuffer.cpp
Normal file
15
srcs/JsonBuffer.cpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include "JsonBuffer.h"
|
||||||
|
//#include "JsonNode.h"
|
||||||
|
#include "JsonObject.h"
|
||||||
|
|
||||||
|
|
||||||
|
JsonObject JsonBuffer::createObject()
|
||||||
|
{
|
||||||
|
allocateNode();
|
||||||
|
return JsonObject(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void JsonBuffer::createNode()
|
||||||
|
{
|
||||||
|
allocateNode();
|
||||||
|
}
|
22
srcs/JsonBuffer.h
Normal file
22
srcs/JsonBuffer.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
class JsonObject;
|
||||||
|
struct JsonNode;
|
||||||
|
|
||||||
|
class JsonBuffer
|
||||||
|
{
|
||||||
|
friend class JsonObject;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// virtual ~JsonBuffer() = 0;
|
||||||
|
|
||||||
|
JsonObject createObject();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual /*JsonNode&*/void allocateNode() = 0;
|
||||||
|
|
||||||
|
/*JsonNode&*/void createNode();
|
||||||
|
};
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
//#include "JsonBuffer.h"
|
#include "JsonBuffer.h"
|
||||||
#include "JsonObject.h"
|
#include "JsonObject.h"
|
||||||
#include "JsonValue.h"
|
#include "JsonValue.h"
|
||||||
//#include "JsonNode.h"
|
//#include "JsonNode.h"
|
||||||
@ -33,6 +33,8 @@
|
|||||||
|
|
||||||
JsonValue JsonObject::operator[](char const* key)
|
JsonValue JsonObject::operator[](char const* key)
|
||||||
{
|
{
|
||||||
|
_buffer->createNode();
|
||||||
|
_buffer->createNode();
|
||||||
_size++;
|
_size++;
|
||||||
return JsonValue();
|
return JsonValue();
|
||||||
}
|
}
|
@ -24,9 +24,8 @@ class JsonObject
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
JsonObject(JsonBuffer* buffer)
|
||||||
explicit JsonObject()
|
: _size(0), _buffer(buffer)
|
||||||
: _size(0)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,8 +40,8 @@ private:
|
|||||||
|
|
||||||
int _size;
|
int _size;
|
||||||
|
|
||||||
// JsonBuffer& _buffer;
|
JsonBuffer* _buffer;
|
||||||
// JsonNode& _node;
|
//JsonNode* _node;
|
||||||
//
|
//
|
||||||
// void addNodeAt(char const* key, JsonNode& node);
|
// void addNodeAt(char const* key, JsonNode& node);
|
||||||
// JsonNode& getNodeAt(const char* key);
|
// JsonNode& getNodeAt(const char* key);
|
||||||
|
@ -4,8 +4,10 @@
|
|||||||
#include "JsonObject.h"
|
#include "JsonObject.h"
|
||||||
|
|
||||||
template<int CAPACITY>
|
template<int CAPACITY>
|
||||||
class StaticJsonBuffer //: public JsonBuffer
|
class StaticJsonBuffer : public JsonBuffer
|
||||||
{
|
{
|
||||||
|
friend JsonObject;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
explicit StaticJsonBuffer()
|
explicit StaticJsonBuffer()
|
||||||
@ -15,14 +17,6 @@ public:
|
|||||||
|
|
||||||
virtual ~StaticJsonBuffer() {}
|
virtual ~StaticJsonBuffer() {}
|
||||||
|
|
||||||
JsonObject createObject()
|
|
||||||
{
|
|
||||||
if (_size < CAPACITY)
|
|
||||||
_size++;
|
|
||||||
|
|
||||||
return JsonObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
int capacity()
|
int capacity()
|
||||||
{
|
{
|
||||||
return CAPACITY;
|
return CAPACITY;
|
||||||
@ -34,7 +28,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//virtual JsonNode& allocateNode();
|
virtual /*JsonNode&*/void allocateNode()
|
||||||
|
{
|
||||||
|
if (_size < CAPACITY)
|
||||||
|
_size++;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//JsonNode _buffer[CAPACITY];
|
//JsonNode _buffer[CAPACITY];
|
||||||
|
@ -65,11 +65,13 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="JsonBuffer.h" />
|
||||||
<ClInclude Include="JsonObject.h" />
|
<ClInclude Include="JsonObject.h" />
|
||||||
<ClInclude Include="JsonValue.h" />
|
<ClInclude Include="JsonValue.h" />
|
||||||
<ClInclude Include="StaticJsonBuffer.h" />
|
<ClInclude Include="StaticJsonBuffer.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="JsonBuffer.cpp" />
|
||||||
<ClCompile Include="JsonObject.cpp" />
|
<ClCompile Include="JsonObject.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
@ -24,10 +24,16 @@
|
|||||||
<ClInclude Include="JsonValue.h">
|
<ClInclude Include="JsonValue.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="JsonBuffer.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="JsonObject.cpp">
|
<ClCompile Include="JsonObject.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="JsonBuffer.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -1,5 +1,6 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <StaticJsonBuffer.h>
|
#include <StaticJsonBuffer.h>
|
||||||
|
#include <JsonValue.h>
|
||||||
|
|
||||||
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter)
|
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter)
|
||||||
{
|
{
|
||||||
@ -35,10 +36,22 @@ TEST(StaticJsonBuffer, GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNo
|
|||||||
EXPECT_EQ(1, json.size());
|
EXPECT_EQ(1, json.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(StaticJsonBuffer, WhenWhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned)
|
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned)
|
||||||
{
|
{
|
||||||
StaticJsonBuffer<42> json;
|
StaticJsonBuffer<42> json;
|
||||||
|
|
||||||
JsonObject obj = json.createObject();
|
JsonObject obj = json.createObject();
|
||||||
EXPECT_EQ(0, obj.size());
|
EXPECT_EQ(0, obj.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(StaticJsonBuffer, GivenAJsonObject_WhenValuesAreAdded_ThenSizeIsIncreasedByTwo)
|
||||||
|
{
|
||||||
|
StaticJsonBuffer<42> json;
|
||||||
|
JsonObject obj = json.createObject();
|
||||||
|
|
||||||
|
obj["hello"];
|
||||||
|
EXPECT_EQ(3, json.size());
|
||||||
|
|
||||||
|
obj["world"];
|
||||||
|
EXPECT_EQ(5, json.size());
|
||||||
}
|
}
|
Reference in New Issue
Block a user