2014-10-16 16:23:24 +02:00
|
|
|
/*
|
|
|
|
* Arduino JSON library
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2014-10-19 15:46:36 +02:00
|
|
|
#include <ArduinoJson/JsonObject.hpp>
|
|
|
|
#include <ArduinoJson/JsonValue.hpp>
|
|
|
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-18 23:05:54 +02:00
|
|
|
using namespace ArduinoJson;
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
2014-10-23 23:13:13 +02:00
|
|
|
protected:
|
2014-10-23 19:54:00 +02:00
|
|
|
JsonObject object;
|
|
|
|
StaticJsonBuffer<30> json;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
virtual void SetUp() { object = json.createObject(); }
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void outputMustBe(const char *expected) {
|
|
|
|
size_t n = object.prettyPrintTo(buffer, sizeof(buffer));
|
|
|
|
EXPECT_STREQ(expected, buffer);
|
|
|
|
EXPECT_EQ(strlen(expected), n);
|
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
private:
|
2014-10-23 19:54:00 +02:00
|
|
|
char buffer[256];
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
|
|
|
|
object["key"] = "value";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"{\r\n"
|
|
|
|
" \"key\": \"value\"\r\n"
|
|
|
|
"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"{\r\n"
|
|
|
|
" \"key1\": \"value1\",\r\n"
|
|
|
|
" \"key2\": \"value2\"\r\n"
|
|
|
|
"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
|
|
|
|
object.createNestedObject("key1");
|
|
|
|
object.createNestedArray("key2");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"{\r\n"
|
|
|
|
" \"key1\": {},\r\n"
|
|
|
|
" \"key2\": []\r\n"
|
|
|
|
"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
|
|
|
|
JsonObject nested1 = object.createNestedObject("key1");
|
|
|
|
nested1["a"] = 1;
|
|
|
|
|
|
|
|
JsonArray nested2 = object.createNestedArray("key2");
|
|
|
|
nested2.add(2);
|
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"{\r\n"
|
|
|
|
" \"key1\": {\r\n"
|
|
|
|
" \"a\": 1\r\n"
|
|
|
|
" },\r\n"
|
|
|
|
" \"key2\": [\r\n"
|
|
|
|
" 2\r\n"
|
|
|
|
" ]\r\n"
|
|
|
|
"}");
|
2014-10-07 11:58:59 +02:00
|
|
|
}
|