mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-18 13:02:25 +02:00
Test JsonObject::prettyPrintTo()
This commit is contained in:
@ -16,14 +16,12 @@ public:
|
||||
virtual void beginArray()
|
||||
{
|
||||
_length += _sink.write('[');
|
||||
_indenter.indent();
|
||||
_length += _indenter.println();
|
||||
indent();
|
||||
}
|
||||
|
||||
virtual void endArray()
|
||||
{
|
||||
_length += _indenter.println();
|
||||
_indenter.unindent();
|
||||
unindent();
|
||||
_length += _sink.write(']');
|
||||
}
|
||||
|
||||
@ -41,14 +39,27 @@ public:
|
||||
virtual void beginObject()
|
||||
{
|
||||
_length += _sink.write('{');
|
||||
indent();
|
||||
}
|
||||
|
||||
virtual void endObject()
|
||||
{
|
||||
unindent();
|
||||
_length += _sink.write('}');
|
||||
_indenter.unindent();
|
||||
}
|
||||
|
||||
private:
|
||||
IndentedPrint& _indenter;
|
||||
|
||||
void indent()
|
||||
{
|
||||
_indenter.indent();
|
||||
_length += _indenter.println();
|
||||
}
|
||||
|
||||
void unindent()
|
||||
{
|
||||
_length += _indenter.println();
|
||||
_indenter.unindent();
|
||||
}
|
||||
};
|
||||
|
@ -57,7 +57,6 @@ protected:
|
||||
|
||||
bool checkNodeType(JsonNodeType expectedType);
|
||||
|
||||
private:
|
||||
JsonNode* _node;
|
||||
};
|
||||
|
||||
|
@ -33,6 +33,20 @@ void JsonObject::remove(char const* key)
|
||||
}
|
||||
}
|
||||
|
||||
JsonObject JsonObject::createNestedObject(char const* key)
|
||||
{
|
||||
JsonNode* node = getOrCreateNodeAt(key);
|
||||
|
||||
if (node)
|
||||
{
|
||||
node->type = JSON_OBJECT;
|
||||
node->content.asContainer.child = 0;
|
||||
node->content.asContainer.buffer = _node->content.asContainer.buffer;
|
||||
}
|
||||
|
||||
return JsonObject(node);
|
||||
}
|
||||
|
||||
JsonNode* JsonObject::getOrCreateNodeAt(const char* key)
|
||||
{
|
||||
if (!checkNodeType(JSON_OBJECT)) return 0;
|
||||
|
@ -8,6 +8,7 @@ struct JsonNode;
|
||||
class JsonObject : public JsonContainer
|
||||
{
|
||||
public:
|
||||
|
||||
JsonObject()
|
||||
{
|
||||
}
|
||||
@ -20,6 +21,8 @@ public:
|
||||
JsonValue operator[](const char* key);
|
||||
void remove(const char* key);
|
||||
|
||||
JsonObject createNestedObject(const char* key);
|
||||
|
||||
private:
|
||||
JsonNode* getOrCreateNodeAt(char const* key);
|
||||
};
|
89
tests/JsonObject_PrettyPrintTo_Tests.cpp
Normal file
89
tests/JsonObject_PrettyPrintTo_Tests.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Arduino JSON library
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <JsonObject.h>
|
||||
#include <JsonValue.h>
|
||||
#include <StaticJsonBuffer.h>
|
||||
|
||||
class JsonObject_PrettyPrintTo_Tests : public testing::Test
|
||||
{
|
||||
protected:
|
||||
JsonObject object;
|
||||
StaticJsonBuffer<30> json;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
object = json.createObject();
|
||||
}
|
||||
|
||||
void outputMustBe(const char* expected)
|
||||
{
|
||||
size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), n);
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject)
|
||||
{
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember)
|
||||
{
|
||||
object["key"] = "value";
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key\": \"value\"\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers)
|
||||
{
|
||||
object["key1"] = "value1";
|
||||
object["key2"] = "value2";
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key1\": \"value1\",\r\n"
|
||||
" \"key2\": \"value2\"\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedObjects)
|
||||
{
|
||||
object.createNestedObject("key1");
|
||||
object.createNestedObject("key2");
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key1\": {},\r\n"
|
||||
" \"key2\": {}\r\n"
|
||||
"}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedObjects)
|
||||
{
|
||||
JsonObject nested1 = object.createNestedObject("key1");
|
||||
nested1["a"] = 1;
|
||||
|
||||
JsonObject nested2 = object.createNestedObject("key2");
|
||||
nested2["b"] = 2;
|
||||
|
||||
outputMustBe(
|
||||
"{\r\n"
|
||||
" \"key1\": {\r\n"
|
||||
" \"a\": 1\r\n"
|
||||
" },\r\n"
|
||||
" \"key2\": {\r\n"
|
||||
" \"b\": 2\r\n"
|
||||
" }\r\n"
|
||||
"}");
|
||||
}
|
@ -89,6 +89,7 @@
|
||||
<ClCompile Include="JsonArray_Container_Tests.cpp" />
|
||||
<ClCompile Include="JsonArray_PrettyPrintTo_Tests.cpp" />
|
||||
<ClCompile Include="JsonArray_PrintTo_Tests.cpp" />
|
||||
<ClCompile Include="JsonObject_PrettyPrintTo_Tests.cpp" />
|
||||
<ClCompile Include="JsonObject_Serialization_Tests.cpp" />
|
||||
<ClCompile Include="JsonObject_Container_Tests.cpp" />
|
||||
<ClCompile Include="JsonValueTests.cpp" />
|
||||
|
@ -51,5 +51,8 @@
|
||||
<ClCompile Include="JsonArray_PrettyPrintTo_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JsonObject_PrettyPrintTo_Tests.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
Reference in New Issue
Block a user