Explorar o código

Test that integers in JsonValue are copied

Benoit Blanchon %!s(int64=11) %!d(string=hai) anos
pai
achega
42ce5ab31f
Modificáronse 3 ficheiros con 66 adicións e 18 borrados
  1. 38 4
      srcs/JsonValue.cpp
  2. 2 0
      srcs/JsonValue.h
  3. 26 14
      tests/JsonValueTests.cpp

+ 38 - 4
srcs/JsonValue.cpp

@@ -36,13 +36,36 @@ void JsonValue::operator=(int value)
 
 void JsonValue::operator=(const JsonObject& object)
 {
-    if (_node) 
+    setAsProxyTo(object._node);
+}
+
+void JsonValue::operator=(JsonValue const& value)
+{
+    if (!_node)
     {
-        _node->type = JSON_PROXY;
-        _node->content.asProxy.target = object._node;
+        _node = value._node;
+        return;
     }
     
-    _node = object._node;
+    JsonNodeType nodeType = value._node ? value._node->type : JSON_UNDEFINED;
+    
+
+    switch (nodeType)
+    {
+    case JSON_UNDEFINED:
+        _node->type = JSON_UNDEFINED;
+        break;
+
+    case JSON_INTEGER:
+        _node->type = JSON_INTEGER;
+        _node->content.asInteger = value._node->content.asInteger;
+        break;
+
+    case JSON_OBJECT:
+    case JSON_ARRAY:
+    case JSON_PROXY:
+        setAsProxyTo(value._node);
+    }
 }
 
 JsonValue::operator bool() const
@@ -86,6 +109,17 @@ JsonValue::operator JsonObject() const
     return JsonObject(getActualNode());
 }
 
+void JsonValue::setAsProxyTo(JsonNode* target)
+{
+    if (_node)
+    {
+        _node->type = JSON_PROXY;
+        _node->content.asProxy.target = target;
+    }
+
+    _node = target;
+}
+
 JsonNode* JsonValue::getActualNode() const
 {
     JsonNode* target = _node;

+ 2 - 0
srcs/JsonValue.h

@@ -21,6 +21,7 @@ public:
     void operator=(double);
     void operator=(int);
     void operator=(const JsonObject&);
+    void operator=(const JsonValue&);
     
     operator bool() const;
     operator const char*() const;
@@ -31,6 +32,7 @@ public:
 private:
     JsonNode* _node;
 
+    void setAsProxyTo(JsonNode*);
     JsonNode* getActualNode() const;
 };
 

+ 26 - 14
tests/JsonValueTests.cpp

@@ -7,52 +7,64 @@ class JsonValueTests : public ::testing::Test
 protected:
     virtual void SetUp()
     {
-        jsonValue = json.createValue();
+        jsonValue1 = json.createValue();
+        jsonValue2 = json.createValue();
     }
 
     StaticJsonBuffer<42> json;
-    JsonValue jsonValue;
+    JsonValue jsonValue1;
+    JsonValue jsonValue2;
 };
 
 
 TEST_F(JsonValueTests, CanStoreInteger)
 {
-    jsonValue = 123;
+    jsonValue1 = 123;
 
-    EXPECT_EQ(123, (int) jsonValue);
+    EXPECT_EQ(123, (int) jsonValue1);
 }
 
 TEST_F(JsonValueTests, CanStoreDouble)
 {
-    jsonValue = 123.45;
+    jsonValue1 = 123.45;
 
-    EXPECT_EQ(123.45, (double) jsonValue);
+    EXPECT_EQ(123.45, (double) jsonValue1);
 }
 
 TEST_F(JsonValueTests, CanStoreTrue)
 {
-    jsonValue = true;
-    EXPECT_TRUE((bool) jsonValue);
+    jsonValue1 = true;
+    EXPECT_TRUE((bool) jsonValue1);
 }
 
 TEST_F(JsonValueTests, CanStoreFalse)
 {
-    jsonValue = false;
-    EXPECT_FALSE((bool) jsonValue);
+    jsonValue1 = false;
+    EXPECT_FALSE((bool) jsonValue1);
 }
 
 TEST_F(JsonValueTests, CanStoreString)
 {
-    jsonValue = "hello";
+    jsonValue1 = "hello";
  
-    EXPECT_STREQ("hello", (const char*) jsonValue);
+    EXPECT_STREQ("hello", (const char*) jsonValue1);
 }
 
 TEST_F(JsonValueTests, CanStoreObject)
 {
     JsonObject innerObject1 = json.createObject();
 
-    jsonValue = innerObject1;
+    jsonValue1 = innerObject1;
 
-    EXPECT_EQ(innerObject1, (JsonObject) jsonValue);
+    EXPECT_EQ(innerObject1, (JsonObject) jsonValue1);
+}
+
+TEST_F(JsonValueTests, CanCopyInteger)
+{
+    jsonValue1 = 123;
+    jsonValue2 = jsonValue1;
+    jsonValue1 = 456;
+
+    EXPECT_EQ(456, (int) jsonValue1);
+    EXPECT_EQ(123, (int) jsonValue2);
 }