Test that JsonArray grows after calling add()

This commit is contained in:
Benoit Blanchon
2014-10-05 14:48:19 +02:00
parent cb3c59ec07
commit 4c67d0579a
5 changed files with 22 additions and 15 deletions

View File

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

View File

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

View File

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

View File

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

View File

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