Browse Source

Test empty object serialization

Benoit Blanchon 11 năm trước cách đây
mục cha
commit
ab2587f089

+ 5 - 0
srcs/JsonObject.cpp

@@ -87,4 +87,9 @@ JsonNode* JsonObject::getOrCreateNodeAt(char const* key)
         _node->content.asObject.child = newKeyNode;
 
     return newValueNode;
+}
+
+void JsonObject::serialize(char* buffer, size_t bufferSize) const
+{
+    strcpy_s(buffer, bufferSize, "{}");
 }

+ 2 - 0
srcs/JsonObject.h

@@ -24,6 +24,8 @@ public:
 
     bool operator==(const JsonObject& other) const;
 
+    void serialize(char* buffer, size_t bufferSize) const;
+
 private:
     JsonNode* _node;
 

+ 30 - 0
tests/JsonObjectSerializationTests.cpp

@@ -0,0 +1,30 @@
+#include <gtest/gtest.h>
+#include <JsonObject.h>
+#include <StaticJsonBuffer.h>
+
+class JsonObjectSerializationTests : public testing::Test
+{
+protected:
+    virtual void SetUp()
+    {
+        object = json.createObject();
+    }
+
+    void jsonMustBe(const char* expected)
+    {        
+        char actual[256];
+        object.serialize(actual, sizeof(actual));
+
+        EXPECT_STREQ(expected, actual);
+    }
+
+    JsonObject object;
+
+private:
+    StaticJsonBuffer<42> json;
+};
+
+TEST_F(JsonObjectSerializationTests, EmptyObject)
+{
+    jsonMustBe("{}");
+}

+ 12 - 0
tests/JsonValueTests.cpp

@@ -93,4 +93,16 @@ TEST_F(JsonValueTests, CharPointersAreCopied)
     jsonValue1 = "world";
 
     EXPECT_STREQ("hello", (const char*) jsonValue2);
+}
+
+TEST_F(JsonValueTests, ObjectPointsAreCopied)
+{
+    JsonObject object = json.createObject();
+
+    jsonValue1 = object;
+    jsonValue2 = jsonValue1;
+    
+    object["hello"] = "world";
+
+    EXPECT_EQ(1, ((JsonObject) jsonValue2).size());
 }

+ 1 - 0
tests/tests.vcxproj

@@ -85,6 +85,7 @@
   <ItemGroup>
     <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="JsonObjectSerializationTests.cpp" />
     <ClCompile Include="JsonObjectTests.cpp" />
     <ClCompile Include="JsonValueTests.cpp" />
     <ClCompile Include="StaticJsonBufferTests.cpp" />

+ 3 - 0
tests/tests.vcxproj.filters

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