2014-09-30 16:31:22 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <JsonObject.h>
|
2014-09-30 16:59:44 +02:00
|
|
|
#include <JsonValue.h>
|
2014-09-30 16:31:22 +02:00
|
|
|
#include <StaticJsonBuffer.h>
|
|
|
|
|
|
|
|
class JsonObjectSerializationTests : public testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual void SetUp()
|
|
|
|
{
|
|
|
|
object = json.createObject();
|
|
|
|
}
|
|
|
|
|
2014-09-30 16:59:44 +02:00
|
|
|
void outputMustBe(const char* expected)
|
2014-09-30 16:31:22 +02:00
|
|
|
{
|
|
|
|
char actual[256];
|
2014-09-30 16:43:10 +02:00
|
|
|
int result = object.printTo(actual, sizeof(actual));
|
2014-09-30 16:31:22 +02:00
|
|
|
|
|
|
|
EXPECT_STREQ(expected, actual);
|
2014-09-30 16:43:10 +02:00
|
|
|
EXPECT_EQ(strlen(expected), result);
|
2014-09-30 16:31:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject object;
|
|
|
|
|
|
|
|
private:
|
2014-09-30 17:14:59 +02:00
|
|
|
StaticJsonBuffer<5> json;
|
2014-09-30 16:31:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, EmptyObject)
|
|
|
|
{
|
2014-09-30 16:59:44 +02:00
|
|
|
outputMustBe("{}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, OneString)
|
|
|
|
{
|
|
|
|
object["key"] = "value";
|
|
|
|
|
|
|
|
outputMustBe("{\"key\":\"value\"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, TwoStrings)
|
|
|
|
{
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
|
|
|
|
|
|
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
|
|
|
}
|
2014-09-30 17:08:59 +02:00
|
|
|
|
2014-09-30 16:59:44 +02:00
|
|
|
TEST_F(JsonObjectSerializationTests, RemoveFirst)
|
|
|
|
{
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
|
|
|
object.remove("key1");
|
|
|
|
|
|
|
|
outputMustBe("{\"key2\":\"value2\"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, RemoveLast)
|
|
|
|
{
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
|
|
|
object.remove("key2");
|
|
|
|
|
|
|
|
outputMustBe("{\"key1\":\"value1\"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, RemoveUnexistingKey)
|
|
|
|
{
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
|
|
|
object.remove("key3");
|
|
|
|
|
|
|
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, ReplaceExistingKey)
|
|
|
|
{
|
|
|
|
object["key"] = "value1";
|
|
|
|
object["key"] = "value2";
|
|
|
|
|
|
|
|
outputMustBe("{\"key\":\"value2\"}");
|
|
|
|
}
|
2014-09-30 17:14:59 +02:00
|
|
|
|
2014-09-30 16:59:44 +02:00
|
|
|
TEST_F(JsonObjectSerializationTests, OneStringOverCapacity)
|
|
|
|
{
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
|
|
|
object["key3"] = "value3";
|
|
|
|
|
|
|
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
|
|
|
}
|
2014-09-30 17:24:14 +02:00
|
|
|
|
2014-09-30 16:59:44 +02:00
|
|
|
TEST_F(JsonObjectSerializationTests, OneInteger)
|
|
|
|
{
|
|
|
|
object["key"] = 1;
|
|
|
|
outputMustBe("{\"key\":1}");
|
|
|
|
}
|
2014-09-30 17:32:45 +02:00
|
|
|
|
2014-09-30 16:59:44 +02:00
|
|
|
TEST_F(JsonObjectSerializationTests, OneDoubleFourDigits)
|
|
|
|
{
|
2014-09-30 17:32:45 +02:00
|
|
|
object["key"].set(3.14159265358979323846, 4);
|
2014-09-30 16:59:44 +02:00
|
|
|
outputMustBe("{\"key\":3.1416}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, OneDoubleDefaultDigits)
|
|
|
|
{
|
|
|
|
object["key"] = 3.14159265358979323846;
|
|
|
|
outputMustBe("{\"key\":3.14}");
|
|
|
|
}
|
2014-09-30 17:56:28 +02:00
|
|
|
|
2014-09-30 16:59:44 +02:00
|
|
|
TEST_F(JsonObjectSerializationTests, OneNull)
|
|
|
|
{
|
|
|
|
object["key"] = (char*) 0;
|
|
|
|
outputMustBe("{\"key\":null}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, OneTrue)
|
|
|
|
{
|
|
|
|
object["key"] = true;
|
|
|
|
outputMustBe("{\"key\":true}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, OneFalse)
|
|
|
|
{
|
|
|
|
object["key"] = false;
|
|
|
|
outputMustBe("{\"key\":false}");
|
|
|
|
}
|
2014-09-30 17:56:28 +02:00
|
|
|
/*
|
2014-09-30 16:59:44 +02:00
|
|
|
TEST_F(JsonObjectSerializationTests, OneEmptyNestedArray)
|
|
|
|
{
|
|
|
|
auto nestedArray = JsonArray<1>();
|
|
|
|
|
|
|
|
object["key"] = nestedArray;
|
|
|
|
|
|
|
|
outputMustBe("{\"key\":[]}");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonObjectSerializationTests, OneEmptyNestedObject)
|
|
|
|
{
|
|
|
|
auto nestedObject = JsonObject<1>();
|
|
|
|
|
|
|
|
object["key"] = nestedObject;
|
|
|
|
|
|
|
|
outputMustBe("{\"key\":{}}");
|
|
|
|
}*/
|