From e28119f03b127ed45c8170b15cbecc2b9fe5e102 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 7 Oct 2014 11:58:59 +0200 Subject: [PATCH] Test JsonObject::prettyPrintTo() --- srcs/Internals/PrettyJsonWriter.h | 21 ++++-- srcs/JsonContainer.h | 1 - srcs/JsonObject.cpp | 14 ++++ srcs/JsonObject.h | 3 + tests/JsonObject_PrettyPrintTo_Tests.cpp | 89 ++++++++++++++++++++++++ tests/tests.vcxproj | 1 + tests/tests.vcxproj.filters | 3 + 7 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 tests/JsonObject_PrettyPrintTo_Tests.cpp diff --git a/srcs/Internals/PrettyJsonWriter.h b/srcs/Internals/PrettyJsonWriter.h index d4fe18c5..b565697d 100644 --- a/srcs/Internals/PrettyJsonWriter.h +++ b/srcs/Internals/PrettyJsonWriter.h @@ -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(); + } }; diff --git a/srcs/JsonContainer.h b/srcs/JsonContainer.h index 28bce5a5..de4ccb55 100644 --- a/srcs/JsonContainer.h +++ b/srcs/JsonContainer.h @@ -57,7 +57,6 @@ protected: bool checkNodeType(JsonNodeType expectedType); -private: JsonNode* _node; }; diff --git a/srcs/JsonObject.cpp b/srcs/JsonObject.cpp index eedfd48f..61d88b60 100644 --- a/srcs/JsonObject.cpp +++ b/srcs/JsonObject.cpp @@ -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; diff --git a/srcs/JsonObject.h b/srcs/JsonObject.h index 73cb0551..0b631211 100644 --- a/srcs/JsonObject.h +++ b/srcs/JsonObject.h @@ -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); }; \ No newline at end of file diff --git a/tests/JsonObject_PrettyPrintTo_Tests.cpp b/tests/JsonObject_PrettyPrintTo_Tests.cpp new file mode 100644 index 00000000..3837985e --- /dev/null +++ b/tests/JsonObject_PrettyPrintTo_Tests.cpp @@ -0,0 +1,89 @@ +/* +* Arduino JSON library +* Benoit Blanchon 2014 - MIT License +*/ + +#include +#include +#include +#include + +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" + "}"); +} \ No newline at end of file diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index 1242bd17..25945158 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -89,6 +89,7 @@ + diff --git a/tests/tests.vcxproj.filters b/tests/tests.vcxproj.filters index 3b1e3201..31240bc6 100644 --- a/tests/tests.vcxproj.filters +++ b/tests/tests.vcxproj.filters @@ -51,5 +51,8 @@ Source Files + + Source Files + \ No newline at end of file