Test that nested JsonObject can be stored

This commit is contained in:
Benoit Blanchon
2014-09-27 16:18:40 +02:00
parent bcc8cece24
commit bc44c36385
9 changed files with 114 additions and 5 deletions

View File

@ -1,8 +1,10 @@
#include "JsonBuffer.h" #include "JsonBuffer.h"
#include "JsonNode.h"
#include "JsonObject.h"
#include <string.h> // for memset #include <string.h> // for memset
#include "JsonNode.h"
#include "JsonObject.h"
#include "JsonValue.h"
JsonObject JsonBuffer::createObject() JsonObject JsonBuffer::createObject()
{ {
@ -14,6 +16,12 @@ JsonObject JsonBuffer::createObject()
return JsonObject(node); return JsonObject(node);
} }
JsonValue JsonBuffer::createValue()
{
JsonNode* node = createNode(JSON_UNDEFINED);
return JsonValue(node);
}
JsonNode* JsonBuffer::createNode(JsonNodeType type) JsonNode* JsonBuffer::createNode(JsonNodeType type)
{ {
JsonNode* node = allocateNode(); JsonNode* node = allocateNode();

View File

@ -4,6 +4,7 @@
#include "JsonNode.h" #include "JsonNode.h"
class JsonObject; class JsonObject;
class JsonValue;
struct JsonNode; struct JsonNode;
class JsonBuffer class JsonBuffer
@ -14,6 +15,8 @@ public:
// virtual ~JsonBuffer() = 0; // virtual ~JsonBuffer() = 0;
JsonObject createObject(); JsonObject createObject();
JsonValue createValue();
protected: protected:
virtual JsonNode* allocateNode() = 0; virtual JsonNode* allocateNode() = 0;

View File

@ -5,6 +5,8 @@ struct JsonNode;
class JsonObject class JsonObject
{ {
friend JsonValue;
public: public:
JsonObject() JsonObject()
: _node(0) : _node(0)
@ -20,6 +22,11 @@ public:
JsonValue operator[](const char* key); JsonValue operator[](const char* key);
bool operator== (const JsonObject& other) const
{
return _node == other._node;
}
private: private:
JsonNode* _node; JsonNode* _node;

View File

@ -34,6 +34,13 @@ void JsonValue::operator=(int value)
_node->content.asInteger = value; _node->content.asInteger = value;
} }
void JsonValue::operator=(const JsonObject& object)
{
if (!_node) return;
_node = object._node;
}
JsonValue::operator bool() const JsonValue::operator bool() const
{ {
if (!_node || _node->type != JSON_BOOLEAN) return 0; if (!_node || _node->type != JSON_BOOLEAN) return 0;
@ -61,3 +68,8 @@ JsonValue::operator int() const
return _node->content.asInteger; return _node->content.asInteger;
} }
JsonValue::operator JsonObject() const
{
return JsonObject(_node);
}

View File

@ -1,12 +1,16 @@
#pragma once #pragma once
struct JsonNode; struct JsonNode;
//class JsonBuffer;
class JsonValue class JsonValue
{ {
public: public:
explicit JsonValue()
: _node(0)
{
}
explicit JsonValue(JsonNode* node) explicit JsonValue(JsonNode* node)
: _node(node) : _node(node)
{ {
@ -16,14 +20,15 @@ public:
void operator=(const char*); void operator=(const char*);
void operator=(double); void operator=(double);
void operator=(int); void operator=(int);
void operator=(const JsonObject&);
operator bool() const; operator bool() const;
operator const char*() const; operator const char*() const;
operator double() const; operator double() const;
operator int() const; operator int() const;
operator JsonObject() const;
private: private:
//JsonBuffer& _buffer;
JsonNode* _node; JsonNode* _node;
}; };

View File

@ -67,3 +67,15 @@ TEST_F(JsonObjectTests, CanStoreStrings)
EXPECT_STREQ("h3110", (const char*) object["hello"]); EXPECT_STREQ("h3110", (const char*) object["hello"]);
EXPECT_STREQ("w0r1d", (const char*) object["world"]); EXPECT_STREQ("w0r1d", (const char*) object["world"]);
} }
TEST_F(JsonObjectTests, CanStoreInnerObjects)
{
JsonObject innerObject1 = json.createObject();
JsonObject innerObject2 = json.createObject();
object["hello"] = innerObject1;
object["world"] = innerObject2;
EXPECT_EQ(innerObject1, (JsonObject) object["hello"]);
EXPECT_EQ(innerObject2, (JsonObject) object["world"]);
}

58
tests/JsonValueTests.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <gtest/gtest.h>
#include <StaticJsonBuffer.h>
#include <JsonValue.h>
class JsonValueTests : public ::testing::Test
{
protected:
virtual void SetUp()
{
jsonValue = json.createValue();
}
StaticJsonBuffer<42> json;
JsonValue jsonValue;
};
TEST_F(JsonValueTests, CanStoreInteger)
{
jsonValue = 123;
EXPECT_EQ(123, (int) jsonValue);
}
TEST_F(JsonValueTests, CanStoreDouble)
{
jsonValue = 123.45;
EXPECT_EQ(123.45, (double) jsonValue);
}
TEST_F(JsonValueTests, CanStoreTrue)
{
jsonValue = true;
EXPECT_TRUE((bool) jsonValue);
}
TEST_F(JsonValueTests, CanStoreFalse)
{
jsonValue = false;
EXPECT_FALSE((bool) jsonValue);
}
TEST_F(JsonValueTests, CanStoreString)
{
jsonValue = "hello";
EXPECT_STREQ("hello", (const char*) jsonValue);
}
TEST_F(JsonValueTests, CanStoreObject)
{
JsonObject innerObject1 = json.createObject();
jsonValue = innerObject1;
EXPECT_EQ(innerObject1, (JsonObject) jsonValue);
}

View File

@ -86,6 +86,7 @@
<ClCompile Include="..\third-party\gtest-1.7.0\src\gtest-all.cc" /> <ClCompile Include="..\third-party\gtest-1.7.0\src\gtest-all.cc" />
<ClCompile Include="..\third-party\gtest-1.7.0\src\gtest_main.cc" /> <ClCompile Include="..\third-party\gtest-1.7.0\src\gtest_main.cc" />
<ClCompile Include="JsonObjectTests.cpp" /> <ClCompile Include="JsonObjectTests.cpp" />
<ClCompile Include="JsonValueTests.cpp" />
<ClCompile Include="StaticJsonBufferTests.cpp" /> <ClCompile Include="StaticJsonBufferTests.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -30,5 +30,8 @@
<ClCompile Include="JsonObjectTests.cpp"> <ClCompile Include="JsonObjectTests.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="JsonValueTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>