2014-10-16 16:23:24 +02:00
|
|
|
#include <gtest/gtest.h>
|
2014-10-19 15:46:36 +02:00
|
|
|
#include <ArduinoJson/JsonArray.hpp>
|
|
|
|
#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_Serialization_Tests : public testing::Test {
|
2014-10-16 16:23:24 +02:00
|
|
|
protected:
|
2014-10-23 19:54:00 +02:00
|
|
|
virtual void SetUp() { object = json.createObject(); }
|
|
|
|
|
|
|
|
void outputMustBe(const char *expected) {
|
|
|
|
char actual[256];
|
|
|
|
int result = object.printTo(actual, sizeof(actual));
|
|
|
|
|
|
|
|
EXPECT_STREQ(expected, actual);
|
|
|
|
EXPECT_EQ(strlen(expected), result);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonObject object;
|
|
|
|
StaticJsonBuffer<5> json;
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, EmptyObject) { outputMustBe("{}"); }
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneString) {
|
|
|
|
object["key"] = "value";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key\":\"value\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, TwoStrings) {
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, RemoveFirst) {
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
|
|
|
object.remove("key1");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key2\":\"value2\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, RemoveLast) {
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
|
|
|
object.remove("key2");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key1\":\"value1\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, RemoveUnexistingKey) {
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
|
|
|
object.remove("key3");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, ReplaceExistingKey) {
|
|
|
|
object["key"] = "value1";
|
|
|
|
object["key"] = "value2";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key\":\"value2\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneStringOverCapacity) {
|
|
|
|
object["key1"] = "value1";
|
|
|
|
object["key2"] = "value2";
|
|
|
|
object["key3"] = "value3";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneInteger) {
|
|
|
|
object["key"] = 1;
|
|
|
|
outputMustBe("{\"key\":1}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneDoubleFourDigits) {
|
|
|
|
object["key"].set(3.14159265358979323846, 4);
|
|
|
|
outputMustBe("{\"key\":3.1416}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneDoubleDefaultDigits) {
|
|
|
|
object["key"] = 3.14159265358979323846;
|
|
|
|
outputMustBe("{\"key\":3.14}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneNull) {
|
|
|
|
object["key"] = (char *)0;
|
|
|
|
outputMustBe("{\"key\":null}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneTrue) {
|
|
|
|
object["key"] = true;
|
|
|
|
outputMustBe("{\"key\":true}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneFalse) {
|
|
|
|
object["key"] = false;
|
|
|
|
outputMustBe("{\"key\":false}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArrayViaProxy) {
|
|
|
|
JsonArray nestedArray = json.createArray();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["key"] = nestedArray;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key\":[]}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObjectViaProxy) {
|
|
|
|
JsonObject nestedArray = json.createObject();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["key"] = nestedArray;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key\":{}}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedObject) {
|
|
|
|
object.createNestedObject("key");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key\":{}}");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonObject_Serialization_Tests, OneEmptyNestedArray) {
|
|
|
|
object.createNestedArray("key");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("{\"key\":[]}");
|
2014-10-01 15:47:32 +02:00
|
|
|
}
|