Test that integers can be stored in a JsonObject

This commit is contained in:
Benoit Blanchon
2014-09-27 14:51:50 +02:00
parent a2fc188526
commit 71fd2de675
7 changed files with 63 additions and 18 deletions

View File

@ -24,7 +24,7 @@ struct JsonNode
union
{
// int asInteger;
int asInteger;
struct
{

View File

@ -48,7 +48,7 @@ size_t JsonObject::size()
JsonValue JsonObject::operator[](char const* key)
{
JsonNode* node = getOrCreateNodeAt(key);
return JsonValue(/*node*/);
return JsonValue(node);
}
JsonNode* JsonObject::getOrCreateNodeAt(char const* key)

28
srcs/JsonValue.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "JsonObject.h"
#include "JsonNode.h"
#include "JsonValue.h"
//void JsonValue::operator=(JsonObject const& object)
//{
// _node = object._node;
//}
void JsonValue::operator=(int value)
{
if (!_node) return;
_node->type = JSON_INTEGER;
_node->content.asInteger = value;
}
//JsonValue::operator JsonObject()
//{
// return JsonObject(_buffer, _node);
//}
JsonValue::operator int()
{
if (!_node || _node->type != JSON_INTEGER) return 0;
return _node->content.asInteger;
}

View File

@ -6,21 +6,21 @@
class JsonValue
{
//public:
//
// JsonValue(JsonBuffer& buffer, JsonNode& node)
// : _buffer(buffer), _node(node)
// {
// }
//
// void operator=(const JsonObject& object);
// void operator=(int);
//
// operator JsonObject();
// operator int();
//
//private:
// JsonBuffer& _buffer;
// JsonNode& _node;
public:
JsonValue(JsonNode* node)
: _node(node)
{
}
// void operator=(const JsonObject& object);
void operator=(int);
// operator JsonObject();
operator int();
private:
//JsonBuffer& _buffer;
JsonNode* _node;
};

View File

@ -74,6 +74,7 @@
<ItemGroup>
<ClCompile Include="JsonBuffer.cpp" />
<ClCompile Include="JsonObject.cpp" />
<ClCompile Include="JsonValue.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -38,5 +38,8 @@
<ClCompile Include="JsonBuffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="JsonValue.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -26,4 +26,17 @@ TEST(JsonObjectTests, WhenTheSameValueIsAddedTwice_ThenSizeIsOnlyIncreasedByOne)
object["hello"];
EXPECT_EQ(1, object.size());
}
TEST(JsonObjectTests, WhenAnIntegerIsStore_TheSameIntegerIsRetreived)
{
StaticJsonBuffer<42> json;
JsonObject object = json.createObject();
object["hello"] = 123;
object["world"] = 456;
EXPECT_EQ(123, (int) object["hello"]);
EXPECT_EQ(456, (int) object["world"]);
}