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 "JsonValue.h"
|
||||
//#include "JsonNode.h"
|
||||
@ -33,6 +33,8 @@
|
||||
|
||||
JsonValue JsonObject::operator[](char const* key)
|
||||
{
|
||||
_buffer->createNode();
|
||||
_buffer->createNode();
|
||||
_size++;
|
||||
return JsonValue();
|
||||
}
|
@ -24,9 +24,8 @@ class JsonObject
|
||||
|
||||
public:
|
||||
|
||||
|
||||
explicit JsonObject()
|
||||
: _size(0)
|
||||
JsonObject(JsonBuffer* buffer)
|
||||
: _size(0), _buffer(buffer)
|
||||
{
|
||||
}
|
||||
|
||||
@ -41,8 +40,8 @@ private:
|
||||
|
||||
int _size;
|
||||
|
||||
// JsonBuffer& _buffer;
|
||||
// JsonNode& _node;
|
||||
JsonBuffer* _buffer;
|
||||
//JsonNode* _node;
|
||||
//
|
||||
// void addNodeAt(char const* key, JsonNode& node);
|
||||
// JsonNode& getNodeAt(const char* key);
|
||||
|
@ -4,8 +4,10 @@
|
||||
#include "JsonObject.h"
|
||||
|
||||
template<int CAPACITY>
|
||||
class StaticJsonBuffer //: public JsonBuffer
|
||||
class StaticJsonBuffer : public JsonBuffer
|
||||
{
|
||||
friend JsonObject;
|
||||
|
||||
public:
|
||||
|
||||
explicit StaticJsonBuffer()
|
||||
@ -15,14 +17,6 @@ public:
|
||||
|
||||
virtual ~StaticJsonBuffer() {}
|
||||
|
||||
JsonObject createObject()
|
||||
{
|
||||
if (_size < CAPACITY)
|
||||
_size++;
|
||||
|
||||
return JsonObject();
|
||||
}
|
||||
|
||||
int capacity()
|
||||
{
|
||||
return CAPACITY;
|
||||
@ -34,7 +28,11 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
//virtual JsonNode& allocateNode();
|
||||
virtual /*JsonNode&*/void allocateNode()
|
||||
{
|
||||
if (_size < CAPACITY)
|
||||
_size++;
|
||||
}
|
||||
|
||||
private:
|
||||
//JsonNode _buffer[CAPACITY];
|
||||
|
@ -65,11 +65,13 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="JsonBuffer.h" />
|
||||
<ClInclude Include="JsonObject.h" />
|
||||
<ClInclude Include="JsonValue.h" />
|
||||
<ClInclude Include="StaticJsonBuffer.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="JsonBuffer.cpp" />
|
||||
<ClCompile Include="JsonObject.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
@ -24,10 +24,16 @@
|
||||
<ClInclude Include="JsonValue.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="JsonBuffer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="JsonObject.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonBuffer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -1,5 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <StaticJsonBuffer.h>
|
||||
#include <JsonValue.h>
|
||||
|
||||
TEST(StaticJsonBuffer, CapacityMatchTemplateParameter)
|
||||
{
|
||||
@ -35,10 +36,22 @@ TEST(StaticJsonBuffer, GivenBufferIsFull_WhenCreateObjectIsCalled_ThenSizeDoesNo
|
||||
EXPECT_EQ(1, json.size());
|
||||
}
|
||||
|
||||
TEST(StaticJsonBuffer, WhenWhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned)
|
||||
TEST(StaticJsonBuffer, WhenCreateObjectIsCalled_ThenAnEmptyJsonObjectIsReturned)
|
||||
{
|
||||
StaticJsonBuffer<42> json;
|
||||
|
||||
JsonObject obj = json.createObject();
|
||||
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