Răsfoiți Sursa

Test that JsonArray grows after calling add()

Benoit Blanchon 11 ani în urmă
părinte
comite
4c67d0579a

+ 5 - 2
srcs/JsonArray.h

@@ -16,7 +16,10 @@ public:
 
  //   JsonValue operator[](int index);
 
-   // template<typename T>
-   // void add(T value);
+    template<typename T>
+    void add(T value)
+    {
+        addChild(createNode(JSON_UNDEFINED));
+    }
 };
 

+ 13 - 5
srcs/JsonContainer.cpp

@@ -32,12 +32,20 @@ bool JsonContainer::operator==(const JsonContainer & other) const
     return _node == other._node;
 }
 
-void JsonContainer::insertChildAfter(JsonNode* newChild, JsonNode* previous)
+void JsonContainer::addChild(JsonNode* newChild)
 {
-    if (previous)
-        previous->next = newChild;
-    else
-        _node->content.asContainer.child = newChild;
+    JsonNode* lastChild = _node->content.asContainer.child;
+
+    if (!lastChild)
+    {
+        _node->content.asContainer.child = newChild = newChild;
+        return;
+    }
+
+    while (lastChild->next)
+        lastChild = lastChild->next;
+
+    lastChild->next = newChild;
 }
 
 void JsonContainer::removeChildAfter(JsonNode* child, JsonNode* previous)

+ 1 - 1
srcs/JsonContainer.h

@@ -41,7 +41,7 @@ protected:
         return JsonNodeIterator(0);
     }
 
-    void insertChildAfter(JsonNode* newChild, JsonNode* insertAfterMe);
+    void addChild(JsonNode* newChild);
     void removeChildAfter(JsonNode* child, JsonNode* previous);
     JsonNode* createNode(JsonNodeType type);
 

+ 1 - 5
srcs/JsonObject.cpp

@@ -38,16 +38,12 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
 {
     if (!checkNodeType(JSON_OBJECT)) return 0;
 
-    JsonNode* lastChild = 0;
-
     for (JsonNodeIterator it = beginChildren(); it != endChildren(); ++it)
     {
         const char* childKey = it->content.asKey.key;
 
         if (!strcmp(childKey, key))
             return it->content.asKey.value;
-
-        lastChild = *it;
     }
       
     JsonNode* newValueNode = createNode(JSON_UNDEFINED);
@@ -59,7 +55,7 @@ JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
     newKeyNode->content.asKey.key = key;
     newKeyNode->content.asKey.value = newValueNode;
 
-    insertChildAfter(newKeyNode, lastChild);
+    addChild(newKeyNode);
 
     return newValueNode;
 }

+ 2 - 2
tests/JsonArray_Container_Tests.cpp

@@ -18,7 +18,7 @@ TEST_F(JsonArray_Container_Tests, InitialSizeIsZero)
 {
     EXPECT_EQ(0, array.size());
 }
-/*
+
 TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded)
 {
     array.add("hello");
@@ -27,7 +27,7 @@ TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded)
     array.add("world");
     EXPECT_EQ(2, array.size());
 }
-
+/*
 TEST_F(JsonArray_Container_Tests, CanStoreIntegers)
 {
     array.add(123);